网页前端后台技巧(CSS+HTML)Web前端之路Web

解决子元素浮动导致父元素高度塌陷的问题(清除浮动的方法)

2020-04-23  本文已影响0人  瑟闻风倾

备注:浮动会让元素脱离了文档流,使元素的显示更为灵活,但它也是一把双刃剑,同时会带来其他问题。

1. 浮动的元素会覆盖后面处于文档流中的元素

<html>
<head>
<style type="text/css">
   .box-1{
        float:left;
        width:200px;
        height:200px;
        background-color:#333;
    }
    .box-2{
        width:200px;
        height:300px;
        background-color:#ccc;
    }
</style>
</head>

<body>
  <div class="box-1"></div>
  <div class="box-2"></div>
</body>

</html>

浮动引起遮挡问题.png

解决:只要给box-2(与浮动元素同级的元素)清除浮动就行了

<html>
<head>
<style type="text/css">
   .box-1{
        float:left;
        width:200px;
        height:200px;
        background-color:#333;
    }
    .box-2{
        width:200px;
        height:300px;
        background-color:#ccc;
        clear:both;
    }
</style>
</head>

<body>
  <div class="box-1"></div>
  <div class="box-2"></div>
</body>

</html>

清除浮动.png

2. 浮动元素会导致父元素高度坍塌

(1) 问题描述

浮动的元素会覆盖后面处于文档流中的元素:当子元素A使用浮动属性时,若父元素B有同级元素C,则元素C将被子元素A覆盖。

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

子元素浮动导致父元素高度塌陷—与父级同级元素被遮挡覆盖.png

(2) 问题分析

基础:对于一个元素来说,不设固定高度时它的高度是由内容撑开的,也就是说,如果这个元素里面没有任何内容,他的高度就是0,当这个元素有内容的时候,它就有了高度,也就是内容的高度。
分析:当子元素A使用浮动属性时,子元素将脱离文档流,不再默认继承父级的宽高,父级也检测不到子级的内容,这会导致父元素的高度为0,即子元素浮动导致父元素高度塌陷

(3) 解决:解决思路主要是为父级清除浮动从而使父级div能获取到高度。

     display:block;
     content:"";
     clear:both;

即:

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   /*清除浮动代码*/
   .clearfloat:after{display:block;content:"";clear:both;}
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

解决子元素浮动导致父元素高度塌陷.png
     clear:both;

即:

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   /*清除浮动代码*/
   .clearfloat{clear:both}
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
    <div class="clearfloat"></div>
    /*<br class="clearfloat" />*/
</div>
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;height:200px;}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
</div>
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;overflow:hidden}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   /*清除浮动代码*/
   /*.clearfloat:after{display:block;content:"";clear:both;}*/
   /*.clearfloat{clear:both}*/
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
</div>
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

为父级设置overflow:hiddendisplay:inline-block这两种方法其实是根据BFC(块级格式化上下文),因为BFC会让父元素包含浮动的子元素,从而解决父元素高度坍塌问题,所以只要能触发BFC就行。

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;display:inline-block}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   /*清除浮动代码*/
   /*.clearfloat:after{display:block;content:"";clear:both;}*/
   /*.clearfloat{clear:both}*/
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
</div>
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;overflow:auto;}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   /*清除浮动代码*/
   /*.clearfloat:after{display:block;content:"";clear:both;}*/
   /*.clearfloat{clear:both}*/
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
</div>
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

<html>
<head>
<style type="text/css">
   .div1{background:#000080;border:1px solid red;display:table;width:100%}
   .div1 .left{float:left;width:20%;height:200px;background:#DDD}
   .div1 .right{float:right;width:30%;height:80px;background:#DDD}
   .div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}
   
   /*清除浮动代码*/
   /*.clearfloat:after{display:block;content:"";clear:both;}*/
   /*.clearfloat{clear:both}*/
</style>
</head>

<body>
  <div class="div1 clearfloat"> 
    <div class="left">Left浮动</div> 
    <div class="right">Right浮动</div> 
</div>
  </div>
  <div class="div2">
    div2
  </div>
</body>

</html>

备注:推荐使用前两种方案,即为父级div定义伪类after在子级结尾处添加空div标签(clear:both)这两种方式。

拓展:浮动布局多用于需要文字环绕的时候,毕竟这才是浮动真正的用武之地;水平排列可使用inline-block了,参考css之display:inline-block布局

上一篇 下一篇

猜你喜欢

热点阅读