Page layout is a key application of CSS.
The traditional solution for layout, based on the box model, depends on the Display property + Position property + Float property. It is very inconvenient for those special layouts, for example, vertical centering is not easy to achieve.
In 2009, the website proposed a new solution----flex layout that enables easy, complete, responsive implementation of various page layouts. Currently, it has been supported by all browsers, which means that this feature is now safe to use.
The flex layout becomes the preferred solution for future layouts. This article describes its syntax, and the next article gives the flex notation for common layouts.
The following two articles are mainly referenced below: A complete guides to Flexbox and a Visual guide to CSS3 Flexbox Properties.
First, what is the flex layout?
Flex is the abbreviation for flexible box, meaning "resilient layout", which provides maximum flexibility for box-shaped models.
Any container can be specified as a flex layout.
.box{ display: flex;}
Inline elements can also use the flex layout.
.box{ display: inline-flex;}
WebKit the kernel browser, you must -webkit
prefix it.
.box{ display: -webkit-flex; /* Safari */ display: flex;}
Note that after you set the flex layout, the, and properties of the child elements float
clear
vertical-align
are invalidated.
Ii. Basic Concepts
Elements that use flex layouts, called Flex container, are referred to as "containers." All of its child elements automatically become container members, known as Flex items (Flex Item), or "project" for short.
The container has two axes by default: the horizontal spindle (main axis) and the vertical intersection axis (cross axis). The starting position of the spindle (the intersection with the border) is called main start
, the end position is called main end
; the start position of the intersection axis is called cross start
, the end position is called cross end
.
The project is arranged by default along the spindle. The main spindle space occupied by a single item is called the main size
occupied cross-axis space cross size
.
Third, the container properties
The following 6 properties are set on a container.
- Flex-direction
- Flex-wrap
- Flex-flow
- Justify-content
- Align-items
- Align-content
3.1 Flex-direction Properties
flex-direction
Properties determine the direction of the spindle (that is, the direction in which the item is arranged).
.box { flex-direction: row | row-reverse | column | column-reverse;}
It may have a value of 4.
row
(default): The spindle is in the horizontal direction, starting at the left.
row-reverse
: The spindle is in the horizontal direction, starting at the right end.
column
: The spindle is in the vertical direction and the starting point is in the upper edge.
column-reverse
: The spindle is in the vertical direction and the starting point is in the lower edge.
3.2 Flex-wrap Properties
By default, items are lined up in a line (also known as the "axis"). flex-wrap
property Definition, how to wrap a line if one of the axes does not fit.
.box{ flex-wrap: nowrap | wrap | wrap-reverse;}
It may take three values.
(1) nowrap
(default): No Line break.
(2) wrap
: line break, the first line is above.
(3) wrap-reverse
: line break, the first line is below.
3.3 Flex-flow
flex-flow
Properties are flex-direction
flex-wrap
shorthand for properties and properties, and the default value is row nowrap
.
.box { flex-flow: <flex-direction> || <flex-wrap>;}
3.4 Justify-content Properties
justify-content
property defines how the item is aligned on the spindle.
.box { justify-content: flex-start | flex-end | center | space-between | space-around;}
It may take 5 values, and the exact alignment is related to the direction of the axis. The following assumes that the spindle is from left to right.
flex-start
(default): Align Left
flex-end
: Align Right
center
: Center
space-between
: Justified, the interval between items is equal.
space-around
: The intervals on each side of the item are equal. Therefore, the interval between items is one times larger than the interval between items and borders.
3.5 Align-items Properties
align-items
property defines how the item is aligned on the intersection axis.
.box { align-items: flex-start | flex-end | center | baseline | stretch;}
It may take 5 values. The specific alignment is related to the direction of the cross axis, which assumes that the intersection axis is from top to bottom.
flex-start
: The starting point of the intersection axis is aligned.
flex-end
: The end alignment of the intersection axis.
center
: The midpoint of the intersection axis is aligned.
baseline
: The baseline alignment of the first line of text for the item.
stretch
(default): If the item is not set to height or set to auto, the height of the entire container will be filled.
3.6 Align-content Properties
align-content
Properties define the alignment of multiple axes. If the item has only one axis, this property does not work.
.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch;}
This property may take 6 values.
flex-start
: Aligns with the start point of the intersection axis.
flex-end
: Aligns with the end point of the intersection axis.
center
: Aligns with the midpoint of the intersection axis.
space-between
: aligned with the intersection axis, evenly spaced between axes.
space-around
: The intervals between each axis are equal. Therefore, the spacing between the axes is one times larger than the interval between the axes and the border.
stretch
(default): The axis fills the entire cross axis.
Iv. Properties of the project
The following 6 properties are set on a project.
order
flex-grow
flex-shrink
flex-basis
flex
align-self
4.1 Order Property
order
property defines the order in which items are arranged. The smaller the number, the higher the alignment, and the default is 0.
.item { order: <integer>;}
4.2 Flex-grow Properties
flex-grow
property defines the magnification of the item, by default, that is 0
, if there is space left, do not zoom in.
.item { flex-grow: <number>; /* default 0 */}
If all items have flex-grow
properties of 1, they will divide the remaining space (if any). If an item has a flex-grow
property of 2 and the other item is 1, the former occupies more than the remaining space.
4.3 Flex-shrink properties
flex-shrink
Property defines the scale-out for the project, which defaults to 1, that is, if there is not enough space, the item shrinks.
.item { flex-shrink: <number>; /* default 1 */}
If all items have a flex-shrink
property of 1, they will be scaled down when there is not enough space. If an item has a flex-shrink
property of 0 and the other items are 1, the former does not shrink when there is not enough space.
A negative value is not valid for this property.
4.4 Flex-basis Properties
flex-basis
property defines the spindle space (main size) that the item occupies before allocating extra space. Based on this property, the browser calculates whether the spindle has extra space. Its default value is the auto
original size of the project.
.item { flex-basis: <length> | auto; /* default auto */}
It can be set to width
height
the same value as a property (such as 350px), then the item will occupy a fixed space.
4.5 Flex Properties
flex
The property is flex-grow
, flex-shrink
and shorthand for, flex-basis
the default value 0 1 auto
. The latter two properties are optional.
.item { flex: none | [ <‘flex-grow‘> <‘flex-shrink‘>? || <‘flex-basis‘> ]}
This property has two shortcut values: auto
( 1 1 auto
) and None ( 0 0 auto
).
It is recommended that you use this property instead of writing three separate properties separately, because the browser calculates the relevant values.
4.6 Align-self Properties
align-self
property allows a single item to have a different alignment than other items, overriding the align-items
property. The default value auto
, which represents the property that inherits the parent element align-items
, is equivalent to if there is no parent element stretch
.
.item { align-self: auto | flex-start | flex-end | center | baseline | stretch;}
This property may take 6 values, except auto, and the other is exactly the same as the Align-items property.
Flex Layout Tutorial: Grammar chapter (GO)