xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

CSS 定位 relative && absolute 问题?

1

1

1

 

CSS 定位 relative && absolute 问题?

谁能解释一下,为什么div使用 relative是设置right,bottom 后,看不到div 呀,哪里多出来的 top , left 属性负值呀?

(已测试了所有最新的浏览器,自动添加的top , left 属性负值)

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS 定位:relative &&  absolute</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.3);
            color: red;
            box-sizing: border-box;
            overflow: hidden;
        }   
       .relative{           
            background-color: #f0f;
            width: 100px;
            height: 100px;
            position: relative;           
            right: 100px;
            bottom: 100px;
            /*
                position: relative; 浏览器,自动补全 :
                left == -right;
                top == -bottom;
            */
        }      
    </style>
</head>
<body>
    <div class="box">
        <div class="relative">
            <!--comments-->
        </div>
    </div>
</body>
</html>

1.

2. chrome 中修改后,

3. 同样,设置 left,top 后, 多出来 right/bottom 的负值属性?

 

 

1

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS 定位:relative &&  absolute</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.3);
            color: red;
            box-sizing: border-box;
            overflow: hidden;
        }
        .box{
            width: 1000px;
            height: 600px;
            border: 1px solid red;
            /*overflow: hidden;*/
        }   
        .box .relative{           
            background-color: #f0f;
            width: 100px;
            height: 100px;
            position: relative;           
            right: 50px;
            bottom: 50px;
            /*
                position: relative; 浏览器,自动补全 :
                left == -right;
                top == -bottom;
            */
        }
        .box .relative-new{
            background-color: #0f0;
            width: 100px;
            height: 100px;
            position: relative;           
            right: 0;
            bottom: 0;
        }      
    </style>
</head>
<body>
    <div class="box">
        <div class="relative">
            <h3>relative</h3>
        </div>
        <div class="relative-new">
            <h3>relative-new</h3>
        </div>
    </div>
</body>
</html>

 

1

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS 定位:relative &&  absolute</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.3);
            color: red;
            box-sizing: border-box;
            overflow: hidden;
        }
        .box{
            width: 1000px;
            height: 600px;
            border: 1px solid red;
            /*overflow: hidden;*/
        }   
        .box .relative{           
            background-color: #0f0;
            width: 100px;
            height: 100px;
            position: relative;           
            right: 100px;
            bottom: 100px;
            /*
            */
        }
        .box .relative-new{
            background-color: #0f0;
            width: 100px;
            height: 100px;
            position: relative;           
            left: 100px;
            top: 100px;
        }
        .box .absolute{           
            background-color: #0f0;
            width: 100px;
            height: 100px;
            position: absolute;           
            right: 100px;
            bottom: 100px;
            /*
                position:absolute;
                right: 200px;
                bottom: 200px;
                这样才好使!
                //position:absolute; float脱离文档流
            */
        }       
    </style>
</head>
<body>
    <div class="box">
        <div class="relative">
            <h3>relative</h3>
        </div>
        <div class="relative-new">
            <h3>relative-new</h3>
        </div>
        <div class="absolute">
            <h3>absolute</h3>
        </div>
    </div>
</body>
</html>

 

1

1

1

1

1

1

1

1

1

1

1

1

posted @   xgqfrms  阅读(139)  评论(4编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示