码迷,mamicode.com
首页 > 其他好文 > 详细

清除浮动

时间:2019-10-19 13:06:52      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:oat   浮动   图片   一个   内容   strong   flow   info   display   

父元素没有设置高度,高度由内容撑起
<div class="father cle">
        <div class="box1">first</div>
        <div class="box2">second</div>
    </div>
  <div class="foot"></div>
 
 

father {
width: 400px;
border: 1px solid red;
}

.box1 {
width: 100px;
height: 100px;
background-color: cornflowerblue;
}

.box2 {
width: 100px;
height: 100px;
background-color: deepskyblue;
}

.foot {
width: 500px;
height: 200px;
background-color: cadetblue;
}

 

技术图片

当给子元素设置float:left时

技术图片

1.此时父元素的高度没有了,可以在父元素中添加内容(再添加一个div,给这个div设置clear:both)

<div class="father cle">
        <div class="box1">first</div>
        <div class="box2">second</div>
        <div class="box3">third</div>
    </div>
    <div class="foot"></div>
.father {
            width: 400px;
            border: 1px solid red;
        }
        
        .box1 {
            width: 100px;
            height: 100px;
            background-color: cornflowerblue;
            float: left;
        }
        
        .box2 {
            width: 100px;
            height: 100px;
            background-color: deepskyblue;
            float: left;
        }
        
        .foot {
            width: 500px;
            height: 200px;
            background-color: cadetblue;
        }
        
        .box3 {
            clear: both;
        }
技术图片

 

 2.再父元素中设置overflow:hidden

技术图片

 

 3.设置after伪类

<div class="father cle">
        <div class="box1">first</div>
        <div class="box2">second</div>
        
    </div>
    <div class="foot"></div>
 
 .cle:after {
            content: "";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }
        
        .cle {
            *zoom: 1;
        }

 

 技术图片

 

 

4.使用after和before双伪类

 .cle:after,
        .cle:before {
            content: "";
            display: table
        }
        
        .cle:after {
            clear: both;
        }
        
        .cle {
            *zoom: 1;
        }
<div class="father cle">
        <div class="box1">first</div>
        <div class="box2">second</div>
        
    </div>
    <div class="foot"></div>
技术图片

清除浮动

标签:oat   浮动   图片   一个   内容   strong   flow   info   display   

原文地址:https://www.cnblogs.com/zhanghaifeng123/p/11703261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!