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.
.item { flex: 1; } /* grow and shrink, start from 0 */ .fixed { flex: 0 0 200px; } /* never resize, always 200px */
Try it
Syntax
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-growon its own,flex-shrinkandflex-basisstay at their defaults. When you writeflex: 1, the basis resets to0%, which is almost always what people mean. flex: 1versusflex: auto. With1(basis0%) the free space is the whole container width, so items end up with proportional widths. Withauto(basisauto) content size is subtracted first, so wider content stays wider.- A unitless
0is a number first.flex: 0is read asflex-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(oroverflow: hidden) on the item when you need it to shrink further. widthandflex-basis. On the main axis,flex-basiswins overwidthunless the basis isauto. In a column container the main axis is vertical, so it competes withheight.- 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.
Related
flex-grow,flex-shrink,flex-basisโ the longhandsflex-directionโ defines which axis is the main axisorder- Lessons: Flex item properties, Flexbox