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)
|