Flex
layout, also known as flexible layout, provides the maximum flexibility for box models and is the preferred solution for layout. It is now supported by all modern browsers.
By specifying display: flex
, a flex container is identified, which is called a FLEX
container. The boxes inside the container become members of the FLEX
container. The container has two axes by default: the horizontal main axis and the vertical cross axis. The starting position of the main axis is called main start
, and the ending position is called main end
; the starting position of the cross axis is called cross start
, and the ending position is called cross end
. The members of the container are arranged along the main axis by default.
The flex-direction
property determines the direction of the main axis, with values of row | row-reverse | column | column-reverse
.
row
- the main axis is horizontal, starting from the left.row-reverse
- the main axis is horizontal, starting from the right, and the order of the container members is reversed compared to row
.column
- the main axis is vertical, starting from the top.column-reverse
- the main axis is vertical, starting from the bottom, and the order of the container members is reversed compared to column
.The flex-wrap
property determines whether to wrap the flex items when they cannot fit in one line, with values of nowrap | wrap | wrap-reverse
.
nowrap
- no wrapping, the flex items will shrink proportionally along the axis when there is not enough space.wrap
- wrap to a new line when there is not enough space.wrap-reverse
- wrap to a new line when there is not enough space, with the new line starting from the top.The flex-flow
property is a shorthand for flex-direction
and flex-wrap
, with the default value of row nowrap
.
The justify-content
property defines how flex items are aligned along the main axis, allowing for easy implementation of various layouts. The possible values are flex-start | flex-end | center | space-between | space-around
.
flex-start
(default): Aligns items to the left.flex-end
: Aligns items to the right.center
: Aligns items to the center.space-between
: Aligns items with equal spacing between them, with the first item aligned to the start and the last item aligned to the end.space-around
: Aligns items with equal spacing around them, with the spacing between each item and the container's edges being twice as large as the spacing between items.The align-items
property defines how flex items are aligned along the cross axis. The possible values are flex-start | flex-end | center | baseline | stretch
.
stretch
(default): If the item does not have a set height or is set to auto
, it will fill the entire height of the container.flex-start
: Aligns items to the start of the cross axis.flex-end
: Aligns items to the end of the cross axis.center
: Aligns items to the center of the cross axis.baseline
: Aligns items based on the baseline of the first line of text within each item.The align-content
property defines how multiple lines of flex items are aligned along the cross axis. If there is only one line of flex items, this property has no effect. The possible values are flex-start | flex-end | center | space-between | space-around | stretch
.
stretch
(default): The lines of flex items will stretch to fill the entire cross axis.flex-start
: Aligns the lines of flex items to the start of the cross axis.flex-end
: Aligns the lines of flex items to the end of the cross axis.center
: Aligns the lines of flex items to the center of the cross axis.space-between
: Aligns the lines of flex items with equal spacing between them, with the first line aligned to the start and the last line aligned to the end.space-around
: Aligns the lines of flex items with equal spacing around them, with the spacing between each line and the container's edges being twice as large as the spacing between lines.The order
property defines the order in which flex items are displayed. The lower the value, the earlier the item is displayed. The default value is 0
.
The flex-grow
property defines the growth factor of a flex item, with a default value of 0
.
The flex-shrink
property defines the shrink factor of a flex item, with a default value of 1
. If there is not enough space, the item will shrink accordingly.
The flex-basis
property defines the initial main size of a flex item before any available space is distributed. The browser uses this property to determine if there is any extra space on the main axis. Its default value is auto
, which means the item's original size.
The flex
property is a shorthand for flex-grow
, flex-shrink
, and flex-basis
, with a default value of 0 1 auto
. The last two properties are optional.
The align-self
property allows an individual flex item to have a different alignment than the other items, overriding the align-items
property of the parent. Its default value is auto
, which means it inherits the align-items
property of the parent element. If there is no parent element, it is equivalent to stretch
.