Reference this guide a lot? Here’s a high-res image you can print! <\/p>\n\n\n\n
The Flexbox Layout<\/code> (Flexible Box) module (
a W3C Candidate Recommendation<\/a> as of October 2017) aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and\/or dynamic (thus the word “flex”).<\/p>\n\n\n\n
Note:<\/strong> Flexbox layout is most appropriate to the components of an application, and small-scale layouts, while the Grid<\/a> layout is intended for larger scale layouts.<\/p>\n\n\n<\/details>\n\n\n
\n \n Basics and terminology<\/h3>\n <\/summary>\n \n\nSince flexbox is a whole module and not a single property, it involves a lot of things including its whole set of properties. Some of them are meant to be set on the container (parent element, known as “flex container”) whereas the others are meant to be set on the children (said “flex items”).<\/p>\n\n\n\n
If “regular” layout is based on both block and inline flow directions, the flex layout is based on “flex-flow directions”. Please have a look at this figure from the specification, explaining the main idea behind the flex layout.<\/p>\n\n\n\n
<\/figure>\n\n\n\nItems will be laid out following either the main axis<\/code> (from main-start<\/code> to main-end<\/code>) or the cross axis (from cross-start<\/code> to cross-end<\/code>).<\/p>\n\n\n\n
\n- main axis<\/strong> – The main axis of a flex container is the primary axis along which flex items are laid out. Beware, it is not necessarily horizontal; it depends on the
flex-direction<\/code> property (see below).<\/li>\n\n\n\n
main-start | main-end<\/strong> – The flex items are placed within the container starting from main-start and going to main-end.<\/li>\n\n\n\n
main size<\/strong> – A flex item’s width or height, whichever is in the main dimension, is the item’s main size. The flex item’s main size property is either the \u2018width\u2019 or \u2018height\u2019 property, whichever is in the main dimension.<\/li>\n\n\n\n
cross axis<\/strong> – The axis perpendicular to the main axis is called the cross axis. Its direction depends on the main axis direction.<\/li>\n\n\n\n
cross-start | cross-end<\/strong> – Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side.<\/li>\n\n\n\n
cross size<\/strong> – The width or height of a flex item, whichever is in the cross dimension, is the item’s cross size. The cross size property is whichever of \u2018width\u2019 or \u2018height\u2019 that is in the cross dimension.<\/li>\n<\/ul>\n\n\n<\/details>\n\n\n
\n \n Flexbox properties<\/h3>\n <\/summary>\n \n\n\n\n
<\/figure>\n\n\nProperties for the Parent
(flex container)<\/h2>\n\n\ndisplay<\/h4>\n\n\n
This defines a flex container; inline or block depending on the given value. It enables a flex context for all its direct children.<\/p>\n\n\n\n
.container {\n display: flex; \/* or inline-flex *\/\n}<\/code><\/pre>\n\n\n\nNote that CSS columns have no effect on a flex container.<\/p>\n<\/div><\/div>\n\n\n\n
flex-direction<\/h4>\n\n\n
<\/figure>\n\n\n\n
This establishes the main-axis, thus defining the direction flex items are placed in the flex container. Flexbox is (aside from optional wrapping) a single-direction layout concept. Think of flex items as primarily laying out either in horizontal rows or vertical columns.<\/p>\n\n\n\n
.container {\n flex-direction: row | row-reverse | column | column-reverse;\n}<\/code><\/pre>\n\n\n\n\nrow<\/code> (default): left to right in ltr<\/code>; right to left in rtl<\/code><\/li>\n\n\n\n
row-reverse<\/code>: right to left in ltr<\/code>; left to right in rtl<\/code><\/li>\n\n\n\n
column<\/code>: same as row<\/code> but top to bottom<\/li>\n\n\n\n
column-reverse<\/code>: same as row-reverse<\/code> but bottom to top<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\nflex-wrap<\/h4>\n\n\n
<\/figure>\n\n\n\nBy 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.<\/p>\n\n\n\n
.container {\n flex-wrap: nowrap | wrap | wrap-reverse;\n}<\/code><\/pre>\n\n\n\n\nnowrap<\/code> (default): all flex items will be on one line<\/li>\n\n\n\n
wrap<\/code>: flex items will wrap onto multiple lines, from top to bottom.<\/li>\n\n\n\n
wrap-reverse<\/code>: flex items will wrap onto multiple lines from bottom to top.<\/li>\n<\/ul>\n\n\n\nThere are some visual demos of flex-wrap<\/code> here<\/a>.<\/p>\n<\/div><\/div>\n\n\n\n
flex-flow<\/h4>\n\n\nThis is a shorthand for the flex-direction<\/code> and flex-wrap<\/code> properties, which together define the flex container’s main and cross axes. The default value is row nowrap<\/code>.<\/p>\n\n\n\n
.container {\n flex-flow: column wrap;\n}<\/code><\/pre>\n<\/div><\/div>\n\n\n\n
justify-content<\/h4>\n\n\n
<\/figure>\n\n\n\n
This defines the alignment along the main axis. It helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.<\/p>\n\n\n\n
.container {\n justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly | start | end | left | right ... + safe | unsafe;\n}<\/code><\/pre>\n\n\n\n
\nflex-start<\/code> (default): items are packed toward the start of the flex-direction.<\/li>\n\n\n\n
flex-end<\/code>: items are packed toward the end of the flex-direction.<\/li>\n\n\n\n
start<\/code>: items are packed toward the start of the writing-mode<\/code> direction.<\/li>\n\n\n\n
end<\/code>: items are packed toward the end of the writing-mode<\/code> direction.<\/li>\n\n\n\n
left<\/code>: items are packed toward left edge of the container, unless that doesn’t make sense with the flex-direction<\/code>, then it behaves like start<\/code>.<\/li>\n\n\n\n
right<\/code>: items are packed toward right edge of the container, unless that doesn’t make sense with the flex-direction<\/code>, then it behaves like end<\/code>.<\/li>\n\n\n\n
center<\/code>: items are centered along the line<\/li>\n\n\n\n
space-between<\/code>: items are evenly distributed in the line; first item is on the start line, last item on the end line<\/li>\n\n\n\n
space-around<\/code>: items are evenly distributed in the line with equal space around them. Note that visually the spaces aren’t equal, since all the items have equal space on both sides. The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.<\/li>\n\n\n\n
space-evenly<\/code>: items are distributed so that the spacing between any two items (and the space to the edges) is equal.<\/li>\n<\/ul>\n\n\n\nNote that that browser support for these values is nuanced. For example, space-between<\/code> never got support from some versions of Edge, and start\/end\/left\/right aren’t in Chrome yet. MDN has detailed charts<\/a>. The safest values are flex-start<\/code>, flex-end<\/code>, and center<\/code>.<\/p>\n\n\n\n
align-items<\/h4>\n\n\n
<\/figure>\n\n\n\n
This defines the default behavior for how flex items are laid out along the cross axis<\/strong> on the current line. Think of it as the justify-content<\/code> version for the cross-axis (perpendicular to the main-axis).<\/p>\n\n\n\n
.container {\n align-items: stretch | flex-start | flex-end | center | baseline | first baseline | last baseline | start | end | self-start | self-end + ... safe | unsafe;\n}<\/code><\/pre>\n\n\n\n
\nstretch<\/code> (default): stretch to fill the container (still respect min-width\/max-width)<\/li>\n\n\n\n
flex-start<\/code> \/ start<\/code> \/ self-start<\/code>: items are placed at the start of the cross axis. The difference between these is subtle, and is about respecting the flex-direction<\/code> rules or the writing-mode<\/code> rules.<\/li>\n\n\n\n
flex-end<\/code> \/ end<\/code> \/ self-end<\/code>: items are placed at the end of the cross axis. The difference again is subtle and is about respecting flex-direction<\/code> rules vs. writing-mode<\/code> rules.<\/li>\n\n\n\n
center<\/code>: items are centered in the cross-axis<\/li>\n\n\n\n
baseline<\/code>: items are aligned such as their baselines align<\/li>\n<\/ul>\n\n\n\n
The safe<\/code> and unsafe<\/code> modifier keywords can be used in conjunction with all the rest of these keywords (although note
browser support<\/a>), and deal with helping you prevent aligning elements such that the content becomes inaccessible.<\/p>\n<\/div><\/div>\n\n\n\n
align-content<\/h4>\n\n\n
<\/figure>\n\n\n\n
This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content<\/code> aligns individual items within the main-axis.<\/p>\n\n\n\n
Note:<\/strong> This property only takes effect on multi-line flexible containers, where flex-wrap<\/code> is set to either wrap<\/code> or wrap-reverse<\/code>). A single-line flexible container (i.e. where flex-wrap<\/code> is set to its default value, no-wrap<\/code>) will not reflect align-content<\/code>.<\/p>\n\n\n\n
.container {\n align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | start | end | baseline | first baseline | last baseline + ... safe | unsafe;\n}<\/code><\/pre>\n\n\n\n
\nnormal<\/code> (default): items are packed in their default position as if no value was set.<\/li>\n\n\n\n
flex-start<\/code> \/ start<\/code>: items packed to the start of the container. The (more supported) flex-start<\/code> honors the flex-direction<\/code> while start<\/code> honors the writing-mode<\/code> direction.<\/li>\n\n\n\n
flex-end<\/code> \/ end<\/code>: items packed to the end of the container. The (more support) flex-end<\/code> honors the flex-direction<\/code> while end honors the writing-mode<\/code> direction.<\/li>\n\n\n\n
center<\/code>: items centered in the container<\/li>\n\n\n\n
space-between<\/code>: items evenly distributed; the first line is at the start of the container while the last one is at the end<\/li>\n\n\n\n
space-around<\/code>: items evenly distributed with equal space around each line<\/li>\n\n\n\n
space-evenly<\/code>: items are evenly distributed with equal space around them<\/li>\n\n\n\n
stretch<\/code>: lines stretch to take up the remaining space<\/li>\n<\/ul>\n\n\n\n
The safe<\/code> and unsafe<\/code> modifier keywords can be used in conjunction with all the rest of these keywords (although note
browser support<\/a>), and deal with helping you prevent aligning elements such that the content becomes inaccessible.<\/p>\n\n\n\n
<\/div><\/div>\n<\/div><\/div>\n\n\n\ngap, row-gap, column-gap<\/h4>\n\n\n
<\/figure>\n\n\n\nThe gap<\/code> property<\/a> explicitly controls the space between flex items. It applies that spacing only between items<\/em> not on the outer edges. <\/p>\n\n\n\n
.container {\n display: flex;\n ...\n gap: 10px;\n gap: 10px 20px; \/* row-gap column gap *\/\n row-gap: 10px;\n column-gap: 20px;\n}<\/code><\/pre>\n\n\n\nThe behavior could be thought of as a minimum<\/em> gutter, as if the gutter is bigger somehow (because of something like justify-content: space-between;<\/code>) then the gap will only take effect if that space would end up smaller.<\/p>\n\n\n\n
It is not exclusively for flexbox, gap<\/code> works in grid and multi-column layout as well. <\/p>\n<\/div><\/div>\n<\/div>\n\n\n\n
\n
<\/figure>\n\n\nProperties for the Children
(flex items)<\/h2>\n\n\norder<\/h4>\n\n\n
<\/figure>\n\n\n\n
By default, flex items are laid out in the source order. However, the order<\/code> property controls the order in which they appear in the flex container.<\/p>\n\n\n\n
.item {\n order: 5; \/* default is 0 *\/\n}<\/code><\/pre>\n\n\n\nItems with the same order<\/code> revert to source order.<\/p>\n<\/div><\/div>\n\n\n\n
flex-grow<\/h4>\n\n\n
<\/figure>\n\n\n\n
This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.<\/p>\n\n\n\n
If all items have flex-grow<\/code> set to 1<\/code>, the remaining space in the container will be distributed equally to all children. If one of the children has a value of 2<\/code>, that child would take up twice as much of the space as either one of the others (or it will try, at least).<\/p>\n\n\n\n
.item {\n flex-grow: 4; \/* default 0 *\/\n}<\/code><\/pre>\n\n\n\nNegative numbers are invalid.<\/p>\n<\/div><\/div>\n\n\n\n
flex-shrink<\/h4>\n\n\n
This defines the ability for a flex item to shrink if necessary.<\/p>\n\n\n\n
.item {\n flex-shrink: 3; \/* default 1 *\/\n}<\/code><\/pre>\n\n\n\nNegative numbers are invalid.<\/p>\n<\/div><\/div>\n\n\n\n
flex-basis<\/h4>\n\n\n
This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword. The auto<\/code> keyword means “look at my width or height property” (which was temporarily done by the main-size<\/code> keyword until deprecated). The content<\/code> keyword means “size it based on the item’s content” – this keyword isn’t well supported yet, so it’s hard to test and harder to know what its brethren max-content<\/code>, min-content<\/code>, and fit-content<\/code> do.<\/p>\n\n\n\n
.item {\n flex-basis: | auto; \/* default auto *\/\n}<\/code><\/pre>\n\n\n\nIf set to 0<\/code>, the extra space around content isn’t factored in. If set to auto<\/code>, the extra space is distributed based on its flex-grow<\/code> value. See this graphic.<\/a><\/p>\n<\/div><\/div>\n\n\n\n
flex<\/h4>\n\n\nThis is the shorthand for flex-grow,<\/code> flex-shrink<\/code> and flex-basis<\/code> combined. The second and third parameters (flex-shrink<\/code> and flex-basis<\/code>) are optional. The default is 0 1 auto<\/code>, but if you set it with a single number value, like flex: 5;<\/code>, that changes the flex-basis<\/code> to 0%, so it’s like setting flex-grow: 5; flex-shrink: 1; flex-basis: 0%;<\/code>.<\/p>\n\n\n\n
.item {\n flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]\n}<\/code><\/pre>\n\n\n\nIt is recommended that you use this shorthand property<\/strong> rather than set the individual properties. The shorthand sets the other values intelligently.<\/p>\n<\/div><\/div>\n\n\n\n
align-self<\/h4>\n\n\n
<\/figure>\n\n\n\n
This allows the default alignment (or the one specified by align-items<\/code>) to be overridden for individual flex items.<\/p>\n\n\n\n
Please see the align-items<\/code> explanation to understand the available values.<\/p>\n\n\n\n
.item {\n align-self: auto | flex-start | flex-end | center | baseline | stretch;\n}<\/code><\/pre>\n\n\n\nNote that float<\/code>, clear<\/code> and vertical-align<\/code> have no effect on a flex item.<\/p>\n<\/div><\/div>\n\n\n<\/div>\n<\/div>\n\n\n\n
<\/p>\n\n\n<\/details>\n\n\n
\n
\n Prefixing Flexbox<\/h3>\n <\/summary>\n \n\n
Flexbox requires some vendor prefixing to support the most browsers possible. It doesn’t just include prepending properties with the vendor prefix, but there are actually entirely different property and value names. This is because the Flexbox spec has changed over time, creating an “old”, “tweener”, and “new”<\/a> versions.<\/p>\n\n\n\n
Perhaps the best way to handle this is to write in the new (and final) syntax and run your CSS through Autoprefixer<\/a>, which handles the fallbacks very well.<\/p>\n\n\n\n
@mixin flexbox() {\n display: -webkit-box;\n display: -moz-box;\n display: -ms-flexbox;\n display: -webkit-flex;\n display: flex;\n}\n\n@mixin flex($values) {\n -webkit-box-flex: $values;\n -moz-box-flex: $values;\n -webkit-flex: $values;\n -ms-flex: $values;\n flex: $values;\n}\n\n@mixin order($val) {\n -webkit-box-ordinal-group: $val; \n -moz-box-ordinal-group: $val; \n -ms-flex-order: $val; \n -webkit-order: $val; \n order: $val;\n}\n\n.wrapper {\n @include flexbox();\n}\n\n.item {\n @include flex(1 200px);\n @include order(2);\n}<\/code><\/pre>\n\n\n<\/details>\n\n\n\n \n Examples<\/h3>\n <\/summary>\n \n\n
Let’s start with a very very simple example, solving an almost daily problem: perfect centering. It couldn’t be any simpler if you use flexbox.<\/p>\n\n\n\n
.parent {\n display: flex;\n height: 300px; \/* Or whatever *\/\n}\n\n.child {\n width: 100px; \/* Or whatever *\/\n height: 100px; \/* Or whatever *\/\n margin: auto; \/* Magic! *\/\n}<\/code><\/pre>\n\n\n\nThis relies on the fact a margin set to auto<\/code> in a flex container absorb extra space. So setting a margin of auto<\/code> will make the item perfectly centered in both axes.<\/p>\n\n\n\n
Now let’s use some more properties. Consider a list of 6 items, all with fixed dimensions, but can be auto-sized. We want them to be evenly distributed on the horizontal axis so that when we resize the browser, everything scales nicely, and without media queries.<\/p>\n\n\n\n
.flex-container {\n \/* We first create a flex layout context *\/\n display: flex;\n\n \/* Then we define the flow direction \n and if we allow the items to wrap \n * Remember this is the same as:\n * flex-direction: row;\n * flex-wrap: wrap;\n *\/\n flex-flow: row wrap;\n\n \/* Then we define how is distributed the remaining space *\/\n justify-content: space-around;\n}<\/code><\/pre>\n\n\n\nDone. Everything else is just some styling concern. Below is a pen featuring this example. Be sure to go to CodePen and try resizing your windows to see what happens.<\/p>\n\n\n\n