avatar

Flex布局

思维导图

传统布局和flex布局对比

传统布局特点

兼容性好

布局繁琐

局限性,不能再移动端很好的布局

flex布局特点

操作方便,布局极其简单,移动端使用比较广泛

pc端浏览器支持情况比较差

IE11或更低版本不支持flex或仅支持部分

建议

移动端 或者 PC端不需要兼容低版本浏览器 就可以使用用flex布局

flex布局原理

给父盒子设置display:flex;来控制子元素的排列方式

flex布局注意事项

使用flex布局就不要使用浮动、vertical-align,这两个样式会失效

flex布局中没有X轴和Y轴的概念

主轴和交叉轴是相对的,可以变换的。主轴方向是横的,交叉轴方向就是竖的。主轴方向是竖的,交叉轴方向就是横的

主轴相关配置

注意事项

1.主轴侧轴相关配置要写到父盒子中

flex-direction

功能和注意事项

功能:设置主轴的方向

注意事项:主轴和交叉轴是相对的,可以变换的。主轴是横的,交叉轴就是竖的,主轴是竖的,交叉轴就是横的

取值范围

说明
row(默认) 主轴为横轴,子元素横着排列(默认,常用)
column 主轴为纵轴,子元素竖着排列

flex-direction:row(默认值,常用)

主轴为横轴,子元素横着排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴,那么子元素会横着排列 */
flex-direction: row;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>

<div class="box">
<span>1</span>
<span>2</span>
</div>
1623660908216

flex-direction:column (偶尔用)

主轴为纵轴,子元素从上到下排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为纵轴,那么子元素竖着排列 */
flex-direction: column;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>

<div class="box">
<span>1</span>
<span>2</span>
</div>
1623661237965

justify-content

功能

设置主轴上的子元素排列方式

取值范围

说明
flex-start(默认) 从主轴头部开始排列
flex-end 从主轴尾部开始排列
center 居中显示
space-around 平分剩余空间
space-between 先两边贴边,再平分剩余空间

justify-content:flext-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

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式,因为主轴方向为横轴,flex-start表示从头部开始排列,所以排列方式为从左到右 */
justify-content: flex-start;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>

<div class="box">
<span>1</span>
<span>2</span>
</div>
1623662578223

主轴为纵轴时,从上向下排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: column;
/* 设置主轴排列方式,因为主轴方向为纵轴,flex-start表示从头部位置排列,头部位置在上,所以排列方式为从上到下 */
justify-content: flex-start;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>

<div class="box">
<span>1</span>
<span>2</span>
</div>
1623662675756

justify-content: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

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式,因为主轴方向为横轴,所以排列方式为从右到左 */
justify-content: flex-end;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>

<div class="box">
<span>1</span>
<span>2</span>
</div>
1623662791509

主轴为纵轴时,从下向上排列

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

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: column;
/* 设置主轴排列方式,因为主轴方向为纵轴,所以排列方式为从下到上 */
justify-content: flex-end;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>


<div class="box">
<span>1</span>
<span>2</span>
</div>
1623662870094

justify-content: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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式,因为主轴方向为横轴,所以子元素在横轴居中显示 */
justify-content: center;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
</div>
</body>
</html>

1623663027777

主轴为纵向时,上下居中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: column;
/* 设置主轴排列方式,因为主轴方向为纵轴,所以子元素在纵轴居中显示 */
justify-content: center;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>

<div class="box">
<span>1</span>
<span>2</span>
</div>

1623663076952

justify-content: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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式 */
justify-content: space-around;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
</div>
</body>
</html>
1623663358530

主轴为纵轴时

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: column;
/* 设置主轴排列方式 */
justify-content: space-around;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
</div>
</body>
</html>
1623663442172

justify-content: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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式 */
justify-content: space-between;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
</div>
</body>
</html>
1623663674550

主轴为纵轴时

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: column;
/* 设置主轴排列方式 */
justify-content: space-between;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
</div>
</body>
</html>
1623663733543

flex-wrap

功能

设置子元素是否换行

取值范围

说明
wrap 换行(常用)
nowrap 不换行(默认)

flex-wrap: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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式 */
justify-content: flex-start;
/* 子元素放不下就换行 */
flex-wrap: wrap;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
1623664075692

flex-wrap: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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
/* 在父盒子中通过display: flex启用flex布局,用来控制子盒子的排列 */
display: flex;
/* 设置主轴排列方向为横轴 */
flex-direction: row;
/* 设置主轴排列方式 */
justify-content: flex-start;
/* 子元素放不下就换行 */
flex-wrap: nowrap;
width: 100px;
height: 100px;
background-color: burlywood;
}
.box > span {
width: 40px;
height: 40px;
background-color: coral;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
1623664134543

交叉轴(侧轴)相关配置

交叉轴和主轴是垂直关系,主轴为横向,交叉轴为纵向。主轴为纵向,交叉轴为横向。

注意事项

交叉轴的相关配置也是写在父盒子中。

align-items 设置侧轴上的子元素的排列方式(主轴不换行)

功能

设置侧轴上的子元素排列方式

取值范围

说明
stretch(默认) 侧轴内容拉伸显示
flex-start 从侧轴尾部开始排列
flex-end 从侧轴尾部开始排列
center 居中显示

在线演示地址

https://www.runoob.com/try/playit.php?f=playcss_align-items&preval=stretch

align-items:flex-start

1623682847928

align-items:flex-end

1623682935504

align-items:center

1623682976427

align-items:stretch

1623683036038

align-content 设置侧轴上的子元素的排列方式(主轴换行)

功能

设置子项在侧轴上的排列方式

注意事项

主轴必须换行,flex-wrap:的值是wrap

取值范围

说明
flex-start 默认值,在侧轴的头部开始排列
flex-end 在侧轴的尾部开始排列
center 在侧轴中间显示
space-around 子项在侧轴平分剩余空间
space-between 子项在侧轴先贴边,剩下的平分剩余空间

在线演示地址

https://www.runoob.com/try/try.php?filename=trycss3_align-content

align-content:flex-start

1623684218538

align-content:flex-end

1623684264695

align-content:center

1623684305973

align-content:space-around

1623684640418

align-content:space-between

1623685526782

align-content 和align-items区别

  • align-items 适用于单行情况下, 只有上对齐、下对齐、居中和 拉伸
  • align-content适应于换行(多行)的情况下(单行情况下无效), 可以设置 上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性值。
  • 总结就是单行找align-items 多行找 align-content

flex-flow

该属性是 flex-direction 和 flex-wrap 属性的复合属性

1
flex-flow:row wrap;

flex布局子项常见属性

  • flex子项目占的份数
  • align-self控制子项自己在侧轴的排列方式
  • order属性定义子项的排列顺序(前后顺序)

flex 属性

flex 属性定义子项目分配剩余空间,用flex来表示占多少份数。

1
2
3
.item {
flex: <number>; /* 默认值 0 */
}
1623685804325

align-self控制子项自己在侧轴上的排列方式

align-self 属性允许单个项目有与其他项目不一样的对齐方式,可覆盖 align-items 属性。

默认值为 auto,表示继承父元素的 align-items 属性,如果没有父元素,则等同于 stretch。

1
2
3
4
span:nth-child(2) {
/* 设置自己在侧轴上的排列方式 */
align-self: flex-end;
}
1623685872763

order 属性定义项目的排列顺序

数值越小,排列越靠前,默认为0。

注意:和 z-index 不一样。

1
2
3
.item {
order: <number>;
}

面试题

如果以下两个面试题能理解,那么flex布局就掌握的非常OK了

使用flex布局完成圣杯布局

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 特点:三栏布局,左右两侧固定,中间自适应,并且中间内容需要优先加载 -->
<style>
.big {
display: flex;
width: 100%;
height: 300px;
border: 1px solid black;
}
.big > div:nth-child(1) {
flex: 1;
background-color: coral;
}
.big > div:nth-child(2) {
order: -1;
width: 200px;
background-color: bisque;
}
.big > div:nth-child(3) {
width: 200px;
background-color: burlywood;
}
</style>
</head>
<body>
<div class="big">
<div>center</div>
<div>left</div>
<div>right</div>
</div>
</body>
</html>

使用flex布局完成三点的骰子

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
div {
display: flex;
justify-content: space-between;
width: 300px;
height: 300px;
border: 1px solid black;
border-radius: 30px;
}
div span {
width: 80px;
height: 80px;
border-radius: 50%;
background-color: coral;
}

div span:nth-child(2) {
align-self: center;
}
div span:nth-child(3) {
align-self: flex-end;
}
</style>
</head>
<body>
<div>
<span></span>
<span></span>
<span></span>
</div>
</body>
</html>
文章作者: 微信:hao_yongliang
文章链接: https://haoyongliang.gitee.io/2021/06/14/%E5%89%8D%E7%AB%AF/css/flex%E5%B8%83%E5%B1%80/flex%E5%B8%83%E5%B1%80/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 郝永亮的主页
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论