CSS——position定位属性

就像photoshop中的图层功能会把一整张图片分层一个个图层一样,网页布局中的每一个元素也可以看成是一个个类似图层的层模型。层布局模型就是把网页中的每一个元素看成是一层一层的,然后通过定位属性position对元素进行定位摆放,最终实现网页的布局。

定位属性position有4个值,分别是静态定位(static)、相对定位(relative)、绝对定位(absolute)和固定定位(fixed)。默认就是static。所以我们略过。

元素设置了定位以后,还要依靠4个方位属性来进行定位摆放。

方位属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
top:让元素相对于指定目标的顶部偏移指定的距离。
  例如: top:10px; 表示距离顶部10像素
 
right:让元素相对于指定目标的右边偏移指定的距离。
  例如: right:10px; 表示距离顶部10像素
 
bottom:让元素相对于指定目标的底部偏移指定的距离。
  例如: bottom:10px; 表示距离顶部10像素
 
left:让元素相对于指定目标的左边偏移指定的距离。
  例如: left:10px; 表示距离顶部10像素
*/

  

  • 相对定位(relative)

相对定位就是在正常文档流中,元素相对于自身位置使用left、right、top、bottom属性进行定位偏移。

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
.c1{
           width: 200px;
           height: 200px;
           background-color: indianred;
 
       }
 
       .c2{
           width: 200px;
           height: 200px;
           background-color: orange;
           position: relative;
           left: 200px;
           top: 200px;
 
 
       }
 
       .c3{
           width: 200px;
           height: 200px;
           background-color: lightblue;
 
 
       }

  

 

  • 绝对定位(absolute)

绝对定位就是将元素脱离文档流,然后使用left、right、top、bottom属性相对于其最接近的一个具有定位属性的父级元素进行绝对定位,如果不存在这样的父级元素,则默认是相对于body元素进行绝对定位。

轮播图案例:

 

 

 

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
 
        *{
            margin: 0;
            padding: 0;
        }
        .outer .img img{
             width: 590px;
             height: 470px;
        }
 
        .outer{
             width: 590px;
             height: 470px;
             margin: 100px auto;
             position: relative;
             border: 1px solid red;
        }
 
        .outer ul{
            list-style: none;
        }
 
        .outer .img li{
             position: absolute;
             top: 0;
             left: 0;
        }
 
        .outer .hide{
            display: none;
        }
 
        .outer .num{
            position: absolute;
            z-index: 100;
            bottom: 16px;
            left: 16px;
 
        }
 
        .outer .num li{
           display: inline-block;
            width: 16px;
            height: 16px;
            background-color: lightgray;
            text-align: center;
            line-height: 16px;
            border-radius: 50%;
            margin-left: 5px;
        }
 
        .num li.current{
            background-color: red;
 
        }
 
        .btn li{
            position: absolute;
            top:50%;
            width: 30px;
            height: 60px;
            background-color: gray;
            text-align: center;
            line-height: 60px;
            color: white;
            margin-top: -30px;
        }
 
        .btn .left_btn{
            left: 0;
        }
 
        .btn .right_btn{
            right: 0;
        }
 
 
    </style>
</head>
<body>
 
 
 
<div class="outer">
      <ul class="img">
            <li><a href=""><img src="https://imgcps.jd.com/ling4/100009077475/5Lqs6YCJ5aW96LSn/5L2g5YC85b6X5oul5pyJ/p-5bd8253082acdd181d02fa71/c3196f74/cr/s/q.jpg" alt=""></a></li>
            <li class="hide"><a href=""><img src="https://img12.360buyimg.com/pop/s590x470_jfs/t1/178599/8/1142/28979/6087858aE1679d862/173e0cfa2612b705.jpg.webp" alt=""></a></li>
            <li class="hide"><a href=""><img src="https://imgcps.jd.com/ling4/6038430/5Lqs5Lic5aW954mp57K-6YCJ/MuS7tjjmipgz5Lu2N-aKmA/p-5bd8253082acdd181d02fa42/9ea6716c/cr/s/q.jpg" alt=""></a></li>
            <li class="hide"><a href=""><img src="https://img12.360buyimg.com/pop/s1180x940_jfs/t1/174771/34/8431/98985/6095eaa2E8b8b4847/044f1b6318db4a9f.jpg.webp" alt=""></a></li>
            <li class="hide"><a href=""><img src="https://img11.360buyimg.com/pop/s1180x940_jfs/t1/180648/29/4209/88436/609f7547Ec7b73259/74a4d25e8d614173.jpg.webp" alt=""></a></li>
      </ul>
 
     <ul class="num">
         <li class="current"></li>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
     </ul>
 
    <ul class="btn">
        <li class="left_btn"> < </li>
        <li class="right_btn"> > </li>
    </ul>
</div>
 
 
 
</body>
</html>

  

 

  • 固定定位(fixed)

固定定位与绝对定位有点相似,但是固定定位是使用left、right、top、bottom属性相对于整个浏览器的窗口进行定位,而不再相对于某个HTML页面元素了,所以当元素使用了固定定位以后,就算页面的滚动条滚动了,固定定位的元素也不会变化位置。也就是说固定定位是相对于窗口的定位,不受文档流的影响了。

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
<!DOCTYPE HTML>
<html lang="en-US">
  <head>
      <meta charset="utf8">
    <style>
 
        body{
            margin: 0;
        }
 
        .c1{
            width: 100%;
            height: 2000px;
            background-color: lightgray;
        }
 
        .c2{
            width: 200px;
            height: 60px;
            background-color: yellowgreen;
            text-align: center;
            line-height: 60px;
            position: fixed;
            right: 20px;
            bottom: 20px;
        }
         
    </style>
  </head>
  <body>
 
   <div class="c1"></div>
   <div class="c2">返回顶部</div>
 
  </body>
</html>

  

 

posted @   映辉  阅读(453)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示