Skip to main content

Flex 容器的属性

flex-direction

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

flex-direction

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

它有四个有效选项

默认值:row

  • row: 主轴为水平方向 起点在左端
  • row-reverse: 主轴为水平方向 起点在右端
  • column: 主轴为垂直方向 起点在最上面
  • column-reverse: 主轴为垂直方向 起点在下沿

flex-wrap

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

flex-wrap

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

它有三个有效选项

默认值:nowrap

  • nowrap:不换行

    flex-direction

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

    flex-direction

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

    flex-direction

flex-wrap

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

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

justify-content

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

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

flex-direction

它有五个有效选项

默认值:flex-start

  • flex-start:左对齐
  • flex-end: 右对齐
  • center: 居中
  • space-between: 两端对齐 项目之间的间隔都相等
  • space-around: 每个项目两侧的间隔相等 项目之间的间隔比项目与边框的间隔大一倍

align-items

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

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

align-items

默认值:stretch

它有五个有效选项

  • flex-start:交叉轴的起点对齐
  • flex-end:交叉轴的终点对齐
  • center:交叉轴的中点对齐
  • baseline:项目的第一行文字的基线对齐
  • stretch:如果项目未设置高度或设置未 auto 将占满整个容器的宽度

align-content 属性

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

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

align-content

默认值:stretch

它有六个有效选项

  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between: 与交叉轴两端对齐,轴线之间的间隔平均分布
  • space-around: 每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch:轴线占满整个交叉轴。