CSS3 Flexible Box (Flex Box)
The flexible box is a new layout mode in CSS3.
CSS3 flexible box (Flexible Box or flexbox) is a layout approach that ensures elements behave appropriately when a page needs to adapt to different screen sizes and device types.
The flexible box layout model was introduced to provide a more efficient way to arrange, align, and distribute space among child elements in a container.
Browser support
The numbers in the table indicate the version number of the first browser that supports this property.
The -webkit- or -moz- following a number is the prefix for the specified browser.
| property | |||||
|---|---|---|---|---|---|
| Basic support | |||||
| (single-line flexbox) | 29.0 | ||||
| 21.0 -webkit- | 11.0 | 22.0 | |||
| 18.0 -moz- | 6.1 -webkit- | 12.1 -webkit- | |||
| Multi-line flexbox | 29.0 | ||||
| 21.0 -webkit- | 11.0 | 28.0 | 6.1 -webkit- | 17.0 |
15.0 -webkit-
12.1 |
CSS3 Flexbox Content
A flexbox consists of a flex container and flex items.
A flex container is defined by setting the display property to flex or inline-flex.
A flex container contains one or more flex items.
Note: Outside the flex container and inside the flex items, rendering is normal. Flexbox only defines how flex items are laid out inside the flex container.
Flex items are usually displayed in a single row within the flexbox. By default each container has only one row.
The following elements show flex items displayed in a single row, from left to right:
Example
<!DOCTYPE html><html>
<head>
<style.flex-container{>display:
webkit-flex
;
display:
flex
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
.flex-item
{
background-color:
cornflowerblue
;
width:
100
px
;
height:
100
px
;
margin:
10
px
;
}
style
head
<body>
<div class="flex-container">
<div class="flex-item">flex item 1
div
<div class="flex-item">flex item 2
div
<div class="flex-item">flex item 3
div
div
body
html
Try it »
Of course we can change the arrangement.
If we set direction attribute to rtl (right-to-left), the arrangement of the flex items will change, and the page layout will change accordingly:
Example
body
{
direction:
rtl
;
}
.flex-container
{
display:
webkit-flex
;
display:
flex
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
.flex-item
{
background-color:
cornflowerblue
;
width:
100
px
;
height:
100
px
;
margin:
10
px
;
}
Try it »
flex-direction
flex-direction The property specifies the position of flex items in the parent container.
Syntax
flex-direction: row | row-reverse | column | column-reverse
flex-directionThe values are:
- row: arranged horizontally from left to right (left-aligned), the default arrangement.
- row-reverse: reversed horizontal arrangement (right-aligned, arranged from back to front, with the last item first).
- column: arranged vertically.
- column-reverse: reversed vertical arrangement, arranged from back to front, with the last item at the top.
The following example demonstrates the row-reverse usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-direction:
row-reverse
;
flex-direction:
row-reverse
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the column usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-direction:
column
;
flex-direction:
column
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the column-reverse usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-direction:
column-reverse
;
flex-direction:
column-reverse
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
justify-content Property
The justify-content property is applied to the flex container to align flex items along the main axis of the container.
The justify-content syntax is as follows:
justify-content: flex-start | flex-end | center | space-between | space-around
Explanation of each value:
- flex-start:
Flex items are packed toward the start of the line. This is the default. The main-start margin edge of the first flex item is placed at the main-start edge of the line, and subsequent items are laid out flush in order.
- flex-end:
Flex items are packed toward the end of the line. The main-end margin edge of the first flex item is placed at the main-end edge of the line, and subsequent items are laid out flush in order.
- center:
Flex items are packed toward the center of the line. (If the remaining free space is negative, the flex items will overflow in both directions.)
- space-between:
Flex items are evenly distributed on the line. If the remaining space is negative or there is only one flex item, this value is equivalent to flex-start. Otherwise, the margin of the first item aligns with the main-start edge of the line, the margin of the last item aligns with the main-end edge, and the remaining items are distributed on the line with equal spacing between adjacent items.
- space-around:
Flex items are evenly distributed on the line, with half the spacing left at both ends. If the remaining space is negative or there is only one flex item, this value is equivalent to center. Otherwise, the items are distributed along the line with equal spacing between them (say 20px), while half the spacing (1/2*20px=10px) is left between the two ends and the flex container.
The effect is shown below:

The following example demonstrates the flex-end usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-justify-content:
flex-end
;
justify-content:
flex-end
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the center usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-justify-content:
center
;
justify-content:
center
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the space-between usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-justify-content:
space-between
;
justify-content:
space-between
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the space-around usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-justify-content:
space-around
;
justify-content:
space-around
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
align-items Property
align-items Sets or retrieves the alignment of flexbox elements along the cross axis (vertical axis).
Syntax
align-items: flex-start | flex-end | center | baseline | stretch
Explanation of each value:
- flex-start: The cross-start margin edge of the flexbox element is placed flush with the cross-start edge of the line.
- flex-end: The cross-start margin edge of the flexbox element is placed flush with the cross-end edge of the line.
- center: The flexbox element is centered along the cross axis (vertical axis) of the line. (If the line's size is smaller than the element's size, it will overflow equally in both directions.)
- baseline: If the inline axis and the cross axis of the flexbox element are the same, this value is equivalent to 'flex-start'. Otherwise, the value participates in baseline alignment.
- stretch: If the property specifying the cross-axis size has the value 'auto', its value will make the item's margin box as close as possible to the size of the line, while still respecting the constraints of the 'min/max-width/height' properties.
The following example demonstrates the stretch(默认值) usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-align-items:
stretch
;
align-items:
stretch
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the flex-start usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-align-items:
flex-start
;
align-items:
flex-start
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the flex-end usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-align-items:
flex-end
;
align-items:
flex-end
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the center usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-align-items:
center
;
align-items:
center
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the baseline usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-align-items:
baseline
;
align-items:
baseline
;
width:
400
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
flex-wrap Property
flex-wrap The property specifies how the child elements of a flexbox wrap.
Syntax
flex-wrap: nowrap|wrap|wrap-reverse|initial|inherit;
Explanation of each value:
- nowrap - Default; the flex container is a single line. In this case flex items may overflow the container.
- wrap - The flex container is multi-line. In this case overflowing flex items are placed on new lines, and line breaks occur within the items
- wrap-reverse - Reversed wrap arrangement.
The following example demonstrates the nowrap usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-wrap:
nowrap
;
flex-wrap:
nowrap
;
width:
300
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the wrap usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-wrap:
wrap
;
flex-wrap:
wrap
;
width:
300
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
The following example demonstrates the wrap-reverse usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-wrap:
wrap-reverse
;
flex-wrap:
wrap-reverse
;
width:
300
px
;
height:
250
px
;
background-color:
lightgrey
;
}
Try it »
align-content Property
align-content The property is used to modify the flex-wrap behavior of the property. Similar to align-items, but instead of setting the alignment of flex items, it sets the alignment of the lines.
Syntax
align-content: flex-start | flex-end | center | space-between | space-around | stretch
Explanation of each value:
stretch- Default. The lines will stretch to take up the remaining space.flex-start- The lines are packed toward the start of the flex container.flex-end- The lines are packed toward the end of the flex container.center- The lines are packed toward the middle of the flex container.space-between- The lines are evenly distributed in the flex container.space-around- The lines are evenly distributed in the flex container, with half the spacing between child elements kept at both ends.
The following example demonstrates the center usage:
Example
.flex-container
{
display:
webkit-flex
;
display:
flex
; -
webkit-flex-wrap:
wrap
;
flex-wrap:
wrap
; -
webkit-align-content:
center
;
align-content:
center
;
width:
300
px
;
height:
300
px
;
background-color:
lightgrey
;
}
Try it »
Flex Item Properties
Sorting
Syntax
order:
Explanation of each value:
: Defines the order with an integer value; smaller values come first. Negative values are allowed.
order The property sets properties of flex items within the flex container:
Example
.flex-item
{
background-color:
cornflowerblue
;
width:
100
px
;
height:
100
px
;
margin:
10
px
;
}
.first
{
webkit-order:
1
;
order:
1
;
}
Try it »
Alignment
Setting the "margin" value to "auto" automatically takes up the remaining space in the flex container. So setting the vertical margin to "auto" can center a flex item completely along both axes of the flex container.
The following example sets on the first flex item margin-right: auto; . It places the remaining space to the right of the element:
Example
.flex-item
{
background-color:
cornflowerblue
;
width:
75
px
;
height:
75
px
;
margin:
10
px
;
}
.flex-item
:first-child
{
margin-right:
auto
;
}
Try it »
Perfect Centering
The following example will perfectly solve the centering problems we usually encounter.
With flexbox, centering becomes very simple; you only need to set margin: auto; which can center a flex item completely along both axes:
Example
.flex-item
{
background-color:
cornflowerblue
;
width:
75
px
;
height:
75
px
;
margin:
auto
;
}
Try it »
align-self
align-self The property sets the alignment of the flex element itself along the cross axis (vertical axis).
Syntax
align-self: auto | flex-start | flex-end | center | baseline | stretch
Explanation of each value:
- auto: If the value of 'align-self' is 'auto', its computed value is the 'align-items' value of the element's parent; if it has no parent, the computed value is 'stretch'.
- flex-start: The cross-start margin edge of the flexbox element is placed flush with the cross-start edge of the line.
- flex-end: The cross-start margin edge of the flexbox element is placed flush with the cross-end edge of the line.
- center: The flexbox element is centered along the cross axis (vertical axis) of the line. (If the line's size is smaller than the element's size, it will overflow equally in both directions.)
- baseline: If the inline axis and the cross axis of the flexbox element are the same, this value is equivalent to 'flex-start'. Otherwise, the value participates in baseline alignment.
- stretch: If the property specifying the cross-axis size has the value 'auto', its value will make the item's margin box as close as possible to the size of the line, while still respecting the constraints of the 'min/max-width/height' properties.
The following example demonstrates the effect of different align-self values on a flex item:
Example
.flex-item
{
background-color:
cornflowerblue
;
width:
60
px
;
min-height:
100
px
;
margin:
10
px
;
}
.item1
{
webkit-align-self:
flex-start
;
align-self:
flex-start
;
}
.item2
{
webkit-align-self:
flex-end
;
align-self:
flex-end
;
}
.item3
{
webkit-align-self:
center
;
align-self:
center
;
}
.item4
{
webkit-align-self:
baseline
;
align-self:
baseline
;
}
.item5
{
webkit-align-self:
stretch
;
align-self:
stretch
;
}
Try it »
flex
flex The property specifies how a flex item distributes space.
Syntax
flex: auto | initial | none | inherit | [ flex-grow ] || [ flex-shrink ] || [ flex-basis ]
Explanation of each value:
- auto: computed value is 1 1 auto
- initial: computed value is 0 1 auto
- none: computed value is 0 0 auto
- inherit: inherited from the parent element
- [ flex-grow ]: Defines the expansion ratio of a flexbox element.
- [ flex-shrink ]: Defines the shrink ratio of a flexbox element.
- [ flex-basis ]: Defines the default basis value of a flexbox element.
In the following example, the first flex item takes 2/4 of the space, and the other two each take 1/4:
Example
.flex-item
{
background-color:
cornflowerblue
;
margin:
10
px
;
}
.item1
{
webkit-flex:
2
;
flex:
2
;
}
.item2
{
webkit-flex:
1
;
flex:
1
;
}
.item3
{
webkit-flex:
1
;
flex:
1
;
}
Try it »
More examples
Creating a Responsive Page with Flexbox
CSS3 Flexbox Properties
The table below lists the properties commonly used in flexbox:
| property | Description |
|---|---|
| display | Specifies the box type of an HTML element. |
| flex-direction | Specifies the arrangement of child elements in a flex container |
| justify-content | Sets the alignment of flexbox elements along the main axis (horizontal axis). |
| align-items | Sets the alignment of flexbox elements along the cross axis (vertical axis). |
| flex-wrap | Sets whether the child elements of a flexbox wrap when they overflow the parent container. |
| align-content | Modifies the behavior of the flex-wrap property, similar to align-items, but instead of setting the alignment of child elements, it sets the alignment of lines |
| flex-flow | Shorthand for flex-direction and flex-wrap |
| order | Sets the order of the child elements of a flexbox. |
| align-self | Used on a flex item. Overrides the container's align-items property. |
| flex | Sets how the child elements of a flexbox distribute space. |