簡介
布局的傳統(tǒng)解決方案,基于盒狀模型,依賴 display屬性 + position屬性 + float屬性。它對于那些特殊布局非常不方便,比如,垂直居中就不容易實(shí)現(xiàn)。2009年,W3C提出了一種新的方案—-Flex布局,可以簡便、完整、響應(yīng)式地實(shí)現(xiàn)各種頁面布局。
Flex是Flexible Box的縮寫,意為”彈性布局”,用來為盒狀模型提供最大的靈活性。
任何一個(gè)容器都可以指定為Flex布局。
.box{
display: flex;
}
行內(nèi)元素也可以使用Flex布局。
.box{
display: inline-flex;
}
Webkit內(nèi)核的瀏覽器,必須加上-webkit前綴。
.box{
display: -webkit-flex; /* Safari */
display: flex;
}
注意,設(shè)為Flex布局以后,子元素的float、clear和vertical-align屬性將失效。
- 雖然 Flexbox 非常適合縮放,對齊和重新排序元素,但以下情況應(yīng)該盡量避免使用 Flexbox 布局:
- 整體頁面布局
- 完全支持舊瀏覽器的網(wǎng)站
舊版瀏覽器,如IE 11或更低版本,不支持或僅部分支持 Flexbox 。如果你想安全的使用頁面正常呈現(xiàn),你應(yīng)該退回到其他的 CSS 布局方式,比如結(jié)合float 的 display: inline-block 或者 display: table等。但是,如果您只針對現(xiàn)代瀏覽器,那么 Flexbox 絕對值得一試。
基本概念
采用Flex布局的元素,稱為Flex容器(flex container),簡稱”容器”。它的所有子元素自動(dòng)成為容器成員,稱為Flex項(xiàng)目(flex item),簡稱”項(xiàng)目”。

容器默認(rèn)存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點(diǎn))叫做main start,結(jié)束位置叫做main end;交叉軸的開始位置叫做cross start,結(jié)束位置叫做cross end。
項(xiàng)目默認(rèn)沿主軸排列。單個(gè)項(xiàng)目占據(jù)的主軸空間叫做main size,占據(jù)的交叉軸空間叫做cross size。
屬性
- flex-direction
項(xiàng)目排列方向,默認(rèn)橫向
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
}
/* 以下為輔助樣式 */
.flex-container {
background-color: #F0f0f0;
}
.flex-container .flex-item {
padding: 20px;
background-color: #B1FF84;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
</div>
</body>
</html>
flex-container添加:
flex-direction: column;
會(huì)顯示為縱向排列
也可以通過設(shè)置 flex-direction: column-reverse 或 flex-direction: row-reverse 來使 flex 項(xiàng)以相反的順序排列
注意:
正常排序和反向排序是對項(xiàng)目在容器里的代碼順序,不是按里面值排序,比如上例中第一個(gè)項(xiàng)目的內(nèi)容設(shè)置為2,第二個(gè)設(shè)置為1,反向后會(huì)變成1,2
容器寬默認(rèn)充滿父元素,高由里面的項(xiàng)目高決定
沒有定義寬高的情況下,橫向排列時(shí),項(xiàng)目的寬默認(rèn)由里面的內(nèi)容決定,高充滿容器
縱向排列時(shí),寬充滿容器,高由內(nèi)容決定
- justify-content
justify-content屬性定義了項(xiàng)目在主軸上的對齊方式。
.flex-container {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
}
效果:

row-reverse會(huì)讓容器里的項(xiàng)目按內(nèi)容反向排列,并且沿主軸右對齊,加上flex-end,又會(huì)使item左對齊
justify-content還可以設(shè)置為以下值:
flex-start | flex-end | center | space-between | space-around | space-evenly;
space-between:兩端對齊,項(xiàng)目之間的間隔都相等
space-around:每個(gè)項(xiàng)目兩側(cè)的間隔相等。所以,項(xiàng)目之間的間隔比項(xiàng)目與邊框的間隔大一倍。
space-evenly : flex 容器起始邊緣和第一個(gè) flex 項(xiàng)之間的間距和每個(gè)相鄰 flex 項(xiàng)之間的間距是相等。

設(shè)置了justify-content后再還可以單獨(dú)設(shè)置item的margin:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
/* 以下為輔助樣式 */
.flex-container {
background-color: #F0f0f0;
}
.flex-container .flex-item {
padding: 20px;
background-color: #B1FF84;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item" style="margin-right: 20px;margin-left: 400px;">3</div>
</div>
</body>
</html>

-
align-items
align-items屬性定義項(xiàng)目在交叉軸上如何對齊。
align-items: flex-start | flex-end | center | baseline | stretch;
stretch:如果項(xiàng)目未設(shè)置高度或設(shè)為auto,交叉軸對齊方式默認(rèn)值為stretch,將占滿整個(gè)容器的高度
flex-start:如果項(xiàng)目設(shè)置了高度,交叉軸對齊方式默認(rèn)值為 flex-start
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
align-items: center
}
.flex-container {
height: 60px;
background-color: #F0f0f0;
}
.flex-container .flex-item {
background-color: #B1FF84;
height: 30px;
width: 30px;
text-align: center;
line-height: 30px;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
</body>
</html>
可以在某個(gè)特定的 flex 項(xiàng)上使用 align-self CSS 屬性,來使該特定的 flex 項(xiàng)與容器中的其他 flex 項(xiàng)進(jìn)行對齊。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
align-items: center
}
.flex-bottom {
align-self: flex-end
}
.flex-container {
height: 60px;
background-color: #F0f0f0;
}
.flex-container .flex-item {
background-color: #B1FF84;
height: 30px;
width: 30px;
text-align: center;
line-height: 30px;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item flex-bottom">3</div>
</div>
</body>
</html>
- flex-wrap
flex 項(xiàng)不允許多行/列排列,如果 flex 容器尺寸對于所有 flex 項(xiàng)來說不夠大,那么flex 項(xiàng)將被調(diào)整大小以適應(yīng)單行或列排列。
通過添加 flex-wrap: wrap ,可以將溢出容器的 flex 項(xiàng)將被排列到另一行/列中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
flex-wrap: wrap;
width: 100px;
height: 60px;
background-color: #F0f0f0;
}
.flex-container .flex-item {
background-color: #B1FF84;
height: 30px;
width: 30px;
text-align: center;
line-height: 30px;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
<div class="flex-item">5</div>
</div>
</body>
</html>

- align-content
多行/列排列的 flex 項(xiàng)在交叉軸上的對齊方式
默認(rèn)情況下,當(dāng) flex 容器的交叉軸(cross axis)上存在多余空間時(shí),您可以在 flex 容器上設(shè)置 align-content,以控制 flex 項(xiàng)在交叉軸(cross axis)上的對齊方式??赡艿闹凳?flex-start,flex-end,center,space-between,space-around ,space-evenly 和 stretch(默認(rèn))。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
flex-wrap: wrap;
align-content: space-evenly;
}
.flex-container {
width: 100px;
height: 300px;
background-color: #F0f0f0;
}
.flex-container .flex-item {
background-color: #B1FF84;
height: 30px;
width: 30px;
text-align: center;
line-height: 30px;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item ">4</div>
<div class="flex-item">5</div>
<div class="flex-item">6</div>
<div class="flex-item">7</div>
</div>
</body>
</html>

- flex-grow
任何情況下,item都會(huì)被container包裹,不會(huì)超過,如果container空間不夠,item會(huì)自動(dòng)壓縮
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
width: 50px;
}
/* 以下為輔助樣式 */
.flex-container {
background-color: #F0f0f0;
}
.flex-container .flex-item {
width: 50px;
text-align: center;
background-color: #B1FF84;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
</div>
</body>
</html>
上例中item的寬度會(huì)縮小到25px
flex-grow 只有在 flex 容器中有剩余空間時(shí)才會(huì)生效。flex 項(xiàng)的 flex-grow 屬性指定該 flex 項(xiàng)相對于其他 flex 項(xiàng)將拉伸多少,以填充 flex 容器。默認(rèn)值為1。當(dāng)設(shè)置為 0 時(shí),該 flex 項(xiàng)將不會(huì)被拉伸去填補(bǔ)剩余空間。在這個(gè)例子中,兩個(gè)項(xiàng)的比例是 1:2,意思是在被拉伸時(shí),第一個(gè) flex 項(xiàng)將占用剩余空間的 1/3,而第二個(gè) flex 項(xiàng)將占據(jù)剩余空間的2/3。(如果item不定義flex-grow,也不定義寬度,則item寬度由內(nèi)容決定)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.flex-container {
display: flex;
width: 180px;
padding: 10px;
background-color: #F0f0f0;
}
.flex-item1 {
flex-grow: 0;
}
.flex-item2 {
flex-grow: 1;
}
.flex-item3 {
flex-grow: 2;
}
.flex-container .flex-item {
padding: 20px 0;
text-align: center;
width: 30px;
background-color: #B1FF84;
}
.flex-container .flex-item:first-child {
background-color: #F5DE25;
}
.flex-container .flex-item:last-child {
background-color: #90D9F7;
}
</style>
<body>
<div class="flex-container">
<div class="flex-item flex-item1">1</div>
<div class="flex-item flex-item2">2</div>
<div class="flex-item flex-item3">3</div>
</div>
</div>
</body>
</html>
上例中,item的寬度即使不定義,item2和item3也會(huì)拉升,item1寬度由內(nèi)容決定
- flex-shrink:收縮:
flex-shrink 只有在 flex 容器空間不足時(shí)才會(huì)生效。它指定 flex 項(xiàng)相對于其他 flex 項(xiàng)將縮小多少,以使 flex 項(xiàng)不會(huì)溢出 flex 容器。 默認(rèn)值為 1。當(dāng)設(shè)置為0時(shí),該 flex 項(xiàng)將不會(huì)被收縮。在這個(gè)例子中,比例是1:2,意思是在收縮時(shí),第一項(xiàng)將收縮 1/3 ,而第二個(gè)項(xiàng)目將被收縮 2/3 。注: flex-shrink 和 flex-grow 正好相反
.flex-item1{flex-shrink: 0;}
.flex-item2{flex-shrink: 1;}
.flex-item3{flex-shrink: 2;}
案例
響應(yīng)式菜單
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
/*Flexbox是一個(gè)相當(dāng)優(yōu)秀的屬性,它可能會(huì)成為未來版面布局的一部分。
如果考慮到只處理移動(dòng)方面的,那么兼容性就可以忽略了。
-moz代表firefox瀏覽器私有屬性
-ms代表IE瀏覽器私有屬性
-webkit代表chrome、safari私有屬性*/
.navigation {
list-style: none;
margin: 0;
background: deepskyblue;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
/*多欄多列布局*/
-webkit-flex-flow: row wrap;
/*讓靈活的項(xiàng)目在必要的時(shí)候進(jìn)行拆行*/
justify-content: flex-end;
}
.navigation a {
text-decoration: none;
display: block;
padding: 1em;
color: white
}
.navigation a:hover {
background: #00AEE8
}
@media all and (max-width:800px) {
.navigation {
justify-content: space-around
}
}
@media all and (max-width:600px) {
.navigation {
-webkit-flex-flow: column wrap;
padding: 0
}
}
/*寬度小于600的時(shí)候自動(dòng)換行:*/
.navigation a {
text-align: center;
padding: 10px;
border-top: 1px solid rgba(255, 255, 255, 0.3);
border-bottom: 1px solid rgba(0, 0, 0, 0.1)
}
.navigation li:last-of-type a {
background: #00AEE8;
border-bottom: 0;
}
/*指定父元素的最后一個(gè) li 元素的背景色: li:nth-child(3) li的第3個(gè)元素 li:first-child 父元素的第一個(gè)子元素li里的內(nèi)容*/
</style>
<body>
<ul class="navigation">
<li><a href="#">首頁</a></li>
<li><a href="#">簡介</a></li>
<li><a href="#">公司介紹</a></li>
<li><a href="#">聯(lián)系我們</a></li>
</ul>
</body>
</html>
flex-flow 屬性是 flex-direction 和 flex-wrap 屬性的復(fù)合屬性
默認(rèn)情況下,每個(gè)flex項(xiàng)目的寬高由內(nèi)容決定
通過媒體查詢,瀏覽器寬度小于800時(shí),讓項(xiàng)目在主軸均勻分隔,小于600時(shí),變?yōu)榭v向排列
