ZhangYang's Blog

flex布局

  • Flex 是 Flexible Box 的缩写,意为”弹性布局”
1
2
3
4
5
6
7
8
9
// 任何一个容器都可以指定为 Flex 布局
.box{
display: flex;
}
// 行内元素也可以使用 Flex 布局
.box{
display: inline-flex;
}
  • 设为 Flex 布局以后,子元素的float、clear和vertical-align属性将失效

基本概念

  • 设置 Flex 布局的元素,称为 Flex 容器(flex container),简称”容器”
  • 所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称”项目”
  • 容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)
  • 主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end
  • 交叉轴的开始位置叫做cross start,结束位置叫做cross end
  • 项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size

image

容器的属性

  • flex-direction
  • flex-wrap
  • flex-flow(不常用)
  • justify-content
  • align-items
  • align-content(不常用)

flex-direction

  • flex-direction属性决定主轴的方向(即项目的排列方向)
  • .box { flex-direction: row | row-reverse | column | column-reverse; }
  • row(默认值):主轴为水平方向,起点在左端
  • row-reverse:主轴为水平方向,起点在右端
  • column:主轴为垂直方向,起点在上沿
  • column-reverse:主轴为垂直方向,起点在下沿

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #E2E2E2;
max-width: 1024px;
color: #595B66;
}
.box {
background-color: white;
margin: 0 0 55px;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
vertical-align: middle;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-1 {
flex-direction: row;
}
</style>
</head>
<body>
<div class="box box-1">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
</div>
</body>
</html>

image

flex-wrap

  • 默认情况下,项目都排在一条线(又称”轴线”)上;flex-wrap属性定义,如果一条轴线排不下,如何换行
  • .box{ flex-wrap: nowrap | wrap | wrap-reverse; }

image

  • nowrap(默认):不换行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    body {
    padding: 20px;
    margin: 0 auto;
    background-color: #ccc;
    max-width: 1024px;
    }
    .box {
    background-color: white;
    display: flex;
    }
    .box-item {
    width: 200px;
    height: 200px;
    line-height: 200px;
    margin: 5px;
    background-color: #ffd200;
    font-size: 100px;
    color: white;
    text-align: center;
    }
    .box-2 {
    flex-direction: row;
    flex-wrap: nowrap;
    }
    </style>
    </head>
    <body>
    <div class="box box-2">
    <div class="box-item">1</div>
    <div class="box-item">2</div>
    <div class="box-item">3</div>
    <div class="box-item">4</div>
    <div class="box-item">5</div>
    <div class="box-item">6</div>
    <div class="box-item">7</div>
    </div>
    </body>
    </html>

image

  • wrap:换行,第一行在上方
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-3 {
flex-direction: row;
flex-wrap: wrap;
}
</style>
</head>
<body>
<div class="box box-3">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
</div>
</body>
</html>

image

  • wrap-reverse:换行,第一行在下方
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-4 {
flex-direction: row;
flex-wrap: wrap-reverse;
}
</style>
</head>
<body>
<div class="box box-4">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
</div>
</body>
</html>

image

flex-flow

  • flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap
  • .box { flex-flow: || ; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-5 {
flex-direction: row;
flex-flow: row nowrap;
}
</style>
</head>
<body>
<div class="box box-5">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
</div>
</body>
</html>

image

justify-content

  • justify-content属性定义了项目在主轴上的对齐方式
  • .box { justify-content: flex-start | flex-end | center | space-between | space-around; }
  • flex-start(默认值):左对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-6 {
justify-content: flex-start;
}
</style>
</head>
<body>
<div class="box box-6">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
</div>
</body>
</html>

image

  • flex-end:右对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-7 {
justify-content: flex-end;
}
</style>
</head>
<body>
<div class="box box-7">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
</div>
</body>
</html>

image

  • center: 居中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-8 {
justify-content: center;
}
</style>
</head>
<body>
<div class="box box-8">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
</div>
</body>
</html>

image

  • space-between:两端对齐,项目之间的间隔都相等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-9 {
justify-content: space-between;
}
</style>
</head>
<body>
<div class="box box-9">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
</div>
</body>
</html>

image

  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-10 {
justify-content: space-around;
}
</style>
</head>
<body>
<div class="box box-10">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
</div>
</body>
</html>

image

align-items

  • align-items属性定义项目在交叉轴上如何对齐
  • .box { align-items: flex-start | flex-end | center | baseline | stretch; }
  • flex-start:左对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-11 {
align-items: flex-start;
}
.item-tall {
height: 400px;
line-height: 400px;
}
</style>
</head>
<body>
<div class="box box-11">
<div class="box-item">1</div>
<div class="box-item item-tall">2</div>
<div class="box-item">3</div>
<div class="box-item item-tall">4</div>
</div>
</body>
</html>

image

  • flex-end:交叉轴的终点对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-12 {
align-items: flex-end;
}
.item-tall {
height: 400px;
line-height: 400px;
}
</style>
</head>
<body>
<div class="box box-12">
<div class="box-item">1</div>
<div class="box-item item-tall">2</div>
<div class="box-item">3</div>
<div class="box-item item-tall">4</div>
</div>
</body>
</html>

image

  • center:交叉轴的中点对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-13 {
align-items: center;
}
.item-tall {
height: 400px;
line-height: 400px;
}
</style>
</head>
<body>
<div class="box box-13">
<div class="box-item">1</div>
<div class="box-item item-tall">2</div>
<div class="box-item">3</div>
<div class="box-item item-tall">4</div>
</div>
</body>
</html>

image

  • baseline: 项目的第一行文字的基线对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-14 {
align-items: baseline;
}
.box-14 .box-item{
font-size: 88px;
line-height: initial;
text-decoration: underline;
}
.item-tall {
height: 400px;
line-height: 400px;
}
.box-14 .item-tall{
font-size: 122px;
line-height: initial;
}
</style>
</head>
<body>
<div class="box box-14">
<div class="box-item">1</div>
<div class="box-item item-tall">2</div>
<div class="box-item">3</div>
<div class="box-item item-tall">4</div>
</div>
</body>
</html>

image

  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 200px;
height: 200px;
line-height: 200px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-15 {
align-items: stretch;
}
.box-15 .box-item {
height: auto;
}
.item-tall {
height: 400px;
line-height: 400px;
}
</style>
</head>
<body>
<div class="box box-15">
<div class="box-item">1</div>
<div class="box-item item-tall">2</div>
<div class="box-item">3</div>
<div class="box-item item-tall">4</div>
</div>
</body>
</html>

image

align-content

  • align-content属性定义了多根轴线(多行)的对齐方式。如果项目只有一根轴线,该属性不起作用
  • .box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }
  • flex-start:交叉轴的起点对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-16 {
flex-wrap: wrap;
align-content: flex-start;
}
.box-tall {
height: 500px;
}
</style>
</head>
<body>
<div class="box box-tall box-16">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
<div class="box-item">8</div>
<div class="box-item">9</div>
</div>
</body>
</html>

image

  • flex-end:与交叉轴的终点对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-17 {
flex-wrap: wrap;
align-content: flex-end;
}
.box-tall {
height: 500px;
}
</style>
</head>
<body>
<div class="box box-tall box-17">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
<div class="box-item">8</div>
<div class="box-item">9</div>
</div>
</body>
</html>

image

  • center:与交叉轴的中点对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-18 {
flex-wrap: wrap;
align-content: center;
}
.box-tall {
height: 500px;
}
</style>
</head>
<body>
<div class="box box-tall box-18">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
<div class="box-item">8</div>
<div class="box-item">9</div>
</div>
</body>
</html>

image

  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-19 {
flex-wrap: wrap;
align-content: space-between;
}
.box-tall {
height: 500px;
}
</style>
</head>
<body>
<div class="box box-tall box-19">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
<div class="box-item">8</div>
<div class="box-item">9</div>
</div>
</body>
</html>

image

  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-20 {
flex-wrap: wrap;
align-content: space-around;
}
.box-tall {
height: 500px;
}
</style>
</head>
<body>
<div class="box box-tall box-20">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
<div class="box-item">8</div>
<div class="box-item">9</div>
</div>
</body>
</html>

image

  • stretch(默认值):轴线占满整个交叉轴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-21 {
flex-wrap: wrap;
align-content: stretch;
}
.box-21 .box-item {
height: auto;
}
.box-tall {
height: 500px;
}
</style>
</head>
<body>
<div class="box box-tall box-21">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item">4</div>
<div class="box-item">5</div>
<div class="box-item">6</div>
<div class="box-item">7</div>
<div class="box-item">8</div>
<div class="box-item">9</div>
</div>
</body>
</html>

image

项目的属性

  • order(代替双飞翼)
  • flex-grow
  • flex-shrink(不常用)
  • flex-basis(不常用)
  • flex
  • align-self

order

  • order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0
  • .item { order: ; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-item div{
font-size: 14px;
position: relative;
top: -100px;
}
.box-22 .order {
order: -1;
}
</style>
</head>
<body>
<div class="box box-22">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
<div class="box-item order">4<div>(order:-1)</div></div>
</div>
</body>
</html>

image

flex-grow

  • flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大
  • 如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话);如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍
  • .item { flex-grow: ; / default 0 / }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-item div{
font-size: 14px;
position: relative;
top: -100px;
}
.box-23 .box-item{
flex-grow: 1;
}
.box-23 .grow-2 {
flex-grow: 2;
}
</style>
</head>
<body>
<div class="box box-23">
<div class="box-item grow">1 <div>flex-grow: 1</div></div>
<div class="box-item grow grow-2">2 <div>flex-grow: 2</div></div>
<div class="box-item grow">3 <div>flex-grow: 1</div></div>
</div>
</body>
</html>

image

flex-shrink

  • flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小
  • 如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小
  • 负值对该属性无效
  • .item { flex-shrink: ; / default 1 / }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-item div{
font-size: 14px;
position: relative;
top: -100px;
}
.box-24 .box-item {
width: 400px;
}
.box-24 .shrink{
flex-shrink: 0;
}
</style>
</head>
<body>
<div class="box box-24">
<div class="box-item shrink">1 <div>flex-shrink: 0</div></div>
<div class="box-item">2</div>
<div class="box-item">3</div>
</div>
</body>
</html>

image

flex-basis属性

  • flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)
  • 浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小
  • .item { flex-basis: ; | auto; / default auto / }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-25 .box-item {
flex-basis: 200px;
width: 400px; // width 将失去作用
}
</style>
</head>
<body>
<div class="box box-25">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item">3</div>
</div>
</body>
</html>

image

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’> ] }

align-self属性

  • align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch
  • .item { align-self: auto | flex-start | flex-end | center | baseline | stretch; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
padding: 20px;
margin: 0 auto;
background-color: #ccc;
max-width: 1024px;
}
.box {
background-color: white;
display: flex;
}
.box-item {
width: 150px;
height: 150px;
line-height: 150px;
margin: 5px;
background-color: #ffd200;
font-size: 100px;
color: white;
text-align: center;
}
.box-item div{
font-size: 14px;
position: relative;
top: -100px;
}
.box-26 {
height: 400px;
}
.box-26 .box-item {
align-self: flex-start;
}
.box-26 .end {
align-self: flex-end;
}
</style>
</head>
<body>
<div class="box box-26">
<div class="box-item">1</div>
<div class="box-item">2</div>
<div class="box-item end">3<div>flex-end</div></div>
<div class="box-item">4</div>
</div>
</body>
</html>

image