绝对定位、相对定位、固定定位的区别有哪些?

原文链接:https://note.noxussj.top/?source=helloworld


正常布局

文档流布局方式,按照顺序一个个排列好,效果如下图:

1<html> 2 <head> 3 <style> 4 .box1 { 5 width: 100px; 6 height: 100px; 7 background-color: #ff8077; 8 } 9 10 .box2 { 11 width: 100px; 12 height: 100px; 13 background-color: #5cd8a2; 14 } 15 </style> 16 </head> 17 <body> 18 <div class="box1"></div> 19 <div class="box2"></div> 20 </body> 21</html>

image

在演练场中尝试一下

绝对定位

该元素脱离文档流,不参与布局一个个排列。完全自由想去哪里就去哪里。写了绝对定位就要写上 left 和 top。 这两者默认是距离文档左上角的距离。

1<html> 2 <head> 3 <style> 4 .box1 { 5 position: absolute; 6 left: 20px; 7 top: 20px; 8 width: 100px; 9 height: 100px; 10 background-color: #ff8077; 11 } 12 13 .box2 { 14 width: 100px; 15 height: 100px; 16 background-color: #5cd8a2; 17 } 18 </style> 19 </head> 20 <body> 21 <div class="box1"></div> 22 <div class="box2"></div> 23 </body> 24</html>

::: warning 由于预览模式是模拟的,为了让小伙伴更好的观看,可以把灰色想象成就是文档部分 body 标签。而且 body 标签默认是自带了 8px 的 margin。 :::

image

在演练场中尝试一下

相对定位

刚才已经介绍了绝对定位可以通过 left 和 top 来控制距离文档左上角的距离,也就是说 left 和 top 是相对于 "文档" 进行定位的。而相对定位则是把这个相对于 "某某元素" 进行修改。

1<html> 2 <head> 3 <style> 4 .box1 { 5 position: absolute; 6 left: 20px; 7 top: 20px; 8 width: 100px; 9 height: 100px; 10 background-color: #ff8077; 11 } 12 13 .box2 { 14 position: relative; 15 width: 100px; 16 height: 100px; 17 background-color: #5cd8a2; 18 } 19 </style> 20 </head> 21 <body> 22 <div class="box2"> 23 <div class="box1"></div> 24 </div> 25 </body> 26</html>

image

现在把 box1 放进 box2 里面,并且给 box2 设置相对定位 position: relative 那么 box1 的 left 和 top 就会相对于 box2 的左上角原点。

在演练场中尝试一下

固定定位

固定定位和绝对定位很像,可以让元素飘起来,想去哪里去哪里。但是绝对定位是相对于 "某某元素" 进行定位的。而固定定位则是永远是相对于 "浏览器可视区左上角"。尽管出现了滚动条也是丝毫不影响。

1<html> 2 <head> 3 <style> 4 .box1 { 5 position: fixed; 6 left: 0; 7 top: 0; 8 width: 100px; 9 height: 100px; 10 background-color: #ff8077; 11 } 12 13 .box2 { 14 position: relative; 15 width: 100px; 16 height: 100px; 17 background-color: #5cd8a2; 18 } 19 </style> 20 </head> 21 <body> 22 <div class="box2"> 23 <div class="box1"></div> 24 </div> 25 </body> 26</html>

image

可以发现尽管 box1 在 box2 里面,并且 box2 也设置了相对定位,box1 依然直接无视了,直接相对于 "可视区左上角" 进行定位。

在演练场中尝试一下

点赞
收藏

评论区

加载中...

相关推荐

干货 | CSS中的四种定位有什么区别?

我们都知道,前端开发里面的CSS中常用的定位方式有普通定位,相对定位,绝对定位、固定定位定位这四种。但是很多零基础的前端小白都不知道这4种定位方式都有什

你需要知道的CSS基础样式知识

原文链接:定位相关相对定位css.mycontentposition:relative;绝对定位css.mycontentposition:absolute;left:0;top:0;固定定位css.mycontentposition:fixed;left

【Flutter实战】层叠布局(Stack、Positioned)

4.5层叠布局Stack、Positioned层叠布局和Web中的绝对定位、Android中的Frame布局是相似的,子组件可以根据距父容器四个角的位置来确定自身的位置。绝对定位允许子组件堆叠起来(按照代码中声明的顺序)。Flutter中使用Stack和Positioned这两个组件来配合实现绝对定位。Stack允许子组件堆叠,而Pos

Python+Selenium自动化篇

本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子。0.元素定位方法主要有:id定位:find\_element\_by\_id('')name定位:find\_element\_by\_name('')class定位:find\_element\_by\_class\_name(''

CSS之border绘制三角形

用CSS的border可以画出高质量的三角形。我们一般会这么使用border:testborder{width:100px;height:100px;margin:100pxauto;background:fff;border:2px

CSS定位和滚动条

0805自我总结一.绝对定位position:absolute;/绝对定位:1、定位属性值:absolute2、在页面中不再占位(浮起来了),就无法继承父级的宽度(必须自己自定义宽度)3、一旦定位后,定位的布局方位top、bottom、left、right都能参与布局