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.

Syntaxrow | row-reverse | column | column-reverse
Initialrow
Applies toFlex containers
InheritedNo
AnimationDiscrete
SupportChrome 29 · Firefox 22 · Safari 9
Speccss-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.

Syntaxnowrap | [ wrap | wrap-reverse ] || balance
Initialnowrap
Applies toFlex containers
InheritedNo
AnimationDiscrete
SupportChrome 29 · Firefox 28 · Safari 9
balance: Chrome 150 · Firefox no · Safari no
Speccss-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'>
InitialSee individual properties
Applies toSee individual properties
InheritedSee individual properties
AnimationSee individual properties
SupportChrome 29 · Firefox 28 · Safari 9
Speccss-flexbox-2
flex-flow: row wrap;
Items run along the line and wrap onto new lines when they no longer fit.

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,∞]>
Initial1
Applies toMulti-line flex containers
InheritedNo
AnimationAs an integer
SupportChrome 150 · Firefox no · Safari no
Speccss-flexbox-2
flex-flow: column balance;
flex-line-count: 2;
A list split into two balanced columns, however much content it has.

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.

Syntaxnone | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
Initial0 1 auto
Applies toFlex items
InheritedNo
PercentagesSee individual properties
AnimationBy computed value type
SupportChrome 29 · Firefox 22 · Safari 9
Speccss-flexbox-2
ValueSame asEffect
initial0 1 autoSized by its width or content; shrinks if needed but never grows.
auto1 1 autoSized by its width or content, then grows or shrinks to fill the line.
none0 0 autoFixed at its width or content size.
<number><number> 1 0Starts from zero and takes that share of the free space.
.row { display: flex; }
.row > * { flex: 1; }  /* 1 1 0 */
Every item starts from a basis of zero, so they share the width equally whatever their content.

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,∞]>
Initial0
Applies toFlex items
InheritedNo
AnimationBy computed value type
SupportChrome 29 · Firefox 20 · Safari 9
Speccss-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,∞]>
Initial1
Applies toFlex items
InheritedNo
AnimationAs a number
SupportChrome 29 · Firefox 20 · Safari 9
Speccss-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.

Syntaxcontent | <'width'>
Initialauto
Applies toFlex items
InheritedNo
PercentagesRelative to the flex container's inner main size
AnimationBy computed value type
SupportChrome 29 · Firefox 22 · Safari 9
Speccss-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>
Initial0
Applies toFlex items and grid items
InheritedNo
AnimationBy computed value type
SupportChrome 29 · Firefox 20 · Safari 9
Speccss-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.

Syntaxnormal | <content-distribution> | <overflow-position>? [ <content-position> | left | right ]
Initialnormal
Applies toMulti-column containers, flex containers and grid containers
InheritedNo
AnimationDiscrete
SupportChrome 29 · Firefox 20 · Safari 9
Speccss-align-3
ValueEffect
normalBehaves as stretch. In a flex container that packs items at the start, like flex-start.
flex-start / flex-endPacks items towards the start or end of the main axis, following flex-direction, so row-reverse flips them.
start / endPacks items towards the start or end of the container in its writing mode, ignoring any -reverse direction.
left / rightPacks items towards the physical left or right edge. Where the axis is not horizontal they behave as start.
centerPacks items together in the centre of the line.
space-betweenThe first and last items sit at the edges, with equal space between each pair.
space-aroundEqual space on both sides of every item, so the space at each edge is half the space between items.
space-evenlyEqual space between every pair of items and at both edges.
stretchIn a grid, stretches auto-sized tracks to fill the container. In a flex container it behaves as flex-start.
safe / unsafeA 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.

Syntaxnormal | <baseline-position> | <content-distribution> | <overflow-position>? <content-position>
Initialnormal
Applies toBlock containers, multi-column containers, flex containers and grid containers
InheritedNo
AnimationDiscrete
SupportChrome 29 · Firefox 28 · Safari 9
Block containers: Chrome 123 · Firefox 125 · Safari 17.4
Speccss-align-3
.hero { min-height: 60vh; align-content: center; }
In a plain block container, centres the content vertically without making it a flex or grid container.

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.

Syntaxnormal | stretch | <baseline-position> | <overflow-position>? <self-position>
Initialnormal
Applies toAll elements
InheritedNo
AnimationDiscrete
SupportChrome 29 · Firefox 20 · Safari 9
Speccss-align-3
ValueEffect
normalBehaves as stretch for flex items.
stretchItems with an auto cross size are stretched to fill the line.
flex-start / flex-endItems sit at the cross-start or cross-end edge of the line, following flex-wrap.
start / endItems sit at the start or end edge in the container's writing mode.
self-start / self-endEach item sits at the start or end edge in its own writing mode.
centerItems are centred in the line.
baseline, first baseline, last baselineItems are shifted so their first (or last) lines of text share a baseline.
safe / unsafeA 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.

Syntaxauto | <overflow-position>? [ normal | <self-position> ]| stretch | <baseline-position> | anchor-center
Initialauto
Applies toFlex items, grid items and absolutely positioned boxes
InheritedNo
AnimationDiscrete
SupportChrome 29 · Firefox 20 · Safari 9
Absolutely positioned boxes: Chrome 122 · Firefox 134 · Safari 26
anchor-center: Chrome 125 · Firefox 147 · Safari 26
Speccss-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.

Syntaxhorizontal | vertical | inline-axis | block-axis
Initialinline-axis
Applies toElements with display: -webkit-box or -webkit-inline-box
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix)
Speccompat
.summary {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}
Cuts the text off after three lines with an ellipsis.

-webkit-box-pack Deprecated

How a legacy box distributes its children along its direction; justify spreads them out. Use justify-content instead.

Syntaxstart | center | end | justify
Initialstart
Applies toElements with display: -webkit-box or -webkit-inline-box
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix)
Speccompat

-webkit-box-align Deprecated

How a legacy box aligns its children across its direction. Use align-items instead.

Syntaxstart | center | end | baseline | stretch
Initialstretch
Applies toElements with display: -webkit-box or -webkit-inline-box
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix)
Speccompat

-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>
Initial0
Applies toDirect children of an element with display: -webkit-box or -webkit-inline-box
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix)
Speccompat

-webkit-box-ordinal-group Deprecated

Moves a child of a legacy box earlier or later; lower groups come first. Use order instead.

Syntax<integer>
Initial1
Applies toDirect children of an element with display: -webkit-box or -webkit-inline-box
InheritedNo
AnimationDiscrete
SupportChrome 1 · Firefox 49 · Safari 3 (all with the -webkit- prefix)
Speccompat