绝对定位

383 阅读1分钟

绝对定位

position:absolude;

以坐标系进行描述 left到左边的距离 ;right 到右边的距离 ; top 到上边的距离 ;bottom 到下边的距离

  • 作用:使元素脱离常规流
  • 特点:
    1. 脱离常规流

    2. 设置尺寸要注意:==百分比比的是==谁?-------==最近定位祖先元素==

    3. lrtb如果设置为auto 它将被打回原形

    4. lrtb如果设置为0==它将对齐到最近定位祖先元素的各边-----衍生出一个居中妙计==

    5. 绝对定位的盒子会以自己祖先元素中,离自己最近的拥有定位属性的盒子,当做基准点。这个盒子通常是相对定位的,所以这个性质也叫作"子绝父相“。。。如果没有最近定位祖先元素 会认body做爹爹

    6. z-index可以控制堆叠顺序999999见过吧

实现垂直水平居中

因为绝对定位已经脱离标准文档流不能使用margin:0 auto;方法使盒子水平居中。

只能通过先将盒子

top:50%;

left:50%;

margin-top:负的一半高度;

margin-left:负的一半宽度;

使盒子垂直水平居中

<!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>
        .parent {
            position:relative;
            width:200px;
            height:150px;
            background:blue;

        }
        .child {
            position:absolute;
            width:80px;
            height:80px;
            background:red;
            top:50%;
            left:50%;
            margin-top:-40px;
            margin-left:-40px;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

堆叠顺序z-index属性

z-index属性是一个没有单位的正整数,数值大的能压住数值小的;

<!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>
        .block {
            width:80px;
            height:80px;
            line-height:80px;
            text-align:center;
            background-color:red;
        }
        .block:nth-child(1) {
            position:absolute;
            top:20px;
            left:20px;
            background-color:blue;
            z-index:999;
        }
        .block:nth-child(2) {
            position:absolute;
            top:30px;
            left:30px;
            background-color:orange;
            z-index:9999;
        }
    </style>
</head>
<body>
    <div class="block">A</div>
    <div class="block">B</div>
    <div class="block">C</div>
</body>
</html>

绝对定位用途

绝对定位用来制作”压盖“、”避罩“效果。

绝对定位用来结合CSS精灵使用。

绝对定位可以结合JS实现动画。

利用绝对定位进行轮播图布局

<!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>
        * {
            margin:0;
            padding:0;
        }
        div {
            position:relative;
            margin:40px auto;
            width:412px;
            height:268px;
           
        }
        img {
          
            width:412px;
            height:268px;
           
        }
        .btn {
            position:absolute;
            width:40px;
            height:40px;
            border: 1px solid #000;
            top:50%;
            margin-top:-20px;
            /* 变为圆形 */
            border-radius:50%;
            text-align:center;
            line-height:40px;
            background-color:rgba(255,255,255,.5);
            cursor:pointer;
        }
        .btn:hover {
            background-color:black;
            color:white;
        }
        .rightbtn {
            right:20px;
        
        }
        .leftbtn {
            left:20px;

        }
        div ol {
            position:absolute;
            width:150px;
            height:20px;
            right:20px;
            bottom:20px;
            /* background-color:green; */
        }
        div ol li {
            float:left;
            width:20px;
            height:20px;
            background-color:rgba(255,255,255,.5);
            /* 变为圆形 */
            border-radius:50%;
            margin-right:30px;
            list-style:none;
        }
        div ol li.focus {
            background-color:gold;
        }
    </style>
</head>
<body>
    <div>

        <img src="../../images/zfzdr.jpg" alt="">

        <a class="rightbtn btn">
            <!-- 使用转义字符设置小于号和大于号 -->
            &gt;
        </a>
        <a class='leftbtn btn'>&lt;</a>

        <!-- 设置轮播图下方小圆点 -->
        <ol>
            <li></li>
            <li class="focus"></li>
            <li></li>
           
        </ol>
    </div>
 
</body>
</html>