08 CSS定位

87 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1.CSS定位的基本概念

定位就是将元素定在网页中的任意位置

2.CSS定位四种类型

1)static静态定位,默认就是静态定位
2)relative相对定位,相对于原来没有定位之前的位置进行定位,还要占据位置
3)fixed固定定位,相对于浏览器窗口进行定位
4)absolute绝对定位,没有占据位置,使元素完全脱离文档流;没有定位父级,则相对整个文档发生偏移;参考最近非static定位的父级进行定位。

3.定位的基本使用方法

1)static静态定位

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    div{
      width: 100px;
      height: 100px;
      background-color: greenyellow;
      /*1.静态定位:static 默认位置,不会发生任何变化*/
      position: static;
      /*2.距离顶部50像素,用静态定位没有用*/
      top: 70px;
    }

  </style>
</head>
<body>

<div></div>

</body>
</html>

2)relative相对定位:不会改变文档流

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    *{
      /*margin: 0;*/
      /*padding: 0;*/
    }
    .div1{
      width: 100px;
      height: 100px;
      background-color: greenyellow;
      /*1.相对定位:relative 相对于之前本身的一个位置,发生的位移变化
      不会脱离文档流,即div2不会改变位置,之前用float浮动时,改变div1,div2会上移*/
      position: relative;
      /*距离顶部50px*/
      top: 50px;
      /*距离左边50px*/
      left: 50px;
    }
    .div2{
      width: 100px;
      height: 100px;
      background-color: red;
    }

  </style>
</head>
<body>

<div class="div1"></div>
<div class="div2"></div>
</body>
</html>

3)fixed固定定位:这里多来几个div才能看出效果

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>

    .div1{
      width: 100px;
      height: 100px;
      background-color: greenyellow;
      /*1.固定定位:fixed 会固定在浏览器的固定位置不动,以浏览器窗口位置做参考*/
      /*像广告,窗口上面的导航栏都是用固定定位做的*/
      /*固定定位会改变文档流*/
      position: fixed;
      top: 20px;
      left: 20px;


    }

    .div2{
      width: 100px;
      height: 100px;
      background-color: skyblue;


    }
    .div3{
      width: 100px;
      height: 100px;
      background-color: red;


    }

  </style>
</head>
<body>

<div class="box">
  <div class="div1"></div>
  <div class="div2"></div>
  <div class="div2"></div>
  <div class="div2"></div>
  <div class="div3"></div>
  <div class="div3"></div>
</div>

</body>
</html>

4)adsolute绝对定位:会改变文档流。

父级没有加上相对定位时,绝对定位是相对于浏览器的边进行移动的,如果最近父级有相对定位,移动是参照最近父级,而不是浏览器。这里记住一个口诀“父相子绝”:父级使用相对定位,子级使用绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .box{
      width: 300px;
      height: 300px;
      border: 1px solid black;
      margin-top: 20px;
      margin-left: 20px;
      position: relative;
      /*top: 20px;*/
      /*left: 20px;*/

    }
    .div1{
      width: 100px;
      height: 100px;
      background-color: greenyellow;
      /*1.绝对定位:absolute 加上绝对定位,会改变文档流,即div2会向上浮动*/
      /*父级没有加上相对定位时,绝对定位是相对于浏览器的边进行移动的 */
      /*如果最近父级有相对定位,移动50是参照最近父级,而不是浏览器*/
      /*父相子绝*/
      position: absolute;
      top: 50px;
      left: 50px;
    }

    .div2{
      width: 100px;
      height: 100px;
      background-color: skyblue;
    }

  </style>
</head>
<body>
<div class="box">
  <div class="div1"></div>
  <div class="div2"></div>
</div>

</body>
</html>

<!--1.注意:加了position定位,定位时就可以直接用top,left等等-->
<!--       没加position定位要用margin-top,margin-left进行移动-->

4.CSS层级

关键字:z-index,设置的数字越大层级越高 具体使用方法如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    ul{
      position: relative;
    }
    li{
      width: 100px;
      height: 100px;
      /*li标签清除小黑点:none*/
      list-style: none;

    }
    .li1{
      background-color: red;
      position: absolute;
      /*1.设置层级:数字越大,层级越高*/
      z-index: 1;
    }
    .li2{
          background-color: greenyellow;
          position: absolute;
          z-index: 2;
        }
    .li3{
          background-color: yellow;
          position: absolute;
        }
  </style>
</head>
<body>

<ul>
  <li class="li1"></li>
  <li class="li2"></li>
  <li class="li3"></li>
</ul>

</body>
</html>

<!--1.同时选中三行进行输入:alt键+同时点击要输入的行-->