css:清除浮動,css清除浮動

來源:互聯網
上載者:User

css:清除浮動,css清除浮動

我們經常用偽類來清除浮動,比如這樣

.clearfix:after {  content: "";  display: block;  clear: both;  height: 0;}.clearfix {  *zoom: 1;}//hack IE6
我們來看一下範例:
<!DOCTYPE html><html><head>    <title></title>    <style>        *{            padding: 0px ;            margin: 0px ;        }        .block{            width: 100px ;            height: 100px ;            float: left ;            margin:10px;            border: solid 1px ;        }        .clearfix:after{            content: "" ;            display: block;            clear: both ;        }    </style></head><body><div class="clearfix">    <div class="block"></div>    <div class="block"></div>    </div>    <div class="block"></div>    <div class="block"></div></body></html>

本來四個格子是同行排列的,清除了浮動後就在清除浮動的地方換行了。

注意一定要是在父類添加清除浮動的clearfix類,如果不加在父類上是沒有用的。我個人的理解是這樣的,像before和after這樣的偽類其實就是在元素內部的前面或後面添加一個元素,這樣效果是一樣的

<!DOCTYPE html><html><head>    <title></title>    <style>        *{            padding: 0px ;            margin: 0px ;        }        .block{            width: 100px ;            height: 100px ;            float: left ;            margin:10px;            border: solid 1px ;        }        .clear{            clear: both;        }    </style></head><body><div>    <div class="block"></div>    <div class="block"></div>    <div class="clear"></div>    </div>    <div class="block"></div>    <div class="block"></div></body></html>

但注意下面這樣是不行的

<body>    <div class="block"></div>    <div class="block clearfix"></div>        <div class="block"></div>    <div class="block"></div></body>

還可以用overflow來清除浮動

<!DOCTYPE html><html><head>    <title></title>    <style>        *{            padding: 0px ;            margin: 0px ;        }        .block{            width: 100px ;            height: 100px ;            float: left ;            margin:10px;            border: solid 1px ;        }        .clear{            overflow: hidden;        }    </style></head><body><div class="clear">    <div class="block"></div>    <div class="block"></div>    </div>    <div class="block"></div>    <div class="block"></div></body></html>

 

清除浮動的另外一個原因就是要把浮動元素的父元素撐開,為什麼用overflow能清除浮動我查到了以下這段話:
是因為overflow除了(visible)會重新給他裡面的元素建立塊級格式化(block formatting context)floats, position absolute, inline-block, table-cell和table-caption都不是塊級樣式,所以才會用到clear來控制浮動overflow也可以清除浮動是因為當在父級元素設定overflow時候,除了visible,就是只有auto, hidden或者scroll時候,也會建立新的塊級格式給他的子項目, 從而起到清楚浮動效果具體資訊可以在這裡看雖然clear是舊的方式,但還是推薦用clear來做,有些情況會比overflow處理的要好.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.