Flex 布局示例

感谢阮一峰老师的教程http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
本示例在网友JailBreak所做的Demo的基础上将教程上所有的布局都简单的实现了一遍
并保存在github上面https://github.com/brushbird/flex-demo

容器的属性

1、flex-direction属性

flex-direction属性决定主轴的方向(即项目的排列方向)。

.box { flex-direction: row | row-reverse | column | column-reverse; }

row row-reverse column column-reverse
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4

2、flex-wrap属性

默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

.box{ flex-wrap: nowrap | wrap | wrap-reverse; }

nowrap wrap wrap-reverse

nowrap(默认):不换行。

1
2
3
4
5
6
7

wrap:换行,第一行在上方。

1
2
3
4
5
6
7

wrap-reverse:换行,第一行在下方。

1
2
3
4
5
6
7

3、flex-flow

flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。

.box { flex-flow: <flex-direction> || <flex-wrap>; }

1
2
3
4
5
6
7

4、justify-content属性

justify-content属性定义了项目在主轴上的对齐方式。

.box { justify-content: flex-start | flex-end | center | space-between | space-around; }

flex-start flex-end center space-between space-around

flex-start(默认值):左对齐

1
2
3
4

flex-end:右对齐

1
2
3
4

center: 居中

1
2
3
4

space-between:两端对齐,项目之间的间隔都相等。

1
2
3
4

space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

1
2
3
4

5、align-items属性

align-items属性定义项目在交叉轴上如何对齐。

.box { align-items: flex-start | flex-end | center | baseline | stretch; }

flex-start flex-end center baseline stretch

flex-start:交叉轴的起点对齐。

1
2
3
4

flex-end:交叉轴的终点对齐。

1
2
3
4

center:交叉轴的中点对齐。

1
2
3
4

baseline: 项目的第一行文字的基线对齐。

1
2
3
4

stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

1
2
3
4

6、align-content属性

align-content属性定义了多根轴线(多行)的对齐方式。如果项目只有一根轴线,该属性不起作用。

.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }

flex-start flex-end center space-between space-around stretch

flex-start:交叉轴的起点对齐。

1
2
3
4
5
6
7
8
9

flex-end:与交叉轴的终点对齐。

1
2
3
4
5
6
7
8
9

center:与交叉轴的中点对齐。

1
2
3
4
5
6
7
8
9

space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。

1
2
3
4
5
6
7
8
9

space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。

1
2
3
4
5
6
7
8
9

stretch(默认值):轴线占满整个交叉轴。

1
2
3
4
5
6
7
8
9

项目的属性

1、order属性

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

.item { order: <integer>; }

1
2
3
4
(order:-1)

2、flex-grow属性

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

.item { flex-grow: <number>; /* default 0 */ }

1
flex-grow: 1
2
flex-grow: 2
3
flex-grow: 1

3、flex-shrink

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效。

.item { flex-shrink: <number>; /* default 1 */ }

1
flex-shrink: 0
2
3

4、flex-basis属性

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

.item { flex-basis: <length>; | auto; /* default auto */ }

1
2
3

5、flex属性

flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] }

6、align-self属性

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

.item { align-self: auto | flex-start | flex-end | center | baseline | stretch; }

1
2
3
flex-end
4

进阶实例

一、骰子的布局

一个点 两个点 三个点 四个点 五个点 六个点

<div class="first-face">
  <span class="pip"></span>
</div>

.first-face {
  display: flex;
  justify-content: center;
  align-items: center;
}

<div class="second-face">
  <span class="pip"></span>
  <span class="pip"></span>
</div>

.second-face {
  display: flex;
  justify-content: space-between;
}
.second-face .pip:nth-of-type(2) {
  align-self: flex-end;
}

<div class="third-face">
  <span class="pip"></span>
  <span class="pip"></span>
  <span class="pip"></span>
</div>

.third-face {
  display: flex;
  justify-content: space-between;
}
.third-face .pip:nth-of-type(2) {
  align-self: center;
}
.third-face .pip:nth-of-type(3) {
align-self: flex-end;
}

<div class="fourth-face">
  <div class="column">
    <span class="pip"></span>
    <span class="pip"></span>
  </div>
  <div class="column">
    <span class="pip"></span>
    <span class="pip"></span>
  </div>
</div>

.fourth-face{
  display: flex;
  justify-content: space-between;
}
.fourth-face .column{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

<div class="fifth-face">
  <div class="column">
    <span class="pip"></span>
    <span class="pip"></span>
  </div>
  <div class="column">
    <span class="pip"></span>
  </div>
  <div class="column">
    <span class="pip"></span>
    <span class="pip"></span>
  </div>
</div>

.fifth-face {
  display: flex;
  justify-content: space-between;
}
.fifth-face .column {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.fifth-face .column:nth-of-type(2) {
  justify-content: center;
}

<div class="sixth-face">
  <div class="column">
    <span class="pip"></span>
    <span class="pip"></span>
    <span class="pip"></span>
  </div>
  <div class="column">
    <span class="pip"></span>
    <span class="pip"></span>
    <span class="pip"></span>
  </div> </div>

.sixth-face{
  display: flex;
  justify-content: space-between;
}
.sixth-face .column{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

二、网格布局

2.1 基本网格布局

1/2
1/2
1/3
1/3
1/3
1/4
1/4
1/4
1/4

2.2 百分比布局

1/2
auto
auto
auto
1/3
1/4
auto
1/3

三、圣杯布局

圣杯布局(Holy Grail Layout)指的是一种最常见的网站布局。页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer)。其中躯干又水平分成三栏,从左到右为:导航、主栏、副栏。

四、流式布局

每行的项目数固定,会自动分行。