preface

Flex layout for simple, complete, and responsive page layouts. Currently, this layout is supported by all browsers, so we can safely use this feature.

What is the Flex layout?

Flex, which stands for “flexible layout,” is used to provide maximum flexibility for box models. Any container can be specified with a Flex layout, and inline elements can also use a Flex layout.

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

The basic concept

Elements that use Flex layout are called Flex containers, or “containers” for short. All child elements automatically become container members and are called Flex projects, or “projects” for short.

The container has two axes by default: a horizontal axis and a vertical cross axis. By default, items are arranged along the main axis.

Container properties

The following six properties are set on the container.

The flex – direction attribute

The flex-direction property determines the direction of the spindle.

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

It could have four values.

Row (default) : The main axis is horizontal and the starting point is on the left. Row-reverse: The main axis is horizontal and the starting point is at the right end. Column: The main axis is in the vertical direction, and the starting point is in the upper edge. Column-reverse: the main axis is vertical and the starting point is at the bottom.Copy the code

The flex – wrap attributes

By default, items are placed in one line. The flex-wrap property defines how to wrap a line if an axis does not fit.

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

It could take three values.

Nowrap (default) : no line breaks. Wrap: The first line is at the top. Wrap-reverse: newline with the first line at the bottom.Copy the code

The flex – flow properties

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

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-align Flex-end: right-align center: center space-between: align both ends with equal intervals between items. Space-around: Equal spacing on both sides of each item. As a result, the spacing between items is twice as large as the spacing between items and the border.Copy the code

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: Alignment of the starting point of the cross axes. Flex-end: alignment of ends of crossed axes. Center: Alignment of the midpoint of the cross axis. Baseline: The baseline alignment of the first line of text of the project. Stretch (default) : If the height is not set or auto is set, the project will occupy the entire height of the container.Copy the code

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: aligns with the end of the cross axis. Center: aligns with the midpoint of the intersecting axis. Space-between: aligned with both ends of the intersecting axes, with evenly distributed spacing between axes. 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 takes up the entire cross axis.Copy the code

Project attributes

The following six properties are set on the project.

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

The flex – turns up properties

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

Note: If all projects have a flex-grow attribute of 1, they will 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.

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

Note: 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.

The flex – the basis properties

The Flex-basis property defines the amount of principal space that the project occupies before allocating excess 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

Note: If set to the same value as the width or height attributes, the item will occupy a fixed space.

Flex properties

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).

Note: Use this attribute in preference to writing three separate attributes, as the browser will infer related values.

The align – self properties

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.

Excerpt from: Ruan Yifeng’s weblog