position的五种常规取值

1.静态定位

静态定位:position取值为static,他是HTML文档元素的默认值,不受top、bottom、left、right属性影响,并且不能进行z-index分层,元素在这次的文档流中。

<style>
      div{
          background-color: gray;
          width: 400px;
          height: 300px;
          top: 400px;
          left: 400px;
      }
</style>
<body>
    <div>

    </div>
    
</body>

2.相对定位

相对定位:当position取值为relative时,该元素为相对定位,相对自身位置定位,不会脱离文档流,可以使用z-index进行分层。它可以根据文档正常流动偏移相对于它本身top、bottom、right和left的值,偏移量不会影响其他元素的位置。

<style>
      .a1{
          background-color: gray;
          width: 400px;
          height: 300px;
          top: 400px;
          left: 400px;
      }
      .a2{
        background-color: rgb(188, 231, 86);
          width: 400px;
          height: 300px;
          top: 100px;
          left: 100px;
          position: relative;
      }
</style>
<body>
    <div class="a1">

    </div>
    <div class="a2">

    </div>
</body>
3.绝对定位

绝对定位:当position取值为absolute时,是绝对定位,脱离文档流的布局,后面的元素会填补它空下来的位置,它的起始位置为最近的position值而不是static的父元素,若没有则是以HTML为准,由top、bottom、left、right确定最终位置,可以使用z-index进行分层。

<style>
      .a1{
          background-color: gray;
          width: 400px;
          height: 300px;
          top: 400px;
          left: 400px;
      }
      .a2{
        background-color: rgb(188, 231, 86);
          width: 600px;
          height: 200px;
          top: 100px;
          left: 100px;
          position:absolute;
      }
</style>
<body>
    <div class="a1">

    </div>
    <div class="a2">

    </div>
</body>

以下是图一为相对定位,图二为绝对定位情况

<style>
      .a1{
          background-color: gray;
          width: 400px;
          height: 300px;
          top: 50px;
          left: 50px;
          position: relative;
      }
      .a2{
        background-color: rgb(188, 231, 86);
          width: 600px;
          height: 200px;
          top: 100px;
          left: 100px;
          position:absolute;
      }
</style>
<body>
    <div class="a1">

    </div>
    <div class="a2">

    </div>
</body>

以下是父元素是相对定位,子元素是绝对定位的情况

<style>
      .a1{
          background-color: gray;
          width: 400px;
          height: 300px;
          top: 50px;
          left: 50px;
          position: relative;
      }
      .a2{
        background-color: rgb(188, 231, 86);
          width: 600px;
          height: 200px;
          top: 100px;
          left: 100px;
          position:absolute;
      }
</style>
<body>
    <div class="a1">
        <div class="a2">

        </div>
    </div>
4.固定定位

固定定位:当position的取值为fixed时,该元素为固定定位,脱离文档流,它的起始位置相对于视图是固定位置不随滚动条的滑动而滚动。一般像导航栏大多数会使用固定定位。

5.粘性定位

粘性定位:当position取值为sticky时该元素为粘性定位,它会脱离文档流,会在文档流中预留该元素的物理空间;它的相对偏移是相对于父标签而言,设置top等值只会在父标签中sticky。

  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值