A Complete Guide to Flexbox _ CSS-Tricks - CSS-Tricks
A Complete Guide to Flexbox _ CSS-Tricks - CSS-Tricks
HOME / GUIDES /
Our comprehensive guide to CSS flexbox layout. This complete guide explains
everything about flexbox, focusing on all the different possible properties for the
parent element (the flex container) and the child elements (the flex items). It also
includes history, demos, patterns, and a browser support chart.
Table of contents
Part 1: (#aa-introduction) Background (#aa-background)
Part 2: (#aa-introduction) Basics and terminology (#aa-basics-and-terminology)
Part 3: (#aa-introduction) Flexbox properties (#aa-flexbox-properties)
Part 4: (#aa-introduction) Prefixing Flexbox (#aa-prefixing-flexbox)
Part 5: (#aa-introduction) Examples (#aa-examples)
Part 6: (#aa-introduction) Flexbox tricks (#aa-flexbox-tricks)
Part 7: (#aa-introduction) Browser support (#aa-browser-support)
Part 8: (#aa-introduction) Bugs (#aa-bugs)
Part 9: (#aa-introduction) Related properties (#aa-related-properties)
Part 10: (#aa-introduction) More information (#aa-more-information)
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 1/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
Reference this guide a lot? Here’s a high-res image you can print!
(#aa-background) Background
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 2/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
(#aa-properties-for-the- (#aa-properties-for-the-
parentflex-container) Properties childrenflex-items) Properties for
for the Parent the Children
(flex container) (flex items)
CSS
.container {
display: flex; /* or inline-flex */
}
(#aa-flex-direction) flex- By default, flex items are laid out in the source
direction order. However, the order property controls
the order in which they appear in the flex
container.
CSS
.item {
order: 5; /* default is 0 */
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 3/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
CSS
.container {
flex-direction: row | row-reverse | column | column-
}
CSS
.item {
flex-grow: 4; /* default 0 */
}
By default, flex items will all try to fit onto one
line. You can change that and allow the items
to wrap as needed with this property. Negative numbers are invalid.
CSS
.container {
flex-wrap: nowrap | wrap | wrap-reverse;
}
(#aa-flex-shrink) flex-shrink
This defines the ability for a flex item to
nowrap (default): all flex items will be on
shrink if necessary.
one line
wrap: flex items will wrap onto multiple CSS
.item {
lines, from top to bottom. flex-shrink: 3; /* default 1 */
}
wrap-reverse: flex items will wrap onto
multiple lines from bottom to top.
Negative numbers are invalid.
There are some visual demos of flex-wrap
here (https://css-
tricks.com/almanac/properties/f/flex-wrap/) .
(#aa-flex-basis) flex-basis
This defines the default size of an element
before the remaining space is distributed. It
(#aa-flex-flow) flex-flow can be a length (e.g. 20%, 5rem, etc.) or a
This is a shorthand for the flex-direction keyword. The auto keyword means “look at
and flex-wrap properties, which together my width or height property” (which was
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 4/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
define the flex container’s main and cross temporarily done by the main-size keyword
axes. The default value is row nowrap. until deprecated). The content keyword
means “size it based on the item’s content” –
CSS
CSS
.item {
(#aa-justify-content) justify- flex-basis: | auto; /* default auto */
content }
(#aa-flex) flex
This is the shorthand for flex-grow, flex-
shrink and flex-basis combined. The
second and third parameters (flex-shrink
and flex-basis) are optional. The default is 0
1 auto, but if you set it with a single number
value, like flex: 5;, that changes the flex-
basis to 0%, so it’s like setting flex-grow: 5;
flex-shrink: 1; flex-basis: 0%;.
CSS
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'f
}
This defines the alignment along the main
axis. It helps distribute extra free space
It is recommended that you use this
leftover when either all the flex items on a line
shorthand property rather than set the
are inflexible, or are flexible but have reached
individual properties. The shorthand sets the
their maximum size. It also exerts some
other values intelligently.
control over the alignment of items when they
overflow the line.
CSS
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 5/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
(#aa-align-items) align-items
CSS
.container {
align-items: stretch | flex-start | flex-end | cente
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 7/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
(#aa-align-content) align-
content
CSS
.container {
align-content: flex-start | flex-end | center | spac
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 8/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
(#aa-gap-row-gap-column-gap)
gap, row-gap, column-gap
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 9/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
CSS
.container {
display: flex;
...
gap: 10px;
gap: 10px 20px; /* row-gap column gap */
row-gap: 10px;
column-gap: 20px;
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 10/11
16/09/2022 00:46 A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks
(#aa-examples) Examples
(#aa-bugs) Bugs
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 11/11