Flexbox 佈局 (Flexible Box) 模塊 (目前是 W3C 的一個最後徵集工做草案(Last Call Working Draft))旨在提供一種更有效的方式,在容器的項之間處理佈局、對齊和空間分配,即便它們的大小未知而且/或是動態變化的(所以叫作「flex」)。 css
Flex 佈局背後的主要思想是讓容器可以改變其項的寬度/高度(和順序),以最好地填充可用空間(主要是爲了適應各類顯示設備和屏幕大小)。一個 flex 容器擴大其項來填充可用的空閒空間,或者縮小它們以防止溢出。html
最重要的是,flexbox 佈局與常規佈局(基於垂直的塊(block)、基於水平的內聯(inline))截然相反,flexbox 佈局是方向無關的。雖然常規佈局工做很適合頁面,但它們缺少靈活性(沒有雙關語)來支持大型或複雜的應用程序(特別是當涉及到方向變化、大小調整、拉伸、收縮等)時。
css3
注意:Flexbox 佈局最適合應用程序的組件和小型佈局,而網格佈局則適合更大規模的佈局。git
基本上,彈性項(flex items)將沿着主軸(main axis)(從 main-start 到 main-end)或側軸(cross axis)(從 cross-start 到 cross-end)佈局。github
Properties for the Parentweb (flex container)瀏覽器 |
Properties for the Childrenapp (flex items)ide |
# display
定義一個 flex 容器;inline 或 block 取決於給定的值。它爲全部直接的子元素提供一個 flex 上下文。
.container {
display: flex; /* or inline-flex */
}
注意,CSS 列對 flex 容器沒有影響。
# flex-direction![]()
肯定主軸, 用來定義 flex 容器中的 flex 項的放置方向。Flexbox 是(除了可選的 wrap)一個單向佈局概念。能夠將 flex 項看做主要是在水平的行或垂直的列中佈局的。
.container {
flex-direction: row | row-reverse | column | column-reverse;
}
# flex-wrap![]()
默認狀況下,flex 項都將嘗試放置在一行上。你能夠根據須要使用該屬性來更改默認行爲以支持換行。
.container{
flex-wrap: nowrap | wrap | wrap-reverse;
}
# flex-flow (適用於:父 flex 容器元素)
這是一個簡化的 flex-direction 和 flex-wrap 屬性,它們一塊兒定義了 flex 容器的主和側軸。默認是 row nowrap。
flex-flow: <‘flex-direction’> || <‘flex-wrap’>
# justify-content![]()
該屬性定義了瀏覽器如何分配順着父容器主軸的彈性元素之間及其周圍的空間。它幫助分配剩餘的空閒空間當不管在一行中的全部 flex 項是固定大小的仍是彈性的可是達到它們的最大尺寸的時候。 當它們溢出行時,
該屬性也能這些項的對齊方式發揮一些控制做用。
.container {
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}
# align-items![]()
該屬性定義彈性元素沿當前行的側軸方向上如何佈局的默認行爲。能夠將其理解爲 justify-content 應用於側軸的版本(垂直於主軸)。
.container {
align-items: flex-start | flex-end | center | baseline | stretch;
}
# align-content![]()
該屬性設置當在側軸方向上有額外空間的時候彈性容器中行的對齊方式,相似於在主軸上 justify-content 屬性對個體元素的對齊方式。
注意:該屬性在彈性容器中僅有一行彈性元素的狀況下沒有效果。
.container {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
|
# order![]()
默認狀況下,flex 元素按源順序排列。同時,order 屬性能夠控制它們在彈性容器中出現的順序。
.item {
order: <integer>;
}
# flex-grow![]()
該屬性定義彈性盒子項(flex item)的拉伸因子,它定義了一個 flex 項在必要時能夠拉伸的能力。它接受一個無單位數值,做爲一個比例。它規定了 flex 項在 flex 容器內可佔據的可用空間的大小。
若是全部項的 flex-grow 屬性都設置爲 1,那麼容器中的剩餘空間將平均分配給全部的子元素。若是其中一個子元素的值爲 2,那麼其對剩餘空間的佔用將比其餘項多一倍(或者至少嘗試一下)。
.item {
flex-grow:
<number>; /* default 0 */
}
負值是無效的。
# flex-shrink
這定義了一個 flex 項在必要時收縮的能力。flex 元素僅在默認寬度之和大於容器的時候纔會發生收縮,其收縮的大小是依據 flex-shrink 的值。
.item {
flex-shrink: <number>; /* default 1 */
}
負值是無效的。
# flex-basis
這定義了在剩餘空間被分配以前,元素的默認大小。它能夠是一個長度(例如 20%,5rem 等)或一個關鍵字。auto 關鍵字的意思是「看個人 width 或 height 屬性」(這是由關鍵字 main-size 臨時完成的,直到廢棄爲止)。content 關鍵字的意思是「基於項中內容的大小調整尺寸」 ———— 這個關鍵字尚未獲得很好的支持,因此很難測試,也很難知道它的同胞屬性 max-content、min-content 和 fit-content 是什麼。
.item {
flex-basis: <length> | auto; /* default auto */
}
若是設置爲0,則不將內容周圍的額外空間考慮在內。若是設置爲 auto,額外的空間將基於它的 flex-grow 屬性值分配。 查看此圖。
# flex
這是一個簡寫屬性,能夠同時設置 flex-grow, flex-shrink 與 flex-basis。第二和第三個參數(flex-shrink 和 flex-basis)是可選的。默認爲 0 1 auto。
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}
建議使用這個簡寫屬性,而不是設置單個屬性。
# align-self![]()
這容許對單個 flex 元素覆蓋默認對齊(或者由 align-item 指定的對齊)。
屬性值請參考對 align-items 屬性的解釋。
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
注意,float,clear 和 vertical-align 對彈性元素沒有影響。
|
.parent { display: flex; height: 300px; /* Or whatever */ } .child { width: 100px; /* Or whatever */ height: 100px; /* Or whatever */ margin: auto; /* Magic! */ }
.flex-container { /* We first create a flex layout context */ display: flex; /* Then we define the flow direction and if we allow the items to wrap * Remember this is the same as: * flex-direction: row; * flex-wrap: wrap; */ flex-flow: row wrap; /* Then we define how is distributed the remaining space */ justify-content: space-around; }
/* Large */ .navigation { display: flex; flex-flow: row wrap; /* This aligns items to the end line on main-axis */ justify-content: flex-end; }
/* Medium screens */ @media all and (max-width: 800px) { .navigation { /* When on medium sized screens, we center it by evenly distributing empty space around items */ justify-content: space-around; } }
/* Small screens */ @media all and (max-width: 500px) { .navigation { /* On small screens, we are no longer using row direction but column */ flex-direction: column; } }
.wrapper { display: flex; flex-flow: row wrap; }
/* We tell all items to be 100% width */ .header, .main, .nav, .aside, .footer { flex: 1 100%; }
/* We rely on source order for mobile-first approach * in this case: * 1. header * 2. nav * 3. main * 4. aside * 5. footer */
/* Medium screens */ @media all and (min-width: 600px) { /* We tell both sidebars to share a row */ .aside { flex: 1 auto; } }
/* Large screens */ @media all and (min-width: 800px) { /* We invert order of first sidebar and main * And tell the main element to take twice as much width as the other two sidebars */ .main { flex: 2 0px; } .aside-1 { order: 1; } .main { order: 2; } .aside-2 { order: 3; } .footer { order: 4; } }
https://codepen.io/team/css-tricks/pen/jqzNZqsvg
@mixin flexbox() { display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; } @mixin flex($values) { -webkit-box-flex: $values; -moz-box-flex: $values; -webkit-flex: $values; -ms-flex: $values; flex: $values; } @mixin order($val) { -webkit-box-ordinal-group: $val; -moz-box-ordinal-group: $val; -ms-flex-order: $val; -webkit-order: $val; order: $val; } .wrapper { @include flexbox(); } .item { @include flex(1 200px); @include order(2); }
Chrome
|
Safari
|
Firefox
|
Opera
|
IE
|
Android
|
iOS
|
20- (old)
|
3.1+ (old)
|
2-21 (old)
|
12.1+ (new)
|
10 (tweener)
|
2.1+ (old)
|
3.2+ (old)
|
21+ (new)
|
6.1+ (new)
|
22+ (new)
|
11+ (new)
|
4.4+ (new)
|
7.1+ (new)
|