CSS定位,绝对定位的布局,元素层级

7 篇文章 0 订阅

定位

在对页面进行布局时除了用到浮动以外,还会用到定位,定位是一种更加高级的布局手段,通过定位可以让元素摆放到页面的任意位置,使用position属性来设置定位。
可选值:

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

相对定位

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

相对定位的特点:

  • 元素开启了相对定位如果不设置偏移元素不会发生任何变化
  • 相对定位是参照元素在文档流的位置进行定位
  • 相对定位会提升元素的层级
  • 相对定位不会使文档脱离文档流
  • 相对定位不会改变元素的性质,块还是块,行内还是行内

偏移量(offset):当元素开启相对定位后需要通过设置偏移量来改变其位置
top 定位元素和定位位置上边的距离
bottom 定位元素和定位位置下边的距离
left 定位元素和定位位置左侧的距离
right 定位元素和定位位置左侧的距离

注意:定位元素垂直方向的位置由top和bottom两个属性来控制
通常只会使用其中之一,水平方向同理

<style>
        .box1{
            width:200px;
            height:200px;
            background-color: aquamarine;
        }
        .box2{
            width:200px;
            height:200px;
            background-color: blue;
            position:relative;
            top:200px;
            left:200px; 
        }
        .box3{
            width:200px;
            height:200px;
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3</div>
</body>

在这里插入图片描述
由上可知偏移量是相对于元素之前位置设置的,不会改变其他元素的位置。

绝对定位

当元素的position属性设置为absolute,则开启了元素的绝对定位,在定位中使用最多的是绝对定位。

绝对定位的特点:

  • 开启绝对定位后,如果不设置偏移量 元素的位置不会发生变化
  • 开启绝对定位,元素会从文档流脱离
  • 绝对定位会改变元素的性质,行内变成块,块的宽高被内容撑开
  • 绝对定位会使元素提升一个层级
  • 绝对定位元素是相对于其包含块进行定位

这里对包含块进行解释:
1.在正常情况下包含块就是当前元素最近的祖先元素
2.在开启绝对定位后,包含块就是离它最近的开启了定位的祖先元素

注意:如果所有的祖先元素都没有开启定位则根元素就是它的包含块,html(根元素,初始包含块),所以绝对定位一般是配合相对定位一起使用。

    <style>
        div {
            font-size: 60px;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: aquamarine;
        }

        .box2 {
            width: 200px;
            height: 200px;
            background-color: blue;
            position: absolute;
            left: 0;
            top: 0;
        }
        .box3 {
            width: 200px;
            height: 200px;
            background-color: yellow;
        }
        .box4{
            width:400px;
            height:400px;
            background-color:blueviolet;
            position: relative;
        }
        .box5
        {
            width:300px;
            height:300px;
            background-color:white;
            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>

在这里插入图片描述
由上图可知给box2开启绝对定位,给box4,box5开启相对定位后,box2是相对box5移动,即是相对于包含块定位。

固定定位

将元素的position属性设置为fixed则开启的固定定位,固定定位也是一种绝对定位,所以固定定位大部分特点都跟绝对定位一样, 唯一不同的是固定定位永远参考浏览器的视口进行定位,固定定位的元素不会随网页的流动条滚动。

.box2 {
            width: 200px;
            height: 200px;
            background-color: blue;  
             position: fixed;
             left:0;
             top:0;
        }

在这里插入图片描述
设置固定定位后元素不会随滚动条改变位置。

粘滞定位

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

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

当视口距离nav元素10px时元素位置将固定住,不随滚动条改变。

绝对定位的布局

开启绝对定位后的水平布局由以下9个属性共同决定

  • left
  • margin-left
  • border-left
  • padding-left
  • width
  • padding-right
  • border-right
  • margin-right
  • right

需要满足以下等式水平方向布局才可成立:
left + margin-left + border-left padding-left + width + padding-right +
boreder-right + margin-right + right = 包含块内容区宽度

当发生过度约束时,如果9个值中没有auto则自动调整right值使等式成立,如果有auto,则自动调整auto的值以使等式满足,可设置auto的值:margin width left right。其中left和right的值默认为auto,不满足时,会自动调整这两个值。

可以这样设置垂直居中

                  left:0;
                  right:0;
                  top:0;
                  bottom:0;
                  margin:auto;

同理垂直方向布局等式如下
top + margin-top/bottom + padding-top/bottom + border-top/bottom + height = 包含块的高度

元素的层级

对于开启了定位的元素,可以通过z-indix属性来指定元素的层级, z-index需要一个整数作为参数,值越大元素的层级越高,元素的层级越高越优先显示,如果元素层级一样,则优先显示靠下的,祖先元素的层级再高也不会盖住后代元素

    <style>
        div{
            font-size: 60px;
        }
        .box1{
            width:200px;
            height:200px;
            background-color: aquamarine;
            position: absolute;
            z-index:1;
        }
        .box2{
            width:200px;
            height:200px;
            background-color: rgba(255,0 , 0, .5);
            position: absolute;
            top:50px;
            left: 50px;
            z-index:2;
        }
        .box3{
            width:200px;
            height:200px;
            background-color: yellow;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index:3;
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color:slateblue;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3
        <div class="box4">4</div>
    </div>
</body>

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值