CSS flex Property

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis. It sets how a flex item grows or shrinks to fit space in its flex container.

Apply it to children of a display: flex (or inline-flex) container. For most layouts, MDN recommends auto, initial, none, or a positive unitless number such as 1.

Baseline · Widely available.

Not deprecated. Prefer the flex shorthand over setting the three longhands separately unless you need only one of them.

CSS Syntax

syntax.css
flex: none | [ <grow> <shrink>? || <basis> ];

One value: a grow number (flex: 1 → 1 1 0%) or a basis (flex: 10em → 1 1 10em). Two or three values set grow, shrink, and/or basis in order.

Property Values

ValueDescriptionTry it
initial0 1 auto — no grow, can shrink, content-sized basisTry it
auto1 1 auto — can grow and shrink from content sizeTry it
none0 0 auto — fixed; neither grows nor shrinksTry it
1 (number)1 1 0% — share free space evenly with siblingsTry it
2 / ratiosHigher grow factor takes a larger share of free spaceTry it
1 30pxGrow from a 30px basis (shrink defaults to 1)Try it
1 1 100pxFull three-value form: grow, shrink, basisTry it
inheritTakes the parent's flex valueTry it

How to Use flex

Make a flex container, then size the items with flex.

  1. Set display: flex on the parent.
  2. Pick a common item value: 1, auto, none, or initial.
  3. Use different numbers (2 vs 1) when one item should take more free space.
  4. Add min-width: 0 (or min-height: 0) if an item refuses to shrink below its content.
PieceRequired?Role
Flex containerYesdisplay: flex or inline-flex on the parent
flex on itemsTypicalControls grow, shrink, and basis per item
Free spaceTo see growContainer wider/taller than content sum
InheritedNoSet on each flex item that needs it

Live Preview

Change flex on the pink item. The other two stay at flex: 1.

Siblings use flex: 1. Compare how the target shares the row.

Flex row
Target 1 1

flex: 1

CSS Examples

Each snippet focuses on flex. Use Try It Yourself for a live editor.

Example 1 — Equal Columns

Share free space evenly with flex: 1.

CSS
<style>
  .row { display: flex; }
  .row > * { flex: 1; }
</style>

<div class="row">
  <span>A</span><span>B</span><span>C</span>
</div>
Try It Yourself

How It Works

  1. Make a flex row. display: flex on the parent.
  2. Set flex: 1. Expands to 1 1 0%.
  3. Equal shares. Each item gets the same fraction of free space.
  4. Basis is 0%. Sizing comes from growth, not content width first.

Example 2 — Fixed Item

Keep one item from growing or shrinking.

CSS
<style>
  .row { display: flex; }
  .grow { flex: 1; }
  .fixed { flex: none; width: 4rem; }
</style>

<div class="row">
  <span class="grow">Grow</span>
  <span class="fixed">Fix</span>
</div>
Try It Yourself

How It Works

  1. flex: none means 0 0 auto.
  2. Width stays put. The fixed item uses its width and does not absorb free space.
  3. The sibling grows. flex: 1 takes the leftover space.
  4. Good for logos and controls that must keep a steady size.

Example 3 — Grow Ratios

Give one item twice the free space of another.

CSS
<style>
  .row { display: flex; }
  .a { flex: 2; }
  .b { flex: 1; }
</style>

<div class="row">
  <span class="a">2</span>
  <span class="b">1</span>
</div>
Try It Yourself

How It Works

  1. Both start from 0% basis when you use a bare number.
  2. Grow factors split free space. 2 gets twice as much as 1.
  3. Total parts = 3. First item ≈ 2/3, second ≈ 1/3 of the free space.
  4. Resize the window in the editor to see the ratio hold.

Edge Cases & Pitfalls

If items will not grow or shrink as expected, check these first.

parent

Needs a flex container

flex only applies to flex items. Set display: flex on the parent first.

min

Items may not shrink below min-content

By default, items resist shrinking below their content. Use min-width: 0 (or min-height: 0) to allow tighter shrink.

shorthand

Omitted values are not always the initials

flex: 1 is 1 1 0%, not 1 1 auto. Prefer the shorthand values MDN lists for predictable results.

vs

flex vs width

A set width can fight with flex-basis. Prefer flex for flexible sizing; use width mainly with flex: none.

order

Visual order is not DOM order

order can rearrange items visually. Keep DOM order sensible for keyboard and screen-reader flow.

Browser Support

flex is a browser CSS property for flex items. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

flex

Supported in all modern browsers. Older Internet Explorer needs the 2012 flexbox syntax in some cases.

97% Modern browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Opera All modern versions
Full support
Samsung Internet Supported
Full support
Internet Explorer IE 11 (partial / older syntax)
Partial
flex Widely supported

Bottom line: Safe for modern flex layouts. Prefer the flex shorthand for grow/shrink/basis. It is not a runtime API.

Key Takeaways

  • Shorthand for grow, shrink, and basis on flex items.
  • Common picks: 1, auto, none, initial.
  • flex: 1 → 1 1 0% for even flexible columns.
  • Parent first: display: flex, then flex on children.

One line: use flex on items to decide who grows, who shrinks, and from what starting size.

Frequently Asked Questions

It is a shorthand for flex-grow, flex-shrink, and flex-basis. It controls how a flex item grows, shrinks, and what size it starts from inside a flex container.
The initial value is 0 1 auto: the item does not grow, can shrink, and uses its content or width/height as the basis.
A unitless number expands to that grow factor with shrink 1 and basis 0%. So flex: 1 means 1 1 0%, which shares free space evenly with other similar items.
display: flex makes a flex container. The flex shorthand is set on flex items (the children) to control how each item uses space.
Use none (0 0 auto) when an item should keep its natural size and neither grow nor shrink, such as a logo or fixed control.
It is Baseline Widely available. It is a browser CSS layout property, not a Node.js runtime feature.

Next: flex-basis

Set the starting main size of a flex item.

flex-basis tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful