我們在Css+Div佈局學習(一)—Div佈局基礎 這篇文章裏面講解了基本的佈局方法,Footer內有clear:both;屬性就一句帶過了,事後想了一下對於清理還是有些話要講清楚的。那我就開闢一篇專門講一下。
還是使用上一篇的佈局事例來說明問題更加的簡單:
這個佈局的Css複習一下:
- #Masthe ad
- {
- background-color : #CCCCCC ;
- margin-bottom : 10px ;
- }
- #Sideba r_a
- {
- float : left ;
- width : 25% ;
- margin-bottom : 10px ;
- background-color : #3399CC ;
- }
- #Sideba r_b
- {
- float : right ;
- width : 25% ;
- margin-bottom : 10px ;
- background-color : #33ff33 ;
- }
- #Conten t
- {
- margin-bottom : 10px ;
- height : 150px ;
- background-color : #ff3366 ;
- }
- #Footer
- {
- clear : both ;
- background-color : #ffff00 ;
- }
如果我們去掉#Footer 裏面的 clear : both ; 屬性,好像也沒有太大的區別。那我們再來看看改變一下佈局內Div的高度後會怎麼樣呢?
這個佈局不好看啊,因爲foot這個塊跟在Content塊的流後面,因此兩邊的浮動塊也會被它包圍。(我現在的話也應該是“圍繞”在Sidebar_b左面的,因爲沒有被清理!)
Clear屬性有4個值:left、right、both和none;none這個值沒有什麼好說的,我們一開始清理了Footer的兩邊,那清理一邊會有怎麼樣的效果呢?
有了這幾張的效果圖,應該很好理解這個屬性了。在有兩個邊欄的情況下,Footer使用Clear:both;是比較穩妥的。