Flexbox
The properties that lay out a flex container's children along a single axis: which way they run, whether they wrap, how they grow and shrink to share the space, and how they are aligned. The alignment properties here are shared with Grid, which also has the justify-* and place-* properties. Spacing between items is set with gap, under Gaps & gap decorations; display itself is under Box model.
Flex container
An element becomes a flex container with display: flex (a block-level container) or display: inline-flex (an inline-level one). Its in-flow children become flex items, laid out along the main axis set by flex-direction; the cross axis runs at right angles to it.
flex-direction
Sets the main axis, and so the direction items are placed in: along the inline axis (row, the direction text runs) or the block axis (column). The -reverse values place items from the opposite end.
| Syntax | row | row-reverse | column | column-reverse |
|---|---|
| Initial | row |
| Applies to | Flex containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 29 · Firefox 22 · Safari 9 |
| Spec | css-flexbox-2 |
The reverse values change only the visual order. Screen readers and keyboard focus still follow the source order.
flex-wrap
Whether items stay on one line and shrink to fit (nowrap) or break onto further lines when they run out of room (wrap). wrap-reverse stacks the new lines in the opposite cross-axis direction.
| Syntax | nowrap | [ wrap | wrap-reverse ] || balance |
|---|---|
| Initial | nowrap |
| Applies to | Flex containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 29 · Firefox 28 · Safari 9balance: Chrome 150 · Firefox no · Safari no |
| Spec | css-flexbox-2 |
balance spreads the items across the lines so each line is as close in length as possible, instead of filling every line and leaving a short last one.
flex-flow
Shorthand for flex-direction and flex-wrap, in either order.
| Syntax | <'flex-direction'> || <'flex-wrap'> |
|---|---|
| Initial | See individual properties |
| Applies to | See individual properties |
| Inherited | See individual properties |
| Animation | See individual properties |
| Support | Chrome 29 · Firefox 28 · Safari 9 |
| Spec | css-flexbox-2 |
flex-flow: row wrap;
flex-line-count
The minimum number of lines that balanced, wrapping items are spread over. It only has an effect when flex-wrap includes balance; if the count is at least the number of items, each item gets a line of its own.
| Syntax | <integer [1,∞]> |
|---|---|
| Initial | 1 |
| Applies to | Multi-line flex containers |
| Inherited | No |
| Animation | As an integer |
| Support | Chrome 150 · Firefox no · Safari no |
| Spec | css-flexbox-2 |
flex-flow: column balance;
flex-line-count: 2;
Flex items
flex
Shorthand for flex-grow, flex-shrink and flex-basis: how an item is sized and how it shares out the free space in its line. Use it rather than the longhands, because its single-number form sets a sensible basis.
| Syntax | none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] |
|---|---|
| Initial | 0 1 auto |
| Applies to | Flex items |
| Inherited | No |
| Percentages | See individual properties |
| Animation | By computed value type |
| Support | Chrome 29 · Firefox 22 · Safari 9 |
| Spec | css-flexbox-2 |
| Value | Same as | Effect |
|---|---|---|
initial | 0 1 auto | Sized by its width or content; shrinks if needed but never grows. |
auto | 1 1 auto | Sized by its width or content, then grows or shrinks to fill the line. |
none | 0 0 auto | Fixed at its width or content size. |
<number> | <number> 1 0 | Starts from zero and takes that share of the free space. |
.row { display: flex; }
.row > * { flex: 1; } /* 1 1 0 */
Items still will not shrink below their content's minimum size, because min-width defaults to auto on flex items. Add min-width: 0 when long words or wide children must be allowed to shrink.
flex-grow
How much of the line's leftover space the item takes, as a share relative to its siblings. 0 means it does not grow.
| Syntax | <number [0,∞]> |
|---|---|
| Initial | 0 |
| Applies to | Flex items |
| Inherited | No |
| Animation | By computed value type |
| Support | Chrome 29 · Firefox 20 · Safari 9 |
| Spec | css-flexbox-2 |
flex-shrink
How much the item gives up when the line is too short for all its items. Shrinking is weighted by the item's basis as well as this factor, so larger items lose more. 0 means it does not shrink.
| Syntax | <number [0,∞]> |
|---|---|
| Initial | 1 |
| Applies to | Flex items |
| Inherited | No |
| Animation | As a number |
| Support | Chrome 29 · Firefox 20 · Safari 9 |
| Spec | css-flexbox-2 |
flex-basis
The item's starting size along the main axis, before free space is shared out. auto uses the item's width or height; content sizes it from its content; any other value is a size like those accepted by width.
| Syntax | content | <'width'> |
|---|---|
| Initial | auto |
| Applies to | Flex items |
| Inherited | No |
| Percentages | Relative to the flex container's inner main size |
| Animation | By computed value type |
| Support | Chrome 29 · Firefox 22 · Safari 9 |
| Spec | css-flexbox-2 |
order
Moves a flex or grid item earlier or later in the layout. Items are placed in ascending order, and items with the same value keep their source order.
| Syntax | <integer> |
|---|---|
| Initial | 0 |
| Applies to | Flex items and grid items |
| Inherited | No |
| Animation | By computed value type |
| Support | Chrome 29 · Firefox 20 · Safari 9 |
| Spec | css-display-4 |
Only the visual position changes. Reading order and tab order still follow the source, so do not use order to fix content that is in the wrong place in the HTML.
Alignment
These box alignment properties also work in grid containers, and align-content also works in ordinary block containers. In flexbox, justify-* works along the main axis and align-* along the cross axis. The grammar uses a few shared groups: <content-distribution> is space-between | space-around | space-evenly | stretch; <content-position> is center | start | end | flex-start | flex-end; <self-position> adds self-start and self-end; <baseline-position> is baseline, first baseline or last baseline; and <overflow-position> is safe or unsafe.
justify-content
Distributes free space along the main axis of a flex container: between and around the items on each line. In a grid it places the grid tracks within the container instead.
| Syntax | normal | <content-distribution> | <overflow-position>? [ <content-position> | left | right ] |
|---|---|
| Initial | normal |
| Applies to | Multi-column containers, flex containers and grid containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 29 · Firefox 20 · Safari 9 |
| Spec | css-align-3 |
| Value | Effect |
|---|---|
normal | Behaves as stretch. In a flex container that packs items at the start, like flex-start. |
flex-start / flex-end | Packs items towards the start or end of the main axis, following flex-direction, so row-reverse flips them. |
start / end | Packs items towards the start or end of the container in its writing mode, ignoring any -reverse direction. |
left / right | Packs items towards the physical left or right edge. Where the axis is not horizontal they behave as start. |
center | Packs items together in the centre of the line. |
space-between | The first and last items sit at the edges, with equal space between each pair. |
space-around | Equal space on both sides of every item, so the space at each edge is half the space between items. |
space-evenly | Equal space between every pair of items and at both edges. |
stretch | In a grid, stretches auto-sized tracks to fill the container. In a flex container it behaves as flex-start. |
safe / unsafe | A prefix such as safe center. With safe, content that would overflow the container aligns to the start instead, so none of it is pushed out of reach. |
align-content
Distributes free space in the cross axis between the lines of a multi-line flex container, using the same values as justify-content. It has no effect on a single-line (nowrap) flex container. In a grid it places the row tracks within the container.
| Syntax | normal | <baseline-position> | <content-distribution> | <overflow-position>? <content-position> |
|---|---|
| Initial | normal |
| Applies to | Block containers, multi-column containers, flex containers and grid containers |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 29 · Firefox 28 · Safari 9 Block containers: Chrome 123 · Firefox 125 · Safari 17.4 |
| Spec | css-align-3 |
.hero { min-height: 60vh; align-content: center; }
align-items
Aligns every item within its line in the cross axis. It sets the default that each child's align-self: auto picks up.
| Syntax | normal | stretch | <baseline-position> | <overflow-position>? <self-position> |
|---|---|
| Initial | normal |
| Applies to | All elements |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 29 · Firefox 20 · Safari 9 |
| Spec | css-align-3 |
| Value | Effect |
|---|---|
normal | Behaves as stretch for flex items. |
stretch | Items with an auto cross size are stretched to fill the line. |
flex-start / flex-end | Items sit at the cross-start or cross-end edge of the line, following flex-wrap. |
start / end | Items sit at the start or end edge in the container's writing mode. |
self-start / self-end | Each item sits at the start or end edge in its own writing mode. |
center | Items are centred in the line. |
baseline, first baseline, last baseline | Items are shifted so their first (or last) lines of text share a baseline. |
safe / unsafe | A prefix such as safe center; safe aligns an overflowing item to the start instead. |
align-self
Overrides align-items for one item, with the same values. auto uses the parent's align-items. On an absolutely positioned box it aligns the box within its inset-modified containing block, and anchor-center centres it on its anchor.
| Syntax | auto | <overflow-position>? [ normal | <self-position> ]| stretch | <baseline-position> | anchor-center |
|---|---|
| Initial | auto |
| Applies to | Flex items, grid items and absolutely positioned boxes |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 29 · Firefox 20 · Safari 9 Absolutely positioned boxes: Chrome 122 · Firefox 134 · Safari 26 anchor-center: Chrome 125 · Firefox 147 · Safari 26 |
| Spec | css-align-3 |
Legacy box layout
The -webkit-box-* properties come from an early draft of flexbox and work on elements with display: -webkit-box or display: -webkit-inline-box. The WHATWG Compat Standard requires every browser to support them, with the -webkit- prefix, as mappings to the modern flex properties. Use flexbox for layout. Their one common use today is line clamping: display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3.
-webkit-box-orient Deprecated
The direction a legacy box lays out its children. Use flex-direction instead, except when setting up -webkit-line-clamp, which needs vertical.
| Syntax | horizontal | vertical | inline-axis | block-axis |
|---|---|
| Initial | inline-axis |
| Applies to | Elements with display: -webkit-box or -webkit-inline-box |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix) |
| Spec | compat |
.summary {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
-webkit-box-pack Deprecated
How a legacy box distributes its children along its direction; justify spreads them out. Use justify-content instead.
| Syntax | start | center | end | justify |
|---|---|
| Initial | start |
| Applies to | Elements with display: -webkit-box or -webkit-inline-box |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix) |
| Spec | compat |
-webkit-box-align Deprecated
How a legacy box aligns its children across its direction. Use align-items instead.
| Syntax | start | center | end | baseline | stretch |
|---|---|
| Initial | stretch |
| Applies to | Elements with display: -webkit-box or -webkit-inline-box |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix) |
| Spec | compat |
-webkit-box-flex Deprecated
How much a child of a legacy box grows to fill spare space. Use flex-grow, or the flex shorthand, instead.
| Syntax | <number> |
|---|---|
| Initial | 0 |
| Applies to | Direct children of an element with display: -webkit-box or -webkit-inline-box |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix) |
| Spec | compat |
-webkit-box-ordinal-group Deprecated
Moves a child of a legacy box earlier or later; lower groups come first. Use order instead.
| Syntax | <integer> |
|---|---|
| Initial | 1 |
| Applies to | Direct children of an element with display: -webkit-box or -webkit-inline-box |
| Inherited | No |
| Animation | Discrete |
| Support | Chrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix) |
| Spec | compat |