浮动与清除浮动与解决高度坍塌 电脑版发表于:2022/3/9 9:51 方法1:最后增加一个元素,使用浮动清除,代码如下: ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .content { width: 500px; background-color: lightgreen; } .item { height: 100px; width: 100px; background-color: #abcdff; border: 1px solid red; box-sizing: border-box; float: left; } .last { /* 清除浮动 */ clear: both; } </style> </head> <body> <div class="content"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> <div class="last"></div> </div> </body> </html> ``` 方法2:伪类选择器,不用添加元素 ``` .content::after { content: ""; display: block; clear: both; } ```