子元素浮动,父元素撑不开height=0解决方法

如代码所示,div1,div2,div3都是左浮动的。父元素box撑不开。

    <div class="box">
        <div class="div div1"></div>
        <div class="div div2"></div>
        <div class="div div3"></div>
        <!--<p style="clear: both"></p>-->
    </div>
解决方法:

1.直接设置父元素height

        .box{
            border: 1px solid #00d6b2;
            /*height: 102px;*/
           
        }

2.父元素设置overflow:hidden

        .box{
            border: 1px solid #00d6b2;
            /*overflow: hidden;*/
        }

3.父元素内末尾添加一个元素

意思就是在父元素内末尾添加一个元素 清楚浮动clear:both;
    <div class="box">
        <div class="div div1"></div>
        <div class="div div2"></div>
        <div class="div div3"></div>
        <!--<p style="clear: both"></p>-->
    </div>

4.伪类方法 父元素:after

意思就是在父元素后面加一个空格,设置这个空格clear:both;display:block;visibility:hidden;height:0;
        .box:after{
            content: " ";
            clear: both;
            display: block;
            visibility: hidden;
            height: 0;
        }
最后,完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Title</title>
    <script type="text/javascript">

    </script>
    <style type="text/css">
        .box{
            border: 1px solid #00d6b2;
            /*height: 102px;*/
            /*overflow: hidden;*/
        }
        .box:after{
            content: " ";
            clear: both;
            display: block;
            visibility: hidden;
            height: 0;
        }
        .div{
            border: 1px solid black;
            float: left;
            width: 100px;
            height: 100px;
        }
        .div1{
            background-color: red;
        }
        .div2{
            background-color: greenyellow;
        }
        .div3{
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="div div1"></div>
        <div class="div div2"></div>
        <div class="div div3"></div>
        <!--<p style="clear: both"></p>-->
    </div>
</body>
</html>





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值