HTML解决高度塌陷的三种方法

本文详细解释了在CSS布局中常见的高度塌陷问题,并提供了三种有效的解决方案:内墙法、使用overflow:hidden属性以及双伪元素浮动法。这些方法能够帮助开发者避免因子元素浮动导致的父元素高度为零的问题。

什么是高度塌陷?

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .outer {}
        .box1 {
            width : 100px;
            height : 200px;
            background-color : #3030ff;
            float :left;
        }
        .box2 {
            width : 100px;
            height : 100px;
            background-color : red;
            float : left;
        }
    </style>
</head>
<body>
    <div class = "outer">
        <div class = "box1"></div>
        <div class = "box2"></div>
    </div>
</body>
</html>

高度塌陷就是当子盒子浮动是,父盒子自适应高度为0.

解决这种高度塌陷有一下三种方法

①内墙法

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /**
        添加一个样式,清楚浮动的影响
        */
        .outer .fix{
            clear : both;
        }
        .box1 {
            width : 100px;
            height : 200px;
            background-color : #3030ff;
            float :left;
        }
        .box2 {
            width : 100px;
            height : 100px;
            background-color : red;
            float : left;
        }
    </style>
</head>
<body>
    <div class = "outer">
        <div class = "box1"></div>
        <div class = "box2"></div>
        <div class = "fix"></div> 
    </div>
</body>
</html>

②给父元素添加overflow : hidden

 .outer{
       overflow : hidden;
 }

③一些知名网站的解决方案(双伪元素浮动)

.outer:before,.outer:after {
            content: "";
            display: table;
        }
        .outer:after {
            clear: both;
        }
        .outer {
            *zoom: 1;
        }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值