CSS 解决Float后塌陷问题

当父级元素没有设定高度时候,而子集元素设定float类型时候,此时父级元素不能靠子集元素撑起来,所以就形成了塌陷; 示例分析 **1.Float之前的效果**
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>float示例</title>
    <style>
        #father{
            background: red;
        }
        #son{
            width: 50px;
            height: 50px;
            background: blue;
        }
        #second{
            background: gray;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="son">子元素</div>
    <p>这是父元素下面的一个p段落</p>
    <div id="clear"></div>
</div>
<div id="second">这是第二个div</div>
</body>
</html>

这里写图片描述

2.Float之后的效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>float示例</title>
    <style>
        #father{
            background: red;
        }
        #son{
            width: 50px;
            height: 50px;
            background: blue;
            float: left;
        }
        #second{
            background: gray;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="son">子元素</div>
    <p>这是父元素下面的一个p段落</p>
    <div id="clear"></div>
</div>
<div id="second">这是第二个div</div>
</body>
</html>

“`这里写图片描述

原文地址:https://www.cnblogs.com/wood2012/p/7896576.html