0
点赞
收藏
分享

微信扫一扫

CSS — 相对定位、绝对定位、固定定位、粘滞定位

ZGtheGreat 2022-01-16 阅读 145

目录

定位(position)

  1.相对定位 

  2.绝对定位

  3.固定定位

  4.粘滞定位

  5.绝对定位元素的布局

        (1).水平布局

        (2).垂直布局

  6.元素的层级


定位(position)

定位是一种更加高级的布局手段,通过定位可以将元素摆到页面的任意位置,使用position属性来设置定位。

        可选值:

  • static 默认值,元素是静止的没有开启定位
  • relative 开启元素的相对定位
  • absolute 开启元素的绝对定位
  • fixed 开启元素的固定定位
  • sticky 开启元素的粘滞定位

 

  1.相对定位 

        当元素的position属性值设置为relative时则开启了元素的相对定位

        相对定位的特点:

                1.元素开启相对定位后,如果不设置偏移量,元素不会发生任何的变化

                2.相对定位是参照元素在文档流中的位置进行定位的

                3.相对定位会提高元素的层级

                4.相对定位不会使元素脱离文档流

                5.相对定位不会改变元素的性质是块元素、行内元素还是行内块元素

 举个例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: orange;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: red;
            position: relative;
            left: 200px;
            top: -200px;
        }
        .box3{
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>

效果如下:

 

 

  2.绝对定位

        当元素的position属性值设置为absolute时,开启了元素的绝对定位

        绝对定位的特点:

                1.开启绝对定位后,如果不设置偏移量,元素的位置不会发生变化

                2.开启绝对定位后,元素会从文档流中脱离

                3.绝对定位会改变元素的性质,行内元素变成块元素,块元素的宽高被内容撑开

                4.绝对定位会使元素提升一个层级

                5.绝对定位元素是相对于其包含块进行定位的

 举个例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: orange;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 0;
        }
        .box3{
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
        .box4{
            width: 400px;
            height: 400px;
            background-color: springgreen;
        }
        .box5{
            width: 300px;
            height: 300px;
            background-color: rgb(224, 71, 255);
            position: relative;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box4">
        4
        <div class="box5">
            5
            <div class="box2">2</div>
        </div>
    </div>
    <div class="box3">3</div>
</body>
</html>

效果如下:

 

  3.固定定位

        当元素的position属性值设置为fixed时,开启了元素的固定定位

        固定定位也是一种绝对定位,所以固定定位的大部分特点都和绝对定位一样

        唯一不同的是,固定定位永远参照于浏览器的视口进行定位,固定定位的元素不会随网页的滚动条滚动

举个例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
            overflow: scroll;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: orange;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: red;
            position: fixed;
            left: 0;
            top: 0;
        }
        .box3{
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
        .box4{
            width: 400px;
            height: 400px;
            background-color: springgreen;
            position: relative;
        }
        .box5{
            width: 300px;
            height: 300px;
            background-color: rgb(224, 71, 255);
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box4">
        4
        <div class="box5">
            5
            <div class="box2">2</div>
        </div>
    </div>
    <div class="box3">3</div>
</body>
</html>

 效果如下:

 

  4.粘滞定位

        当元素的position属性设置为sticky时则开启元素的粘滞定位,粘滞定位和相对定位的特点基本一致,不同的是粘滞定位可以在元素到达某个位置时将其固定

举个例子:

    .nav{
            width: 1205px;
            height: 48px;
            background-color: #E8E7E3;
            margin: 100px auto;
            position: sticky;
            top: 10px;
        }

效果如下(注意导航条的位置和滚动条的位置): 

 

  5.绝对定位元素的布局

        (1).水平布局

left + margin-left + border-left + padding-left + width + padding-right + border-right + margin-right + right = 包含块内容区的宽度

 

        当我们开启了绝对定位以后,水平方向的布局等式就需要添加 left 和 right 两个值,此时规则和之前的一样,只是多增加了两个值。

        当发生过度约束时:

                如果9个值中没有auto,则自动调整right值以使等式满足

                如果有auto,则自动调整auto的值以使等式满足

        因为left和right的值默认是auto,所以如果不知道left和right值时,如果等式不满足,则自动调整这两个值

        (2).垂直布局

            垂直方向布局的等式也要满足

top + margin-top/botton + border-top/bottom + padding-top/bottom + bottom = 包含块内容区的高度

举个例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 500px;
            height: 500px;
            background-color: tomato;
            position: relative;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            margin: auto;
            left: 0;
            right: 0;
            top: 0;
            bottom:0;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

 效果如下:

 

  6.元素的层级

         对于开启了定位的元素,可以通过z-index属性来指定元素的层级, z-index需要一个正数作为参数,值越大,元素的层级越高

               元素层级越高越优先显示

               如果元素的层级一样,则优先显示靠下的位置

               祖先元素的层级再高也不会盖住后代元素

举个例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: orange;
            position: absolute;
        .box2{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            top: 50px;
            left: 50px;
            z-index: 2;
        }
        .box3{
            width: 200px;
            height: 200px;
            background-color: skyblue;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 1;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3</div>
</body>
</html>

 效果如下:

 

举报

相关推荐

0 条评论