CSS清楚浮動

來源:互聯網
上載者:User

標籤:style   io   ar   os   使用   sp   strong   div   on   

浮動是一個有意思(你也可以說它很麻煩)的CSS屬性,任何元素設定了浮動,層級就提高了,會影響它後面沒設定浮動的元素,這些倒黴的被影響者會跑到浮動層的下面去(當然IE6、IE7除外),代碼看起來是這樣:

 

 

    <div style="width: 100px; height: 100px; border: 1px solid #333; float: left;"></div>

    <div style="width: 120px; height: 140px; background: #eee;"></div>

因為設定了浮動的元素會提高層級,所以如果一個平淡無奇的父級元素(沒有設定浮動或別的提高層級的屬性)居然包住了一個浮動的傢伙,並且這個父級還沒有設定高,那它就悲催了,因為父級沒法包住它的子級浮動元素(當然IE6、IE7又除外):

 

<div style="width: 160px; border: 1px solid #333; padding: 10px;">

    <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>

</div>

那怎麼樣才能讓父級包住浮動層呢?

下面是我列舉的一些方法,如有大家還有其他的辦法,歡迎補充

1. 在浮動層下,設定空DIV,代碼如下:

 

<div style="width: 160px; border: 1px solid #333; padding: 10px;">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>        

        <div style="clear: both; height: 0; font-size: 1px;"></div>

</div>

說明:
clear: both;可以讓元素不受到浮動層影響,排到浮動層的底下,
而父級可以包住最底下這個空DIV,這樣看起來,它似乎也就包住那個浮動層了,
其實是障眼法哈~
缺點:IE6下會多出1像素來,並且設定了無語義的空DIV;

------------------------------------------------------------------------------------------------

2. 在父級設定:overflow: hidden;

 

<div style="width: 160px; border: 1px solid #333; padding: 10px; overflow: hidden;">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>

</div>

說明:overflow: hidden; 用它是為了提高父級的層級,層級提高了,自然能包住子級浮動元素了。

缺點:要是父級裡面有什麼元素居然飄到父級以外的地方,那就悲劇了,例如很多JS的提示層效果,

就不能用 overflow: hidden;

---------------------------------------------------------------------------------------------------

3. 在父級設定:float

 

<div style="width: 160px; border: 1px solid #333; padding: 10px; float: left;">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>

</div>

說明:父級包不住浮動層,因為層級不夠,那父級也設定float好嘍

缺點:父級如果也有父級,也要設定浮動,那用這種方法,一路浮動到body那兒才算完事兒~~呃。。此方法本身就很滑稽

-------------------------------------------------------------------------------------------------------------------------------

4. 在父級設定:display: inline-block;

 

<div style="width: 160px; border: 1px solid #333; padding: 10px; display: inline-block;">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>

</div>

說明:inline-block; 是一個不相容的屬性,但用在這裡只是為了提高父級層級,
如此一來,可以完全忽略它的不相容性哈~

缺點:設定不了元素置中:margin-left: auto; margin-right: auto;
你可以在不需要置中的元素上使用這方法哈~

----------------------------------------------------------------------------------------------------------------------------------

5. 父級如果是絕對位置:position: absolute;

 

<div style="width: 160px; border: 1px solid #333; padding: 10px; position: absolute;">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>

</div>

說明:要說層級,那絕對位置必定是老大,在絕對位置眼中,小小的浮動也居然敢稱層級??

缺點:position: absolute;會影響布局,它是相對父級的塊元素為參照物進行定位,
完全不按文檔流的方式走,呵呵,所以如果你要是在絕對位置的元素裡設定浮動,那就盡情設定吧,

不必理會任何浮動神馬的,因為在絕對位置下,浮動啥的,真的是浮雲了~

----------------------------------------------------------------------------------------------------------------------------------

6. 給浮動層下設定:<br clear="all" />

 

<div style="width: 160px; border: 1px solid #333; padding: 10px;">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>        

        <br clear="all" />

</div>

------------------------------------------------------------------------------------------------------------------------------------

7. 給父級加上這行吧:

.clear:after { content: ‘\20‘; clear: both; display: block; }

 

 

 

<style>

.clear:after { content: ‘\20‘; clear: both; display: block; }

</style>

 

<div style="width: 160px; border: 1px solid #333; padding: 10px;" class="clear">

        <div style="width: 120px; height: 140px; background: #eee; float: left;"></div>

</div>

 

 

 

說明::after 是一個很好用的屬性,有了它,可以往任何元素裡插入字串、空格啦,‘.‘之類的,
但可惜,IE6,IE7不認識:after,不過,又有什麼關係呢?
反正IE6\IE7的父級能包住浮動層,所以不必管它們嘍~
忘瞭解釋了,先給浮動層底下插入一個空格(content: ‘\20‘;),再把這個空格轉成塊元素,並且清除浮動層的影響,讓父級包住這個空格,也是障眼法哈~

CSS清楚浮動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.