css中的定位

css中的定位属性为:position

        其的属性值为:  static、 relative、fixed、absolute、sticky

一、static:静态定位

HTML 元素的默认值,即没有定位,遵循正常的文档流对象。

静态定位的元素不会受到 top, bottom, left, right影响。

<style>
        div{
            position: static;
            height: 100px;
            width: 100px;
            top: 1000px;
            left: 100px;
            background-color: #7B2F2F;
        }
    </style>
position:static

 二、relative:相对定位

     相对定位  relative相对于原来的位置不脱离标准文档流。在使用相对定位时,无论是否进行移动,元素仍然占据原来的空间。移动元素会导致它覆盖其它框。(不脱离标准文档流)

<style>
        #box1{
            position: relative;
            height: 100px;
            width: 100px;
            top: 200px;
            left: 100px;
            background-color: #7B2F2F;
        }
        #box2{
            
            height: 100px;
            width: 100px;
            background-color: #1C036C;
        }
    </style>
原本位置

给box1添加位置后

三、fixed:固定定位

脱离标准文档流,保留原本的位置。不管你怎么滑动,他都相对于窗口位置不变。

 <style>
        .divA{
            position: fixed;
            top: 100px;
            left: 100px;
        }
    </style>
<div class="divA">
    <img src="./images/user.png">
</div>

位置1

位置2

四、absolute:绝对定位 

        脱离标准文档流,不保留原本的位置。

 <style>
    .father {
      margin-top: 50px;
      margin-left: 50px;
      width: 500px;
      height: 500px;
      background-color: skyblue;
    }

    .son {
      position: absolute;
      top: 50px;
      left: 50px;
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>

没有定位时的位置
添加绝对定位后

 

 五、sticky:粘性定位

        sticky可以说是 静态定位static 和 固定定位fixed 的混合体。元素在跨越特定阈值前为相对定位,之后为固定定位。这个特定阈值指的是 top, right, bottom 或 left 之一,换言之,指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效。否则其行为与相对定位相同。

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            height: 3000px;
        }
        .nav{
            /* 粘性定位 */
            position: sticky;
            width: 100px;
            height: 50px;
            background-color: pink;
            margin: 10px;
            left: 1500px;
        }
    </style>

</head>
<body>
<div class="nav">我是导航栏</div>
</body>
固定到这个位置

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值