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

菜园前端
• 阅读 195

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


正常布局

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

<html>
    <head>
        <style>
            .box1 {
                width: 100px;
                height: 100px;
                background-color: #ff8077;
            }

            .box2 {
                width: 100px;
                height: 100px;
                background-color: #5cd8a2;
            }
        </style>
    </head>
    <body>
        <div class="box1"></div>
        <div class="box2"></div>
    </body>
</html>

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

在演练场中尝试一下

绝对定位

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

<html>
    <head>
        <style>
            .box1 {
                position: absolute;
                left: 20px;
                top: 20px;
                width: 100px;
                height: 100px;
                background-color: #ff8077;
            }

            .box2 {
                width: 100px;
                height: 100px;
                background-color: #5cd8a2;
            }
        </style>
    </head>
    <body>
        <div class="box1"></div>
        <div class="box2"></div>
    </body>
</html>

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

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

在演练场中尝试一下

相对定位

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

<html>
    <head>
        <style>
            .box1 {
                position: absolute;
                left: 20px;
                top: 20px;
                width: 100px;
                height: 100px;
                background-color: #ff8077;
            }

            .box2 {
                position: relative;
                width: 100px;
                height: 100px;
                background-color: #5cd8a2;
            }
        </style>
    </head>
    <body>
        <div class="box2">
            <div class="box1"></div>
        </div>
    </body>
</html>

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

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

在演练场中尝试一下

固定定位

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

<html>
    <head>
        <style>
            .box1 {
                position: fixed;
                left: 0;
                top: 0;
                width: 100px;
                height: 100px;
                background-color: #ff8077;
            }

            .box2 {
                position: relative;
                width: 100px;
                height: 100px;
                background-color: #5cd8a2;
            }
        </style>
    </head>
    <body>
        <div class="box2">
            <div class="box1"></div>
        </div>
    </body>
</html>

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

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

在演练场中尝试一下

点赞
收藏
评论区
推荐文章
九路 九路
3年前
干货 | CSS中的四种定位有什么区别?
我们都知道,前端开发里面的CSS中常用的定位方式有普通定位,相对定位,绝对定位、固定定位定位这四种。但是很多零基础的前端小白都不知道这4种定位方式都有什
浩浩 浩浩
3年前
【Flutter实战】层叠布局(Stack、Positioned)
4.5层叠布局Stack、Positioned层叠布局和Web中的绝对定位、Android中的Frame布局是相似的,子组件可以根据距父容器四个角的位置来确定自身的位置。绝对定位允许子组件堆叠起来(按照代码中声明的顺序)。Flutter中使用Stack和Positioned这两个组件来配合实现绝对定位。Stack允许子组件堆叠,而Pos
菜园前端 菜园前端
1年前
你需要知道的CSS基础样式知识
原文链接:定位相关相对定位css.mycontentposition:relative;绝对定位css.mycontentposition:absolute;left:0;top:0;固定定位css.mycontentposition:fixed;left
Stella981 Stella981
2年前
Python+Selenium自动化篇
本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子。0.元素定位方法主要有:id定位:find\_element\_by\_id('')name定位:find\_element\_by\_name('')class定位:find\_element\_by\_class\_name(''
Stella981 Stella981
2年前
CSS之border绘制三角形
用CSS的border可以画出高质量的三角形。我们一般会这么使用border:testborder{width:100px;height:100px;margin:100pxauto;background:fff;border:2px
Wesley13 Wesley13
2年前
CSS定位和滚动条
0805自我总结一.绝对定位position:absolute;/绝对定位:1、定位属性值:absolute2、在页面中不再占位(浮起来了),就无法继承父级的宽度(必须自己自定义宽度)3、一旦定位后,定位的布局方位top、bottom、left、right都能参与布局
Stella981 Stella981
2年前
IE7、IE8、IE9对min
问题:    IE7、IE8、IE9对minheight不识别,其他无问题解决:   box{width:100px;height:35px;}   htmlbodybox{width:auto;height:auto;width:100px;minheight:35px;} 实例:
Wesley13 Wesley13
2年前
Java Web中如何上传视频(用上传视频信息为例)
1.上传视频信息的jsp页面uploadVideo.jsp<bodybackground"image/bk\_hero.jpg"<divid"upld"style"height:300px;width:300px;marginleft:300px;margintop:100px;"<table<!
Wesley13 Wesley13
2年前
CSS内容布局
网页是由不同内容块构成的:标题、段落、链接、列表、图片、视频,等等。1.定位定位并不适合总体布局,因为它会把元素拉出页面的正常流。元素的初始定位方式为静态定位(static),即块级元素垂直堆叠。把元素设置为相对定位(relative),然后可以相对于其原始位置控制该元素的偏移量,同时又不影响其周围的元
Stella981 Stella981
2年前
Flutter Stack布局中定位的方式
前言想要记录一下Stack布局中,定位的两种方式代码//……省略无关代码……child:newColumn(children:<WidgetnewSizedBox(height:20.0),new