# Flex 布局
背景知识: flex (opens new window), flex 布局的基本概念 (opens new window)
Flex 布局的全称为 CSS Flexible Box Layout Module,是 W3C 提出的一种新型页面布局方案,第一个版本于 2009 年推出,到现在为止,W3C 一共发布了 12 个版本,最新版本 (opens new window)于 20171019 推出,已经得到了所有主流浏览器的支持,所以请大胆的使用吧~
# 历史版本
https://www.w3.org/TR/2016/CR-css-flexbox-1-20160526/ (opens new window)
https://www.w3.org/TR/2016/CR-css-flexbox-1-20160301/ (opens new window)
https://www.w3.org/TR/2015/WD-css-flexbox-1-20150514/ (opens new window)
https://www.w3.org/TR/2014/WD-css-flexbox-1-20140925/ (opens new window)
https://www.w3.org/TR/2014/WD-css-flexbox-1-20140325/ (opens new window)
https://www.w3.org/TR/2012/CR-css3-flexbox-20120918/ (opens new window)
https://www.w3.org/TR/2012/WD-css3-flexbox-20120612/ (opens new window)
https://www.w3.org/TR/2012/WD-css3-flexbox-20120322/ (opens new window)
https://www.w3.org/TR/2011/WD-css3-flexbox-20111129/ (opens new window)
https://www.w3.org/TR/2011/WD-css3-flexbox-20110322/ (opens new window)
https://www.w3.org/TR/2009/WD-css3-flexbox-20090723/ (opens new window)
# Flexbox 原理演示
来自[xluos]的Flexbox 演示站 (opens new window)~
Flex 布局由容器flex container
和项目flex item
两部分组成,容器默认存在两根轴:水平的主轴main axis
和垂直的交叉轴cross axis
,项目默认以主轴排列。
Flex 属性包括容器属性和项目属性两部分,容器上可设置:flex-direction
、flex-wrap
、flex-grow
、justify-content
、align-items
、align-content
6 个属性,项目上同样可设置 6 个属性,分别为:order
、flex-grow
、flex-shrink
、flex-basis
、flex
、align-self
。示例如下:
# 容器属性
# flex-direction 属性
作用: 决定主轴的方向。
flex-direction: row | row-reverse | column | column-reverse;
- row:默认值,主轴为水平方向,表示从左向右排列
- row-reverse:主轴为水平方向,从右向左排列
- column:主轴为垂直方向,从上向下排列
- column-reverse:主轴为垂直方向,从下向上排列
# flex-wrap 属性
作用: 决定项目在一条轴线排不下时如何换行。
flex-wrap: nowrap | wrap | wrap-reverse;
- nowrap:默认值,不换行
- wrap:换行,第一行在上方
- row-reverse:换行,第一行在下方
# flex-glow 属性
作用: flex-direction
属性和flex-wrap
属性的简写形式,默认值为 row nowrap。
flex-wrap: <flex-direction> || <flex-wrap>;
- row nowrap:默认值,主轴为水平方向,不换行
<flex-direction>
:同flex-direction<flex-wrap>
:同flex-wrap
# justify-content 属性
作用: 定义项目在主轴上的对齐方式。
justify-content: flex-start | flex-end | center | space-between | space-round |
space-evenly;
2
- flex-start:默认值,左对齐
- flex-end:右对齐
- center:居中
- space-evenly:每个项目之间及两端的间隔都相等
- space-around:每个项目两侧间隔相等
- space-between:两端对齐,项目之间间隔相等
# align-items 属性
作用: 定义项目在交叉轴(默认方向从上到下)上的对齐方式。
align-items: flex-start | flex-end | center | baseline | stretch;
- flex-start:交叉轴的起点对齐
- flex-end:交叉轴的终点对齐
- cente:交叉轴的中心对齐
- baseline:项目第一行文字的基线对齐
- stretch:默认值,项目未设置固定高度时,将占满整个容器
# align-content 属性
作用: 定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
align-content: flex-start | flex-end | center | space-between | space-around |
stretch;
2
- flex-start:交叉轴的起点对齐
- flex-end:交叉轴的终点对齐
- center:交叉轴的中心对齐
- space-between:与交叉轴两端对齐,轴线之间的间隔平均分布等
- space-around:每根轴线两侧的间隔都相
- stretch:默认值,轴线占满整个交叉轴
# order 属性
作用: 定义项目的排列顺序。
order: <number>;
<number>
:值为整数,数值越小,排列越靠前,默认为 0
# flex-grow 属性
作用: 定义项目的伸缩比例,按照该比例给项目分配空间。
flex-grow: <number>;
<number>
:值为整数,数值越大,项目占据空间越大,默认为 0
# flex-shrink 属性
作用: 定义项目的收缩比例,按照该比例给项目分配空间。
flex-shrink: <number>;
<number>
:值为整数,数值越大,项目占据空间越小,默认为 0
# flex-basis 属性
作用: 定义在分配多余空间之前,项目占据的主轴空间。浏览器根据这个属性,计算主轴是否有多余空间。
flex-basis: <length> | auto;
<length>
:默认为 auto,即项目的原始尺寸;也可设置和 width 或 height 属性一样的值(比如 329px),则项目将占据固定空间。
# flex 属性
作用: 是flex-grow
,flex-shrink
和flex-basis
的简写,后两个属性可选。
flex: none | [ < "flex-grow" > < "flex-shrink" >? || < "flex-basis" > ];
0 1 auto
:默认值,不伸缩,如果容器空间不足则等比例收缩1 1 auto
:对应关键字auto
,如果容器空间多余,则等比例分配多余空间;如果容器空间不足则等比例收缩0 0 auto
:对应关键字none
,按项目原始大小分配空间
# align-self 属性
作用: 定义单个项目的对齐方式,可覆盖 align-items 属性。
align-self: auto | flex-start | flex-end | center | baseline | stretch;
- auto:默认值,继承父元素的
align-items
属性,如果没有父元素,则等同于 stretch- flex-start:交叉轴的起点对齐
- flex-end:交叉轴的终点对齐
- center:交叉轴的中心对齐
- baseline:项目第一行文字的基线对齐
- stretch:未设置固定高度是,将占满整个容器
← 双飞翼布局 全背景下等宽内容居中 →