# Flex 布局

9 min read
Table of Contents

Flex 布局,即为弹性布局。

Flex 容器

要使用 Flex 布局,首先需要定义一个父容器 container。

display: flex

1
2
<div class="container">
<div>flex item</div>
<div>flex item</div>
</div>
.container {
display: flex;
}

Flex 容器属性

- flex-direction: row^ | row-reverse | column | column-reverse;
- flex-wrap: nowrap^ | wrap | wrap-reverse;
- flex-flow: <flex-direction> || <flex-wrap>; (^: row nowrap)
- justify-content: flex-start^ | flex-end | center | space-between | space-around | space-evenly;
- align-items: stretch^ | flex-start | flex-end | center | baseline;
- align-content: stretch^ | flex-start | flex-end | center | space-between | space-around;

备注: ^: default(默认值)

flex-direction

该属性定义了 flex 项在容器朝哪个方向上堆叠。主要是水平方向或垂直方向上。

flex-direction: row

1
2
3

flex-direction: row-reverse

1
2
3

flex-direction: column

1
2
3

flex-direction: column-reverse

1
2
3

flex-wrap

该属性定义了 flex 项在容器上是否换行。

flex-wrap: nowrap (默认)

1
2
3
4

flex-wrap: wrap

1
2
3
4

flex-flow

该属性是flex-direction和flex-items的简写。

flex-flow: column wrap (即 flex-direction: column; flex-wrap: wrap)

1
2
3
4
5
6

justify-content

该属性定义了 flex 项在容器水平方向上的对齐方式。

justify-content: flex-start (默认)

1
2
3

justify-content: flex-end

1
2
3

justify-content: center

1
2
3

justify-content: space-between

1
2
3

justify-content: space-around

1
2
3

justify-content: space-evenly

1
2
3

align-items

该属性定义了 flex 项在容器垂直方向上的对齐方式。

align-items: stretch (默认)

1
2
3

align-items: flex-start

1
2
3

align-items: flex-end

1
2
3

align-items: center

1
2
3

align-items: baseline

1
2
3

align-content

该属性定义了 flex 项在容器上多行的垂直方向对齐方向, 要求flex-wrap: wrap属性。

align-content: center (需配合 flex-wrap: wrap)

1
2
3
4
5
6

align-content: flex-start

1
2
3
4
5
6

align-content: flex-end

1
2
3
4
5
6

align-content: space-between

1
2
3
4
5
6

align-content: space-around

1
2
3
4
5
6

PS: 该属性在 flex 项只有一行没有效果。

Flex 项

Flex 容器下的直接子元素就是 flex 项。

Flex 项属性

- order: <integer>; (^: 0)
- flex-grow: <number>; (^: 0)
- flex-shrink: <number>; (^: 1)
- flex-basis: <length> | auto^;
- flex: none | [ <flex-grow> <flex-shrink>? || <flex-basis> ]; (^: 0 1 auto)
- align-self: auto^ | flex-start | flex-end | center | baseline | stretch;

order

该属性指定了 flex 项的排列顺序,必须是整数。值越小排名越靠前,默认都为 0。

order: 3 1 2
DOM 顺序为 1、2、3

1
2
3

flex-grow

该属性指定了 flex 项相对于其他的 flex 项的增长量,按比例划分,负数无效。默认值为 0,表示该元素不索取父元素的剩余空间。

flex-grow: 1 / 2 / 1 (剩余空间按比例分配)

1
2
3

flex-shrink

该属性指定了 flex 项相对于其他的 flex 项的收缩量,按比例划分,负数无效。

flex-shrink: 3 / 1 / 1 (空间不足时按比例收缩)

1
2
3

flex-basis

该属性指定了 flex 项的初始长度。例如 20%, 4rem, 250px…

flex-basis: 20% / 40% / auto

1
2
3

flex

该属性是flex-grow, flex-shrink和flex-basis等属性的缩写。

flex: 1 (即 1 1 0%)

1
2
3

align-self

该属性定义了 flex 项在容器内的垂直方向的对齐方向,该属性会覆盖容器的align-items属性设置的对齐方式。

容器的 align-items 为 flex-start,中间项 align-self 为 center

1
2
3

Flex 相关问题

flex 项中 flex 的值

flex 是 flex-grow, flex-shrink 和 flex-basis 的简写

flex 可以设置 flex: auto(1 1 auto) 和 flex: none(0 0 auto) 两个快捷值之外,还可以设置以下方式:

  • flex 取值非负整数,则 flex-grow 取该值,flex-shrink 取值为 1,flex-basis 取值 0%。
  • flex 取值为长度或者百分比,则 flex-basis 取该值,flex-grow 取值为 1,flex-shrink 取值为 1。
  • flex 取值为两个非负整数,则分别 flex-grow 和 flex-shrink 的值,flex-basis 取值 0%。
  • flex 取值为非负数字和一个长度或者百分比,则分别视为 flex-grow 和 flex-basis 的值,flex-shrink 取值 1。

由上面规律可得知,有非负整数先赋值给 flex-grow,长度/百分比赋值给 flex-basis,三项的默认值为:flex: 1 1 0%;

参考链接


More Posts

# SSH

3 min read

Secure Shell(简称 SSH, 安全外壳协议)是一种加密的网络传输协议,可以通过不安全的网络实现安全的系统管理和文件传输。典型的应用就是远程命令的登录和执行。SSH 通常用于类 Unix 系统上,但也可以用于 Windows 上。