HTML关于高度塌陷的解决方案(推荐使用第三种方式)

高度塌陷:父盒子的高度由子盒子撑开,由于子盒子浮动之后,造成父盒子的高度为0的情况。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box1 {
            height: 300px;
            width: 100px;
            background-color: #51b7ec;
            float: left;
        }

        .box2 {
            height: 200px;
            width: 100px;
            background-color: red;
            float: left;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

结果:
在这里插入图片描述
解决方案:
1、w3c提供的一种方法:内墙法

<div class="outer">
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="clearfix" style="clear: both"></div><!--没有实际价值,就是为了清除浮动-->
</div>

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*方式:w3c组织所推荐的一种方式*/
        .outer .clearfix {
            clear: both;
        }

        .box1 {
            height: 300px;
            width: 100px;
            background-color: #51b7ec;
            float: left;
        }

        .box2 {
            height: 200px;
            width: 100px;
            background-color: red;
            float: left;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="clearfix"></div><!--没有实际价值,就是为了清除浮动-->
</div>
</body>
</html>

2、给父盒子添加overflow: hidden;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*解决高度塌陷的第二种方式: overflow:hidden*/
        .outer {
            overflow: hidden;
        }

        .box1 {
            height: 300px;
            width: 100px;
            background-color: #51b7ec;
            float: left;
        }

        .box2 {
            height: 200px;
            width: 100px;
            background-color: red;
            float: left;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

3、其他优秀的处理方案(推荐使用的),双伪元素浮动:
在父盒子的class中添加clearfix,复制以下代码即可

        .clearfix:before, .clearfix:after {
            content: "";
            display: table;
        }

        .clearfix:after {
            clear: both;
        }

        .clearfix {
            *zoom: 1;
        }

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .clearfix:before, .clearfix:after {
            content: "";
            display: table;
        }

        .clearfix:after {
            clear: both;
        }

        .clearfix {
            *zoom: 1;
        }

        .box1 {
            height: 300px;
            width: 100px;
            background-color: #51b7ec;
            float: left;
        }

        .box2 {
            height: 200px;
            width: 100px;
            background-color: red;
            float: left;
        }
    </style>
</head>
<body>
<div class="outer clearfix">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值