<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css" media="screen"> .ft { width: 100px; height: 100px; background: red; float: left; } /*一、overflow:hidden*/ .box { overflow: hidden; } /*二、 添加空元素 設置clear:both*/ .fill { clear: both; } /*3.使用after僞元素清除浮動*/ .box2:after { /*僞元素是行內元素 正常瀏覽器清除浮動方法*/ content: ""; display: block; height: 0; clear: both; visibility: hidden; } .box2 { *zoom: 1; /*ie6清除浮動的方式 *號只有IE6-IE7執行,其餘瀏覽器不執行*/ } /*四、父元素設置高度*/ .box3 { height: 100px; } /*五、父元素設置display:inline-block;*/ .box4 { display: inline-block; } </style> </head> <body> <div class="box"> <div id="box1" class="ft"></div> </div> <div class="gr">overflow:hidden</div> <div class="box1"> <div id="box2" class="ft"></div> <div class="fill"></div> </div> <div class="gr">添加空元素 設置clear:both</div> <div class="box2"> <div id="box3" class="ft"></div> </div> <div class="gr">使用after僞元素清除浮動</div> <div class="box3"> <div id="box4" class="ft"></div> </div> <div class="gr4">父元素設置高度</div> <div class="box4"> <div id="box5" class="ft"></div> </div> <div class="gr">父元素設置display:inline-block;</div> </body> </html>