1.1 单项目

1

首先,只有左上角1个点的情况。Flex布局默认就是首行左对齐,所以一行代码就够了。

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐

.box {
display: flex;
}

2

设置项目的对齐方式,就能实现居中对齐和右对齐。

[css3] Flex布局 —— 实例篇(骰子示例)_html_02


justify-content,调整子元素在主轴上的对齐方式

.box {
display: flex;
justify-content: center;
}

3

[css3] Flex布局 —— 实例篇(骰子示例)_html_03

.box {
display: flex;
justify-content: flex-end;
}

4

设置交叉轴对齐方式,可以垂直移动主轴。

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_04


align-items,调整子元素在侧轴上的对齐方式

.box {
display: flex;
align-items: center;
}

5

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_05

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

6

[css3] Flex布局 —— 实例篇(骰子示例)_html_06

.box {
display: flex;
justify-content: center;
align-items: flex-end;
}

1.2 双项目

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_07

.box {
display: flex;
justify-content: space-between;
}

1

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_08

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

2

[css3] Flex布局 —— 实例篇(骰子示例)_html_09

.box {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}

3

[css3] Flex布局 —— 实例篇(骰子示例)_html_10

.box {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: flex-end;
}

4

[css3] Flex布局 —— 实例篇(骰子示例)_html_11

.box {
display: flex;
}

.item:nth-child(2) {
align-self: center;
}

[css3] Flex布局 —— 实例篇(骰子示例)_html_12

.box {
display: flex;
justify-content: space-between;
}

.item:nth-child(2) {
align-self: flex-end;
}

1.3 三项目

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_13

.box {
display: flex;
}

.item:nth-child(2) {
align-self: center;
}

.item:nth-child(3) {
align-self: flex-end;
}

1.4 四项目

1

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_14

.box {
flex-wrap: wrap;
justify-content: space-between;
}

2

[css3] Flex布局 —— 实例篇(骰子示例)_html_15

.box {
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}

3

[css3] Flex布局 —— 实例篇(骰子示例)_html_16

<div class="box">
<div class="column">
<span class="item"></span>
<span class="item"></span>
</div>
<div class="column">
<span class="item"></span>
<span class="item"></span>
</div>
</div>
.box {
display: flex;
justify-content: space-between;
}

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

1.5 六项目

1

[css3] Flex布局 —— 实例篇(骰子示例)_html_17

.box {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}

2

[css3] Flex布局 —— 实例篇(骰子示例)_左对齐_18

.box {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}

1.6 九项目

[css3] Flex布局 —— 实例篇(骰子示例)_html_19

.box {
display: flex;
flex-wrap: wrap;
}