代码示例
<!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>子绝父相实现</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            text-align: center;
        }
        p{
            color: blue;
            font-size: 16px;
        }
        .box{
            position: relative;
            margin: 0 auto;
            width: 800px;
            height: 500px;
            background: pink;
        }
        .one{
            float: left;
            width: 500px;
            height: 500px;
        }
        .top{
            width: 500px;
            height: 300px;
        }
        .top .box_01{
            width: 500px;
            height: 100px;
            background: burlywood;
        }
        .top .top_bottom{
            width: 500px;
            height: 200px;
        }
        .top .top_bottom>.ke{
            float: left;
            width: 250px;
            height: 200px;
            background: lightblue;
        }
        .top .top_bottom .box_05{
            position: relative;
            background: lightgreen;
        }
        .top .top_bottom .box_05 .box_06{
            position: absolute;
            left: 0px;
            bottom: 0px;
            width: 100px;
            height: 80px;
            background: lime;

        }
        .bottom{
            width: 500px;
            height: 200px;
            background: lightcoral;
        }
        .two{
            float: left;
            width: 300px;
            height: 500px;
        }
        .two .box_02{
            width: 300px;
            height: 150px;
            background: magenta;
        }
        .two .box_03{
            width: 300px;
            height: 350px;
        }
        .thired{
            position: absolute;
            right: 0px;
            bottom: 0px;
            width: 450px;
            height: 280px;
        }
        .thired .n{
            float: left;
            width: 225px;
            height: 280px;
            background: orange;
        }
        .thired .box_09{
            background: slategray;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="one">
            <div class="top">
                <div class="box_01">
                    <p>box_01</p>
                </div>
                <div class="top_bottom">
                    <div class="box_04 ke">
                        <p>box_04</p>
                    </div>
                    <div class="box_05 ke">
                        <p>box_05</p>
                        <div class="box_06">
                            <p>box_06</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="bottom">
                <p>box_07</p>
            </div>
        </div>
        <div class="two">
            <div class="box_02">
                <p>box_02</p>
            </div>
            <div class="box_03">
                <p>box_03</p>
            </div>
        </div>
        <div class="thired">
            <div class="box_08 n">
                <p>box_08</p>
            </div>
            <div class="box_09 n">
                <p>box_09</p>
            </div>
        </div>
    </div>
</body>
</html>

代码结果示例
相对定位

对于代码分析

thired 的父元素是box
box_06 的父元素是box_05

  1. 子绝父相:父元素或祖先元素是相对定位,子元素是绝对定位
  2. 绝对定位:看父元素,也就是看最近一级父元素是否设定了定位,如果没有就往上找,如果一直都没有,那么就会以浏览器的可视窗口作为参照进行偏移
  3. 相对定位:是以自己为参照为,偏移量是看自己原来的位置作为偏移参照的
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐