這篇文章給大家分享的是有關CSS如何清除浮動與BFC的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
BFC
BFC:塊級格式化上下文
BFC的布局規則
內部的Box會在垂直方向,一個接一個地放置。
Box垂直方向的距離由margin決定。屬于同一個BFC的兩個相鄰Box的margin會發生重疊。
計算BFC的高度時,浮動元素也參與計算。
BFC的區域不會與float box重疊。
每個盒子(塊盒與行盒)的margin box的左邊,與包含塊border box的左邊相接觸(對于從左往右的格式化,否則相反)。即使存在浮動也是如此。
BFC就是頁面上的一個隔離的獨立容器,容器里面的子元素不會影響到外面的元素。反之也如此。
如何創建BFC
1、 float 的值不是 none 。
2、 position 的值不是 static 或者 relative 。
3、 display 的值是 inline-block 、 table-cell 、 flex 、 table-caption 或者 inline-flex
4、 overflow 的值不是 visible
BFC的作用
1.利用BFC避免margin重疊。
2.自適應兩欄布局
3.清除浮動。
清除浮動
清除浮動主要是為了解決,父元素因為子級元素浮動引起的內部高度為0的問題。
清除浮動的方法
1. 額外標簽法
在最后一個浮動標簽后,新加一個標簽,給其設置clear:both;(不推薦)
優點:通俗易懂,方便
缺點:添加無意義標簽,語義化差
<style>
.div1 {
background: #00a2d4;
}
.left {
float: left;
width: 200px;
height: 200px;
background: #9889c1;
}
.right {
float: right;
width: 200px;
height: 200px;
background: orangered;
}
.clear {
clear: both;
}
</style>
</head>
<body>
<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
<div class="clear"></div>
</div>
<div class="div2"></div>
</body>2.父級添加overflow屬性
通過觸發BFC方式,實現清除浮動。(不推薦)
優點:代碼簡潔
缺點:內容增多的時候容易造成不會自動換行導致內容被隱藏掉,無法顯示要溢出的元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1 {
background: #00a2d4;
overflow: hidden;
}
.left {
float: left;
width: 200px;
height: 200px;
background: #9889c1;
}
.right {
float: right;
width: 200px;
height: 200px;
background: orangered;
}
</style>
</head>
<body>
<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
<div class="div2"></div>
</body>
</html>3.使用after偽元素清除浮動(推薦使用)
優點:符合閉合浮動思想,結構語義化正確。
缺點:ie6-7不支持偽元素:after,使用zoom:1觸發hasLayout。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1 {
background: #00a2d4;
}
.left {
float: left;
width: 200px;
height: 200px;
background: #9889c1;
}
.right {
float: right;
width: 200px;
height: 200px;
background: orangered;
}
.clearfix:after {
content: ""; /*內容為空*/
display: block; /*轉換為塊級元素*/
height: 0; /*高度為0*/
clear: both; /*清除浮動*/
visibility: hidden; /*隱藏盒子*/
}
.clearfix {
*zoom: 1; /*IE6\7的處理方式*/
}
</style>
</head>
<body>
<div class="div1 clearfix">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
<div class="div2"></div>
</body>
</html>4.使用before和after雙偽元素清除浮動
優點:不僅可以清除浮動,也可以解決高度塌陷的問題(給父盒子添加類名clearfix)
缺點:用zoom:1觸發hasLayout.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1 {
background: #00a2d4;
}
.left {
float: left;
width: 200px;
height: 200px;
background: #9889c1;
}
.right {
float: right;
width: 200px;
height: 200px;
background: orangered;
}
.clearfix:after, .clearfix:before {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
</style>
</head>
<body>
<div class="div1 clearfix">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
<div class="div2"></div>
</body>
</html>感謝各位的閱讀!關于“CSS如何清除浮動與BFC”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。