CSS flex Property

The flex property is a shorthand that controls how a flex item grows, shrinks, and determines its initial size within a flex container.

It combines the flex-grow, flex-shrink, and flex-basis properties into one declaration. This makes it easier to distribute available space, create flexible columns, and build responsive layouts without assigning fixed widths to every item.

flex Syntax

The flex property accepts keywords, numbers, basis values, or combinations of values that define the three flex sizing components.

Basic Syntax

selector {
  flex: 1;
}

Keyword Values

.item {
  flex: auto;
}
.item {
  flex: none;
}
.item {
  flex: initial;
}

Single-Value Syntax

.item {
  flex: 2;
}
.item {
  flex: 200px;
}
.item {
  flex: 30%;
}

Two-Value Syntax

.item {
  flex: 2 1;
}
.item {
  flex: 2 200px;
}

Three-Value Syntax

.item {
  flex: 2 1 200px;
}

CSS-Wide Keywords

.item {
  flex: inherit;
}
.item {
  flex: unset;
}
.item {
  flex: revert;
}
.item {
  flex: revert-layer;
}

The CSS-wide keyword initial is also supported. It resets the shorthand to its initial longhand values, equivalent to flex: 0 1 auto.

When using the three-value syntax, the values appear in this order: flex-grow, flex-shrink, and flex-basis.

Property Values

The flex property provides several convenient ways to define flexible sizing behavior.

Value Description
auto Allows the item to grow and shrink using an automatic basis. Equivalent to 1 1 auto.
none Prevents flexible growth and shrinking. Equivalent to 0 0 auto.
initial Uses the initial longhand values. Equivalent to 0 1 auto.
<number> Sets the growth factor, with a shrink factor of 1 and a zero basis.
<length> Sets the basis to a length, with growth and shrink factors of 1.
<percentage> Sets the basis to a percentage of the flex container's inner main size when that size is definite.
<number> <number> Specifies growth and shrink factors with a zero basis.
<number> <basis> Specifies a growth factor and basis, with a shrink factor of 1.
<number> <number> <basis> Explicitly specifies all three flex components.

CSS-Wide Values

Value Description
inherit Inherits the three flex component values from the parent.
initial Resets the components to 0 1 auto.
unset Resets the components to their initial values because they are not inherited.
revert Reverts the shorthand according to the CSS cascade origins.
revert-layer Reverts the shorthand according to the CSS cascade layers.

Property Characteristics

Characteristic Value
Initial value 0 1 auto
Inherited No
Applies to Flex items
Computed value See individual longhand properties
Animation type See individual longhand properties
Percentages Supported for flex-basis

The shorthand applies to flex items. A parent element must establish a flex formatting context using display: flex or display: inline-flex for its direct children to participate as flex items.

Shorthand Properties

The flex shorthand controls three individual properties.

Property Purpose Initial Value
flex-grow Controls how the item receives positive free space. 0
flex-shrink Controls how the item shrinks when necessary. 1
flex-basis Defines the initial main size used by the flex layout algorithm. auto

Using Individual Properties

.item {
  flex-grow: 2;
  flex-shrink: 1;
  flex-basis: 200px;
}

Equivalent Shorthand

.item {
  flex: 2 1 200px;
}

Common Shorthand Expansions

Shorthand Equivalent Values
flex: initial 0 1 auto
flex: auto 1 1 auto
flex: none 0 0 auto
flex: 1 1 1 0 (specified expansion)
flex: 2 1 2 1 0 (specified expansion)
flex: 2 200px 2 1 200px

Browser note: Browsers commonly expand an omitted zero basis to 0% rather than the specification's 0. These values usually behave alike in definite-sized containers, but can differ when the container's main size is indefinite.

Using the shorthand is generally recommended because it establishes all three components together and avoids unintended combinations of previously declared longhand values.

How It Works

The flex property works with the flex layout algorithm to distribute available space among flex items.

Step 1: Create a Flex Container

.container {
  display: flex;
}

Step 2: Define Flexible Items

.item {
  flex: 1;
}

Each item receives the same growth factor and starts from a zero flex basis.

Step 3: Assign Different Growth Factors

.item-one {
  flex: 1;
}
.item-two {
  flex: 2;
}
.item-three {
  flex: 1;
}

When the items have equal zero bases and no minimum-size restrictions interfere, the second item receives twice as much main-axis space as either of the other items.

Step 4: Set an Initial Basis

.item {
  flex: 1 1 200px;
}

Each item begins with a flex basis of 200px, then grows or shrinks according to the available space and its flex factors.

Important Distinction

The flex property controls an item's size along the main axis. For flex-direction: row, this is normally horizontal. For flex-direction: column, it is vertical.

flex: auto

The value auto allows a flex item to grow and shrink while using its automatic basis.

Syntax

.item {
  flex: auto;
}

Equivalent Declaration

.item {
  flex: 1 1 auto;
}

How It Works

The item uses its automatic main size as its flex basis. It can then receive positive free space or shrink when space is insufficient.

Example

.container {
  display: flex;
  gap: 10px;
}
.item {
  flex: auto;
}

Items can expand to fill the container while retaining the influence of their initial content sizes.

Important Consideration

Unlike flex: 1, flex: auto does not start every item from a zero basis. Items with different content sizes may therefore receive different final widths.

flex: none

The value none prevents a flex item from growing or shrinking through the flex sizing algorithm.

Syntax

.item {
  flex: none;
}

Equivalent Declaration

.item {
  flex: 0 0 auto;
}

Example

.sidebar {
  flex: none;
  width: 220px;
}
.main-content {
  flex: 1;
}

How It Works

The sidebar retains its specified width rather than growing or shrinking in response to the available flex space.

Important Consideration

flex: none does not guarantee that an item will fit inside its container. A nonshrinking item can overflow when there is insufficient space.

flex: initial

The value initial restores the default flex sizing behavior.

Syntax

.item {
  flex: initial;
}

Equivalent Declaration

.item {
  flex: 0 1 auto;
}

How It Works

The item does not grow to consume positive free space, but it can shrink when necessary.

Example

.container {
  display: flex;
  gap: 10px;
}
.item {
  flex: initial;
}

Items generally retain their automatic main sizes unless shrinking is needed to fit the container.

Important Consideration

This value is useful when an item should keep its natural size without expanding into unused space.

flex: number

A single nonnegative number sets the growth factor while assigning a shrink factor of 1 and a zero flex basis.

Syntax

.item {
  flex: 1;
}

Examples

.small-item {
  flex: 1;
}
.large-item {
  flex: 2;
}

How It Works

When both items start from the same zero basis, their growth factors determine the proportions in which positive free space is distributed.

Equal Distribution

.container {
  display: flex;
}
.item {
  flex: 1;
}

Items can share the available main-axis space equally, provided minimum sizes and other sizing constraints do not prevent it.

Important Consideration

Growth factors are ratios, not fixed percentages. flex: 2 does not automatically mean an item occupies 200% of its container or twice the width of every other item in all circumstances.

Flex Basis Values

The third component of the flex shorthand defines the initial main size used before the flex layout algorithm distributes free space.

Fixed Basis

.item {
  flex: 1 1 200px;
}

Percentage Basis

.item {
  flex: 1 1 30%;
}

Automatic Basis

.item {
  flex: 1 1 auto;
}

Content-Based Basis

.item {
  flex: 1 1 content;
}

Comparison

Basis Behavior
0 Starts from a zero basis before distributing free space.
auto Uses the item's main-size property when definite, or falls back to content-based sizing.
200px Starts with a basis of 200 pixels.
30% Uses 30% of the container's inner main size when definite.
content Uses the item's content-based size.

Important Consideration

The flex basis is a starting point, not necessarily the final rendered size. Growth, shrinking, minimum sizes, and maximum sizes can change the result.

Growing and Shrinking

Flex items can expand when extra space is available or shrink when their initial sizes exceed the available space.

Growing Items

.item-one {
  flex: 1 1 100px;
}
.item-two {
  flex: 2 1 100px;
}

When positive free space exists, the second item receives twice the growth allocation of the first item, assuming no sizing constraints interfere.

Shrinking Items

.item-one {
  flex: 0 1 200px;
}
.item-two {
  flex: 0 2 200px;
}

When negative free space exists, the second item has a larger shrink factor and may shrink more than the first.

Preventing Shrinking

.fixed-item {
  flex: 0 0 200px;
}

Allowing Flexible Sizing

.flexible-item {
  flex: 1 1 200px;
}

Important Consideration

Negative free space is distributed using scaled shrink factors, which account for both flex-shrink and the flex base size. Shrinking is also limited by applicable minimum and maximum size constraints.

Equal-Width Columns

A common use of flex is creating columns that share the available width equally.

HTML Example

<div class="equal-columns">
  <div class="column">Column 1</div>
  <div class="column">Column 2</div>
  <div class="column">Column 3</div>
</div>

CSS Example

.equal-columns {
  display: flex;
  gap: 10px;
}
.equal-columns .column {
  flex: 1;
  min-width: 0;
  padding: 15px;
  background-color: #e8f1fa;
  text-align: center;
}

How It Works

Each column uses the same growth factor. Setting min-width: 0 allows columns to shrink below their automatic content-based minimum width when needed.

Important Consideration

Equal flex factors do not guarantee identical widths when minimum sizes, maximum sizes, margins, or other constraints differ between items.

Unequal-Width Columns

Different growth factors can be used to create columns with proportional widths.

HTML Example

<div class="unequal-columns">
  <div class="sidebar">Sidebar</div>
  <div class="main">Main Content</div>
</div>

CSS Example

.unequal-columns {
  display: flex;
  gap: 15px;
}
.unequal-columns .sidebar {
  flex: 1;
  min-width: 0;
  padding: 15px;
  background-color: #dceaf7;
}
.unequal-columns .main {
  flex: 3;
  min-width: 0;
  padding: 15px;
  background-color: #e8f1fa;
}

How It Works

The main content receives three parts of the distributable space for every one part received by the sidebar, assuming their zero bases and sizing constraints permit this distribution.

Important Consideration

The gap value is removed from the space available to the flex items before free space is distributed.

Responsive Layouts

The flex property can be combined with flex-wrap to create layouts that adapt to available space.

HTML Example

<div class="responsive-cards">
  <article class="card">Card 1</article>
  <article class="card">Card 2</article>
  <article class="card">Card 3</article>
</div>

CSS Example

.responsive-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}
.responsive-cards .card {
  flex: 1 1 220px;
  min-width: 0;
  padding: 20px;
  background-color: #e8f1fa;
}

How It Works

Each card starts with a flex basis of 220px. Cards can grow to fill available space, shrink when appropriate, or wrap onto additional lines when the container cannot accommodate them.

Using a Media Query

@media (max-width: 600px) {
  .responsive-cards .card {
    flex: 1 1 100%;
  }
}

At narrower viewport widths, each card requests a full-width basis and typically occupies its own row.

Important Consideration

Wrapping is controlled by the flex container's flex-wrap property, not by the flex shorthand itself.

flex vs. width

The flex property and the width property can both influence an item's size, but they serve different purposes.

Using width

.item {
  width: 200px;
}

The width property provides a preferred horizontal size. A flex item may still grow or shrink according to its flex settings.

Using flex-basis

.item {
  flex: 1 1 200px;
}

The item starts with a flex basis of 200px along the main axis, then participates in flexible sizing.

Comparison

Behavior width flex
Primary purpose Specifies horizontal size Controls flexible main-axis sizing
Growth control No direct control Uses flex-grow
Shrink control No direct control Uses flex-shrink
Initial sizing Sets a preferred width Uses flex-basis
Main-axis awareness Always horizontal Follows flex-direction

Important Consideration

When flex-basis has a value other than auto, it generally takes precedence over the main-size property when determining the flex base size.

Minimum and Maximum Sizing

Minimum and maximum size properties can limit how far flex items grow or shrink.

Minimum Width

.item {
  flex: 1;
  min-width: 150px;
}

Maximum Width

.item {
  flex: 1;
  max-width: 400px;
}

Allowing Content to Shrink

.item {
  flex: 1;
  min-width: 0;
}

Handling Long Content

.item {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

Vertical Flex Layouts

.container {
  display: flex;
  flex-direction: column;
  height: 400px;
}
.content {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

In a column flex container, min-height: 0 can allow a flexible content area to shrink and scroll within the available height.

Important Consideration

Flex items have an automatic minimum size that can prevent them from shrinking as expected. Setting min-width: 0 or min-height: 0, depending on the main axis, is often useful when content needs to fit within a constrained layout.

flex Example

This example compares equal growth factors, proportional growth factors, and a fixed sidebar with flexible main content.

HTML Example

<div class="flex-demo">
  <h3>Equal Columns</h3>
  <div class="flex-row equal">
    <div>1</div>
    <div>1</div>
    <div>1</div>
  </div>
  <h3>Proportional Columns</h3>
  <div class="flex-row proportional">
    <div>1</div>
    <div>2</div>
    <div>1</div>
  </div>
  <h3>Fixed and Flexible</h3>
  <div class="flex-row mixed">
    <div class="sidebar">Sidebar</div>
    <div class="main">Main Content</div>
  </div>
</div>

CSS Example

.flex-demo .flex-row {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
}
.flex-demo .flex-row > div {
  min-width: 0;
  padding: 15px 8px;
  background-color: #dceaf7;
  text-align: center;
}
.flex-demo .equal > div {
  flex: 1;
}
.flex-demo .proportional > div {
  flex: 1;
}
.flex-demo .proportional > div:nth-child(2) {
  flex: 2;
}
.flex-demo .mixed .sidebar {
  flex: 0 0 100px;
}
.flex-demo .mixed .main {
  flex: 1;
}
Browser Result

Equal Columns

1
1
1

Proportional Columns

1
2
1

Fixed and Flexible

Main Content

The first row distributes available space equally. The second row gives the middle item twice the growth factor of the other items. The third row keeps the sidebar at a fixed flex basis while allowing the main content to occupy the remaining space.

These layouts demonstrate how the flex shorthand can replace multiple individual sizing declarations.

Accessibility

The flex property changes the visual sizing of elements but does not change their semantic meaning or document order.

Preserve Logical Source Order

Arrange HTML content in a meaningful reading order. Flex sizing should not be used to compensate for confusing document structure.

Avoid Content Clipping

Ensure that flex items can accommodate long text, enlarged fonts, and translated content without hiding important information.

Support Text Resizing

Test layouts with increased text sizes. Use flexible sizing, wrapping, and appropriate minimum-size constraints to prevent overlap.

Maintain Usable Controls

Buttons, links, and form controls should remain large enough to identify and operate when flex items shrink.

Responsive Reading Order

When a layout changes from horizontal to vertical, ensure the resulting sequence remains understandable for keyboard and assistive technology users.

Important Consideration

Flexbox changes visual layout, not the underlying accessibility tree. Keep source order logical and verify that content remains readable at different viewport sizes.

Important Considerations

Keep the following points in mind when using flex:

  • Purpose: Controls growth, shrinking, and initial main-axis sizing of flex items.
  • Shorthand: Combines flex-grow, flex-shrink, and flex-basis.
  • Initial value: 0 1 auto.
  • Inheritance: The property is not inherited.
  • Flex container: The parent must establish a flex formatting context.
  • Main axis: Sizing follows the direction established by flex-direction.
  • flex: auto: Equivalent to 1 1 auto.
  • flex: none: Equivalent to 0 0 auto.
  • flex: initial: Equivalent to 0 1 auto.
  • flex: 1: Uses a growth factor of 1, a shrink factor of 1, and a zero basis.
  • Growth factors: Determine how positive free space is distributed.
  • Shrink factors: Help determine how negative free space is distributed.
  • Flex basis: Establishes the starting main size before flexible sizing.
  • Minimum sizes: Automatic minimum sizes can prevent expected shrinking.
  • Maximum sizes: Can limit flexible growth.
  • Equal columns: Commonly use flex: 1.
  • Proportional columns: Use different growth factors.
  • Fixed items: Can use flex: none or flex: 0 0 <length>.
  • Responsive layouts: Combine flex sizing with flex-wrap and media queries.
  • Wrapping: Controlled by the container, not by the flex shorthand.
  • Gap: Reduces the space available for flex item sizing.
  • Browser behavior: Omitted zero bases commonly expand to 0% in browsers.
  • Accessibility: Preserve meaningful source order and prevent content clipping.

Use the flex shorthand when defining flexible item sizing. It provides a consistent way to control all three flex components and is particularly useful for columns, sidebars, navigation layouts, and responsive content areas.

Browser Support

The CSS flex property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.

The standard, unprefixed flex shorthand has broad support. Older browser versions may have used prefixed or earlier Flexbox implementations.

Supported Example

.container {
  display: flex;
}
.item {
  flex: 1;
}

Feature Detection

@supports (display: flex) and (flex: 1) {
  .container {
    display: flex;
  }
  .item {
    flex: 1;
  }
}

Compatibility Considerations

Although Flexbox is widely supported, layouts can behave differently when minimum content sizes, percentage flex bases, or indefinite container dimensions are involved.

For current compatibility information, visit Can I Use? .

Specifications

The flex property is defined in the CSS Flexible Box Layout Module Level 1, which specifies the flex formatting context, flexible sizing algorithm, and the three flex shorthand components.

CSS Flexible Box Layout Module Level 1: flex Property

The specification also defines how positive and negative free space are distributed among flex items.

CSS Flexible Box Layout Module Level 1: Flex Layout Algorithm

These definitions establish the behavior of flex growth, shrinking, basis calculations, and sizing constraints.