Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Flexbox

The flex property

Reference for the CSS flex shorthand: how flex-grow, flex-shrink and flex-basis combine, what flex: 1, auto, none and initial mean, with live examples.

The flex property is the shorthand that tells a flex item how to share the space in its container. It sets three longhands at once: flex-grow (how much free space the item may take), flex-shrink (how much it gives up when space is short) and flex-basis (its starting size before growing or shrinking).

It applies to the children of an element with display: flex or display: inline-flex. On any other element it has no effect.

CSS
.item { flex: 1; }            /* grow and shrink, start from 0 */
.fixed { flex: 0 0 200px; }   /* never resize, always 200px */

Try it

Sharing the free space
auto versus 1

Syntax

CSS
flex: none
    | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ];

/* One value */
flex: 2;          /* number: flex-grow, basis becomes 0% */
flex: 10em;       /* length or percentage: flex-basis */
flex: auto;       /* 1 1 auto */
flex: initial;    /* 0 1 auto (the default) */
flex: none;       /* 0 0 auto */

/* Two values */
flex: 2 2;        /* grow shrink */
flex: 2 30px;     /* grow basis */

/* Three values */
flex: 2 2 10%;    /* grow shrink basis */
  • Initial value: 0 1 auto (the same as the longhands' initial values)
  • Applies to: flex items
  • Inherited: no
  • Animatable: yes, through its three longhands
  • Computed value: as each longhand

Values

Value Description
initial 0 1 auto. The item does not grow, may shrink, and starts from its width or content size.
auto 1 1 auto. The item starts from its normal size, then grows and shrinks.
none 0 0 auto. The item is completely inflexible.
<number> A single unitless number sets flex-grow and implies 1 0%, so the item starts from zero. flex: 1 means 1 1 0%.
<length> / <percentage> A single value with a unit sets flex-basis, and implies 1 1 for grow and shrink.
two or three values Grow, then optionally shrink, in any position relative to the basis, but the two numbers must stay together.

Usage notes

  • Prefer the shorthand. When you write flex-grow on its own, flex-shrink and flex-basis stay at their defaults. When you write flex: 1, the basis resets to 0%, which is almost always what people mean.
  • flex: 1 versus flex: auto. With 1 (basis 0%) the free space is the whole container width, so items end up with proportional widths. With auto (basis auto) content size is subtracted first, so wider content stays wider.
  • A unitless 0 is a number first. flex: 0 is read as flex-grow: 0, not as a zero basis. If you want a zero basis, write it with a unit or percentage (flex: 1 1 0px, flex: 1 1 0%).
  • Content can still force a minimum. A flex item's automatic minimum size is its content, so long words or wide images can stop it from shrinking. Set min-width: 0 (or overflow: hidden) on the item when you need it to shrink further.
  • width and flex-basis. On the main axis, flex-basis wins over width unless the basis is auto. In a column container the main axis is vertical, so it competes with height.
  • It is ignored by non-flex children. Grid items and block children do not use flex.

Browser support

Supported in every browser, including the three-value shorthand.