盒子模型中父子元素的margin-bottom问题

        最近在做项目的时候碰到了一个小问题,是要实现如下图所示的一个效果:代码也很简单,一开始并没有想太多:想着是给右边的八个小方块分别设置margin-right和margin-bottom,然后再

<!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;
        }
        .father{
            width: 1226px;
            height: 614px;
            margin: 0 auto;
            background-color: white;
        }
        .son1{
            width: 234px;
            height: 614px;
            background-color: #800080;
            float: left;
        }
        .son2{
            width: 978px;
            height: 614px;
            background-color: white;
            float: right;
        }
        /* 企业当中一般都是用8个li实现方块 */
        ul{
            /* 去掉无序列表前面的小圆点 */
            list-style: none;
        }
        .son2 li{
            width: 234px;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin-right: 14px;
            margin-bottom: 14px;
        }
        /* 清除第4、8个li的右边距 */
        .son2 li:nth-child(4n)
        {
            margin-right: 0;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son1"></div>
        <div class="son2">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>
</body>
</html>

利用结构伪类选择器选中第四和第八个小方块,将其margin-right再设为0,防止出现一排只排三个的情况,但奇怪的是,第二排的四个小方块的margin-bottom没有再设为0却也达到了预期的效果,这令我匪夷所思。

        于是我便再写了一个div盒子(简称为test)换行去测试,因为如果第二排的四个子盒子的margin-bottom传给了父元素(大盒子)的话,那test和原来那个大盒子之间应该会换行的,然而

<!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;
        }
        .father{
            width: 1226px;
            height: 614px;
            margin: 0 auto;
            background-color: white;
        }
        .son1{
            width: 234px;
            height: 614px;
            background-color: #800080;
            float: left;
        }
        .son2{
            width: 978px;
            height: 614px;
            background-color: white;
            float: right;
        }
        /* 企业当中一般都是用8个li实现方块 */
        ul{
            /* 去掉无序列表前面的小圆点 */
            list-style: none;
        }
        .son2 li{
            width: 234px;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin-right: 14px;
            margin-bottom: 14px;
        }
        /* 清除第4、8个li的右边距 */
        .son2 li:nth-child(4n)
        {
            margin-right: 0;
        }
        .test{
            width: 1226px;
            height: 614px;
            margin: 0 auto;
            background-color: green
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son1"></div>
        <div class="son2">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>        
    <div class="test"></div>
</body>
</html>

结果却恰恰相反:没有换行。 

        然后我结合盒子模型的两个外边距经典问题——合并现象和塌陷现象思考到,如果我把father的height设置成auto会不会就能成功,我们来看看:

<!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;
        }
        .father{
            width: 1226px;
            height: 614px;
            margin: 0 auto;
            background-color: white;
        }
        .son1{
            width: 234px;
            height: 614px;
            background-color: #800080;
            float: left;
        }
        .son2{
            width: 978px;
            height: auto;
            background-color: white;
            float: right;
        }
        /* 企业当中一般都是用8个li实现方块 */
        ul{
            /* 去掉无序列表前面的小圆点 */
            list-style: none;
        }
        .son2 li{
            width: 234px;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin-right: 14px;
            margin-bottom: 14px;
        }
        /* 清除第4、8个li的右边距 */
        .son2 li:nth-child(4n)
        {
            margin-right: 0;
        }
        .test{
            width: 1226px;
            height: 614px;
            margin: 0 auto;
            background-color: green
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son1"></div>
        <div class="son2">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>        <div class="test"></div>
</body>
</html>

         可以看到,我们预期的间距确实是出现了,不得不说CSS真的是奇妙的东西,知识的海洋是无尽的。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值