在HTML搭建网页布局时,如果子元素浮动无法撑开父元素的盒子

项目场景:

在HTML搭建网页布局时,如果子元素浮动无法撑开父元素的盒子


问题描述

  <style>
        .top {
            margin: 0 auto;
            width: 1000px;
            /* height: 300px; */
            background-color: pink;
        }

        .bottom {
            height: 100px;
            background-color: green;
        }

        .left {
            float: left;
            width: 200px;
            height: 300px;
            background-color: #ccc;
        }

        .right {
            float: right;
            width: 790px;
            height: 300px;
            background-color: skyblue;
        }
    </style>
</head>
<body>
    <!-- 父子级标签, 子级浮动, 父级没有高度, 后面的标准流盒子会受影响, 显示到上面的位置 -->
    <div class="top">
        <div class="left"></div>
        <div class="right"></div>
    </div>
    <div class="bottom"></div>
</body>

原因分析:

浮动的元素不占据标准流的位置,当子元素浮动,父元素默认里面没有东西,所以无法撑开父盒子


解决方案:

1.直接给父盒子设置高度显示里面浮动的子盒子

2.额外标签法:

        在被浮动影响的父盒子后面添加一个标签clearfix,clearfix标签css内容为里面

clear:both;清除浮动影响

3.单伪元素法:

        及用伪元素把clear:both清除浮动代码添加至clearfix标签后面

4.双伪元素法:

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

        /* 真正清除浮动的标签 */
        .clearfix::after {
            /* content: '';
            display: table; */
            clear: both;
        }
清除浮动影响和塌陷问题都能调用

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值