CSS定位(相对定位,绝对定位,固定定位,粘性定位,子绝父相)

定位 = 定位模式 + 边偏移

1.模式

   position : static (静态) | relative (相对) | absolute (绝对) | fixed (固定)

2.边偏移

   top | bottom | left | right

1.静态定位static

        默认定位方式,无定位的意思

2.相对定位relative

        相对于它原来的位置进行偏移

        特点:原来在标准流中的位置继续占有(不脱标)

        未加定位:

<style>
    div {
      width: 200px;
      height: 100px;
    }
    .one {
      background-color: pink;
    }
    .two {
      background-color: aquamarine;
    }
    .three {
      background-color: blue;
    }
</style>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>

 

   给第二个元素添加定位

.two {
      position: relative;
      left: 100px;
      top: 20px;
      background-color: aquamarine;
}

 

  可以看到,第三个蓝色元素并没有移动到上方,说明原来第二个元素的位置依然占有,且看绿色盒子的移动,top和left值都是相对于它原来在的位置。

3.绝对定位absolute

        相对于它祖先元素来说 

        特点:

  • 如果没有祖先元素或者祖先元素没有定位,则以浏览器为准(document文档)
  • 如果祖先元素有定位(相对,绝对,固定),则以最近一级的有定位的祖先元素为参考点移动位置
  • 绝对定位不再占有原先的位置(脱标)

将上述例子中第二个元素的定位改成绝对定位:

.two {
      position: absolute;
      left: 100px;
      top: 20px;
      background-color: aquamarine;
    }

 

   可以看到,蓝色方块上移,说明绝对定位不占有原来的位置,而根据绿色方块的移动,他是根据浏览器进行移动的。

如果父元素有定位:

<style>
    .big {
      position: absolute;
      width: 200px;
      height: 200px;
      background-color: cornflowerblue;
    }
    .small {
      position: absolute;
      top: 30px;
      left: 20px;
      width: 50px;
      height: 50px;
      background-color: crimson;
    }
</style>
<div class="big">
    <div class="small"></div>
</div>

 

         则相对于父元素移动

 ☆☆子绝父相的由来

        因为父级元素需要占有位置,因此是相对定位,子盒子不需要占有位置,则是绝对定位

<style>
    div {
      width: 200px;
      height: 100px;
    }
    .one {
      background-color: pink;
    }
    .two {
      position: relative;
      background-color: aquamarine;
    }
    .three {
      background-color: blue;
    }
    .small {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 20px;
      height: 20px;
      background-color: crimson;
    }
  </style>
<div class="one"></div>
  <div class="two">
    <div class="small"></div>
  </div>
<div class="three"></div>

 

    像这样,父级元素要按照正常次序摆放,保留自己的位置,因此只能用相对定位,以保证不会影响后面的元素,而子元素需要在父元素中放到一定的位置,且不需要占有原来的位置,因此可以选择绝对定位。

4.固定定位fixed

        页面滚动时元素的位置不会改变,是相对于浏览器的可视窗口

        特点:

  • 跟父元素没有任何关系,不随滚动条滚动
  • 固定定位不占有原先的位置(脱标)
.right {
      position: fixed;
      top: 100px;
      right: 10px;
      width: 50px;
      height: 100px;
      background-color: darkmagenta;
}
<div class="right"></div>

 

  可以看到,右边滚动条移动,紫色盒子距离上方横线的距离并不会改变

5.粘性定位sticky

        特点:

  • 以浏览器可视窗口为参照点
  • 占有原先的位置
  • 必须添加top|bottom|left|right中的一个才生效
<!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>
    div {
      width: 200px;
      height: 400px;
    }
    .one {
      background-color: pink;
    }
    .two {
      /* position: relative; */
      background-color: aquamarine;
    }
    .three {
      background-color: blue;
    }
    .right {
      position: sticky;
      top: 100px;
      width: 50px;
      height: 100px;
      background-color: darkmagenta;
    }
  </style>
</head>
<body>
  <div class="one"></div>
  <div class="right"></div>
  <div class="two"></div>
  <div class="three"></div>
</body>
</html>

 

        设置的top为100px,在紫色盒子的顶部距离可视页面顶部距离大于100px时,按照正常位置的顺序排列,而当滚动页面到距离小于100px时,紫色盒子会始终和顶部保持100px的距离,类似绝对定位,但是他原来的位置会保留,可以看到青色盒子并没有上移。

6.定位叠放次序 z-index

  • 数值越大,越靠上
  • 如果值相同,则按照书写顺序
  • 只有定位的盒子才有该属性
<style>
    div {
      position: absolute;
      width: 200px;
      height: 400px;
    }
    .one {
      background-color: pink;
    }
    .two {
      background-color: aquamarine;
    }
    .three {
      background-color: blue;
    }
</style>

   最开始的例子,给三个盒子都添加绝对定位,因为绝对定位不占有位置,所以三个盒子会层叠在一起,并只能看到第三个:

 现在给第一个元素添加z-index

.one {
      z-index: 1;
      background-color: pink;
    }

 可以看到第一个元素会覆盖其他两个:

 当然,若是第二个设置z-index更大,则第二个元素就会覆盖其他两个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值