关于CSS盒子模型、边框、内外边距对绝对定位的影响

49 阅读1分钟

盒子模型、边框、内外边距对绝对定位的影响

有如下布局:

    <div class="father">
        father
        <div class="son"></div>
        <div class="son1"></div>
    </div>

有如下样式:

        .father {
            position: relative;
            left: 100px;
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            padding: 20px;
            border: 10px solid blueviolet;
            background-color: #fff;
        }
        .son {
            box-sizing: content-box;
            position: absolute;
            left: 10px;
            width: 100px;
            height: 100px;
            border: 10px solid pink;
            background-color: yellow;
            opacity:0.4;
        }
        .son1 {
            box-sizing: border-box;
            width: 150px;
            height: 150px;
            border: 10px solid green;
            background-color: skyblue;
        }

效果图如下: image.png

总结与认识
  1. 子盒子是以父盒子边框的内边界为起点便宜的;
  2. 子盒子的偏移会忽略父盒子的内边距的影响;
  3. 无论盒子模式是border-box,还是content-box,上述两点均成立;