<div class="outer"> <div class="div1">1</div> <div class="div2">2</div> <div class="div3">3</div> </div>
.outer{ border:1px solid #ccc; background:#fc9; color:#fff; margin:50px auto; padding:50px;} .div1{ width:80px; height:80px; background:#f00; float:left; } .div2{ width:80px; height:80px; background:blue; float:left; } .div3{ width:80px; height:80px; background:sienna; float:left; }
.outer{ border:1px solid #ccc; background:#fc9; color:#fff; margin:50px auto; padding:50px;} .div1{ width:80px; height:80px; background:#f00; /*float:left;*/ } .div2{ width:80px; height:80px; background:blue;/* float:left; */} .div3{ width:80px; height:80px; background:sienna;/* float:left;*/ }
<div class="outer"> <div class="div1">1</div> <div class="div2">2</div> <div class="div3">3</div> <div class="clear"></div> </div>
.outer{ border:1px solid #ccc; background:#fc9; color:#fff; margin:50px auto; padding:50px;} .div1{ width:80px; height:80px; background:#f00; float:left; } .div2{ width:80px; height:80px; background:blue; float:left; } .div3{ width:80px; height:80px; background:sienna; float:left; } .clear{ clear:both; height:0; line-height:0; font-size:0; }
方法二:父級div定義 overflow:auto;(主意:是父級div,也就是這裏的 div.outer)。html
<div class="outer over-flow"> <div class="div1">1</div> <div class="div2">2</div> <div class="div3">3</div> </div>
.outer{ border:1px solid #ccc; background:#fc9; color:#fff; margin:50px auto; padding:50px;} .div1{ width:80px; height:80px; background:#f00; float:left; } .div2{ width:80px; height:80px; background:blue; float:left; } .div3{ width:80px; height:80px; background:sienna; float:left; } .over-flow{ overflow:auto; zoom:1; } //zoom:1;是在處理兼容性問題
.outer { zoom:1; } //爲了兼容性,由於ie6/7不能使用僞類,因此加上此行代碼。 .outer:after { clear:both;content:'';display:block;width:0;height:0;visibility:hidden; }
利用僞元素,就能夠再也不HTML中加入標籤。瀏覽器
:after 的意思是再.outer內部的最後加入爲元素:after,
佈局
首先要顯示僞元素,因此display:block,spa
而後爲僞元素加入空內容,以便僞元素中不會有內容顯示在頁面中,因此, content:"";code
其次,爲使僞元素不影響頁面佈局,將僞元素高度設置爲0,因此, height:0,htm
最後,要清除浮動,因此,clear:both。
blog