方法一:給float標籤的父級標籤添加overflow: hidden/auto屬性 例如:css
<!DOCTYPE html> <html> <head> <title>DIVCSS5實例 DIV與DIV覆蓋</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> .boxa{overflow: hidden;} .boxa,.boxb{ margin:0 auto; width:400px;} .boxa-l{ float:left; width:280px; height:80px; border:1px solid #F00} .boxa-r{ float:right; width:100px; height:80px; border:1px solid #F00} .boxb{ border:1px solid #000; height:40px; background:#999} </style> </head> <body> <div class="boxa"><!--方法一:給float標籤的父級標籤添加overflow: hidden屬性--> <div class="boxa-l">內容左</div> <div class="boxa-r">內容右</div> </div> <div class="boxb">boxb盒子裏的內容</div> </body> </html>
方法二:在float標籤的父級標籤閉合以前添加標籤,使用clear:both清楚浮動html
<!DOCTYPE html> <html> <head> <title>DIVCSS5實例 DIV與DIV覆蓋</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> .boxa,.boxb{ margin:0 auto; width:400px;} .boxa-l{ float:left; width:280px; height:80px; border:1px solid #F00} .boxa-r{ float:right; width:100px; height:80px; border:1px solid #F00} .boxb{ border:1px solid #000; height:40px; background:#999} .clear{clear: both;} </style> </head> <body> <div class="boxa"> <div class="boxa-l">內容左</div> <div class="boxa-r">內容右</div> <div class="clear"></div><!--方法二:在float標籤的父級標籤閉合以前添加標籤,使用clear:both清楚浮動--> </div> <div class="boxb">boxb盒子裏的內容</div> </body> </html>
方法三:藉助給父級元素添加after僞類框架
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="Chomo"/> <link rel="start" href="http://www.14px.com" title="Home"/> <title>利用box-sizing實現div仿框架</title> <style type="text/css"> *{margin: 0;padding: 0;} .parent{width: 300px; border: 1px solid;} .son{width: 20px; height: 20px; background: #EE4063; float: left;} span{float: right;} .clear:after{ content: "."; /*添加句號*/ height: 0; /*讓高度爲0,不然會撐大父級框*/ visibility: hidden; /*讓句號只站位不顯示*/ display: block; /*必定要塊級框顯示,不然撐不開*/ clear: both;/*清楚浮動*/ } </style> </head> <body> <div class="parent clear"> <div class="son"></div> <span>操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人操盤達人</span> </div> </body> </html>