The traditional solution to layout, based on the box model, relies on the display property + position property + float property. It is very inconvenient for special layouts, such as vertical center, which is not easy to implement.

In 2009, the W3C introduced a new solution, —-Flex layout, that enables easy, complete, and responsive implementation of a variety of page layouts. It is currently supported by all browsers, which means it is safe to use it now.

What is the Flex layout?

Flex, short for Flexible Box, is designed to provide maximum flexibility to Box models.

Any container can be specified as a Flex layout.

.box{
  display: flex;
}
Copy the code

Inline elements can also be laid out using Flex.

.box{
  display: inline-flex;
}
Copy the code

Webkit-kernel browsers must be prefixed with -webkit.

.box{
  display: -webkit-flex; /* Safari */
  display: flex;
}
Copy the code

Note that with Flex layout, the float, clear, and vertical-align attributes of the child elements are invalidated.

2. Basic concepts

Elements with a Flex layout are called Flex containers, or “containers” for short. All of its child elements automatically become container members and are called Flex items, or “items.”

The container has two axes by default: a horizontal main axis and a vertical cross axis. The starting position of the spindle (where it intersects with the border) is called main start and the ending position is called main end; The starting position of the intersecting axis is called cross start and the ending position is called cross end.

By default, items are arranged along the main axis. The main axis space occupied by a single project is called main size, and the cross axis space occupied is called cross size.

Properties of the container

The following six properties are set on the container.

  · flex-direction
  · flex-wrap
  · flex-flow
  · justify-content
  · align-items
  · align-content

3.1 the flex – direction attribute

The flex-direction attribute determines the direction of the main axis (that is, the alignment of items).

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

It could have four values. (4 values correspond to 1, 2, 3 and 4 above)

· Column-reverse: the main axis is vertical and the starting point is down. · Column: The main axis is vertical, and the starting point is in the upper edge. · ROW (default) : The main axis is horizontal and the starting point is at the left end. · Row-reverse: The main axis is horizontal and the starting point is at the right end.

3.2 the flex – wrap attributes

By default, projects are arranged on a line (also known as an “axis”).flex-wrapProperty defines how to wrap a line if an axis does not fit.

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

It may take three values 1)nowrap(Default) : no line breaks.

1)wrap: newline, first line above.

1)wrap-reverse: newline, first line below.

3.3 the flex – flow

The flex-flow property is a short form of the flex-direction and flex-wrap properties. The default value is Row Nowrap.

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

3.4 the justify – content attribute

The context-content attribute defines the alignment of items on the main axis.

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

It might take five values, aligned in a way that depends on the direction of the axis. So let’s say that the principal axis is going from left to right.

· Flex-start (default) : left-aligned · Flex-end: right-aligned · Center: centered · space-between: aligned at both ends with equal intervals between items. · Space-around: Equal spacing on both sides of each project. As a result, the spacing between items is twice as large as the spacing between items and the border.

3.5 the align – the items property

The align-items property defines how items are aligned on the cross axis.

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

It could take five values. The exact alignment depends on the direction of the intersecting axis, which is assumed to run from top to bottom.

· Flex-start: Align the starting point of the cross axis. · Flex-end: Align the end of the cross axis. · Center: align the midpoint of the cross axis. · Baseline: Baseline alignment of the first line of text of the project. · Stretch (default) : If the height of the project is not set or set to Auto, it will occupy the entire height of the container.

3.6 the align – content attribute

The align-content property defines the alignment of multiple axes. This property has no effect if the project has only one axis.

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

The property may take six values.

· Flex-start: align with the starting point of the cross axis. · Flex-end: align with the end of the cross axis. · Center: align with the midpoint of the cross axis. · Space-between: align with both ends of the cross axes, and the spacing between axes is evenly distributed. · Space-around: The spacing on both sides of each axis is equal. Therefore, the spacing between axes is twice as large as the spacing between axes and borders. · Stretch (default) : axis covers the entire cross axis.

4. Project attributes

4.1 the order attribute

The order attribute defines the order of items. The smaller the value is, the more advanced it is. The default value is 0.

.item {
  order: <integer>;
}
Copy the code

4.2 the flex – turns attributes

The Flex-Grow property defines the project’s zoom scale, which defaults to 0, or no zoom if there is free space.

.item {
  flex-grow: <number>; /* default 0 */
}
Copy the code

If all projects have a flex-Grow attribute of 1, they divide the remaining space equally, if any. If one project has a flex-grow attribute of 2 and all the other projects are 1, the former takes up twice as much free space as the other items.

4.3 the flex – the shrink properties

The Flex-shrink attribute defines the scale by which a project shrinks. The default is 1, that is, if there is insufficient space, the project shrinks.

.item {
  flex-shrink: <number>; /* default 1 */
}
Copy the code

If all projects have a Flex-shrink attribute of 1, they are scaled equally when space is insufficient. If the flex-shrink attribute is 0 for one project and 1 for all other projects, the former does not shrink when space is insufficient.

Negative values have no effect on this property.

4.4 the flex – the basis of attributes

The Flex-basis property defines the main size of the project before allocating extra space. Based on this property, the browser calculates whether the main axis has extra space. Its default value is Auto, the original size of the project.

.item {
  flex-basis: <length> | auto; /* default auto */
}
Copy the code

It can be set to the same value as the width or height attribute (such as 350px), and the project will take up a fixed space.

4.5 the flex property

The flex attribute is short for flex-grow, flex-shrink, and flex-basis. The default value is 0 1 Auto. The last two attributes are optional.

.item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'>]}Copy the code

This property has two shortcut values: auto (1 1 auto) and None (0 0 auto).

It is recommended to use this attribute in preference to writing three separate attributes, as the browser will infer related values.

4.6 the align – self attribute

The align-self property allows a single item to have a different alignment than other items, overriding the align-items property. The default value is auto, which inherits the align-items property of the parent element. If there is no parent element, it equals stretch.

.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
Copy the code

This property may take six values, all identical to the align-items property except auto.

· Flex-start: Align the starting point of the cross axis. · Flex-end: Align the end of the cross axis. · Center: align the midpoint of the cross axis. · Baseline: Baseline alignment of the first line of text of the project. · Stretch (default) : If the height of the project is not set or set to Auto, it will occupy the entire height of the container. Auto,

Thanks to ruan Yifeng senior road pilot, summed up the layout.

If you still want to see Flex: Examples, here’s the Portal to Flex: Examples

Reprinted from: Ruan Yifeng Flex layout tutorial: Syntax