優雅的解決css高度塌陷問題

給父元素加上after僞類:

.box:after {
    content:''; 
    display: block; 
    clear:both; 
 }

例如:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box{
            width: 500px;
            border: 1px solid deepskyblue;
        }
        .box>div{
            width: 100px;
            float: left;
            height: 100px;
            padding: 10px;
            margin: 30px;
            background-color: black;
        }
        .box:after{
            content:'';
            display: block;
            clear:both;
        }
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章