css 清楚浮动三种方法

我们可以看到这样一个布局:

<style>
    .left{
        width: 200px;
        height: 200px;
        background-color: #00ee00;
        float: left;
    }
    .right{
        width: 100px;
        height: 200px;
        background-color: #0000FF;
        float: left;
    }
    .footer{
        width: 300px;
        height: 50px;
        background-color: #0f0f0f;
    }
</style>
<div class="content">
    <div class="left"></div>
    <div class="right"></div>
</div>
<div class="footer"></div>

我们预期效果:                            结果:

                                            

 

原因:因为父盒子没有给高,然后content内的子元素又是左浮动,脱离标准流,然后下面的footer就会跑上去,这是因为浮动问题产生的,如何解决:

方法1:使用clear:both

  

<style>
    .left{
        width: 200px;
        height: 200px;
        background-color: #00ee00;
        float: left;
    }
    .right{
        width: 100px;
        height: 200px;
        background-color: #0000FF;
        float: left;
    }
    .footer{
        width: 300px;
        height: 50px;
        background-color: #0f0f0f;
    }
    .clearfix{
        clear: both;
    }
</style>
<div class="content">
    <div class="left"></div>
    <div class="right"></div>
    <div class="clearfix"></div>
</div>
<div class="footer"></div>

方法二:使用overflow:hidden;

<style>
    .content{
        overflow: hidden;
    }
    .left{
        width: 200px;
        height: 200px;
        background-color: #00ee00;
        float: left;
    }
    .right{
        width: 100px;
        height: 200px;
        background-color: #0000FF;
        float: left;
    }
    .footer{
        width: 300px;
        height: 50px;
        background-color: #0f0f0f;
    }
</style>
<div class="content">
    <div class="left"></div>
    <div class="right"></div>
</div>
<div class="footer"></div>

第三种(推荐):clearfix伪类

<style>
    .left{
        width: 200px;
        height: 200px;
        background-color: #00ee00;
        float: left;
    }
    .right{
        width: 100px;
        height: 200px;
        background-color: #0000FF;
        float: left;
    }
    .footer{
        width: 300px;
        height: 50px;
        background-color: #0f0f0f;
    }
    .clearfix:after{
        content: "";
        display: block;
        clear: both;
        height: 0;
        line-height: 0;
        visibility: hidden;
    }
    .clearfix{
        zoom: 1;//兼容ie浏览器
    }
</style>
<div class="content clearfix">
    <div class="left"></div>
    <div class="right"></div>
</div>
<div class="footer"></div>

 

  

 

转载于:https://www.cnblogs.com/alex-xxc/p/10019443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值