Css Flex 布局

display: flex
An introduction about layout by flexbox (called flex as a value in display property.)

Property of Parent elements (Flex container)

display

Obviously, in this topic, the display of the parent must be ‘flex’, also it can be an inline element – ‘inline-flex’

flex-direction

1
2
3
.container {
flex-direction: row | row-reverse | column | column-reverse;
}

This property describe the direction of this whole flexbox.

  • row (default): left to right in ltr; right to left in rtl
  • row-reverse: right to left in ltr; left to right in rtl
  • column: same as row but top to bottom
  • column-reverse: same as row-reverse but bottom to top

flex-wrap

1
2
3
.container{
flex-wrap: nowrap | wrap | wrap-reverse;
}

This property describe whether the flexbox will be in oneline or in multiple lines.

  • nowrap (default): all flex items will be on one line
  • wrap: flex items will wrap onto multiple lines, from top to bottom.
  • wrap-reverse: flex items will wrap onto multiple lines from bottom to top.

flex-flow

1
flex-flow: <‘flex-direction’> || <‘flex-wrap’>

This is a shorthand flex-direction and flex-wrap properties, which together define the flex container’s main and cross axes. Default is row nowrap.

justify-content

1
2
3
.container {
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}
  • flex-start (default): items are packed toward the start line
  • flex-end: items are packed toward to end line
  • center: items are centered along the line
  • space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
  • space-around: items are evenly distributed in the line with equal space around them. Note that visually the spaces aren’t equal, since all the items have equal space on both sides. The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.
  • space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is equal.

align-items

1
2
3
.item {
align-items: center;
}

5 values include:

  • flex-start: cross-start margin edge of the items is placed on the cross-start line
  • flex-end: cross-end margin edge of the items is placed on the cross-end line
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align
  • stretch (default): stretch to fill the container (still respect min-width/max-width)

Property align-content

1
2
3
.flex-row {
align-items: center;
}

5 values include:

  • flex-start: cross-start margin edge of the items is placed on the cross-start line
  • flex-end: cross-end margin edge of the items is placed on the cross-end line
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align
  • stretch (default): stretch to fill the container (still respect min-width/max-width)