CSS flex Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Flexbox

What You’ll Learn

The flex property is a shorthand that controls how flex items grow, shrink, and size themselves inside a flex container. It is one of the most important tools in modern CSS layout.

01

Shorthand

grow, shrink, basis.

02

flex: 1

Equal columns.

03

Default

0 1 auto.

04

Flex items

Child elements.

05

Responsive

Adaptive layouts.

06

flex: none

Fixed-size items.

Introduction

The flex property in CSS is a shorthand property for the flex-grow, flex-shrink, and flex-basis properties. It is used to define how a flex item will grow or shrink to fit the space available in its flex container.

This property is a key component of the Flexbox layout model, which is designed to provide a more efficient way to lay out, align, and distribute space among items in a container.

Definition and Usage

Apply flex on direct children of a flex container (an element with display: flex or display: inline-flex). Use it to create equal-width columns, proportional layouts, fixed sidebars, and flexible content areas that adapt to screen size.

💡
Beginner Tip

flex: 1 is the most common value — it makes items share leftover space equally. The parent must have display: flex first.

📝 Syntax

The syntax for the flex property is:

syntax.css
element {
  flex: flex-grow flex-shrink flex-basis;
}

Each value can be specified as follows:

  • flex-grow — A number specifying how much the item will grow relative to the rest of the flex items.
  • flex-shrink — A number specifying how much the item will shrink relative to the rest of the flex items.
  • flex-basis — The initial main size of the flex item, which can be a length (e.g., 20%, 5em, 30px) or the keyword auto.

Basic Example

flex-grow.css
.item {
  flex: 1;
}
flex: 1; flex: 2; flex: auto; flex: none; flex: 0 0 200px;

Default Value

The default value of the flex property is 0 1 auto, which means:

  • flex-grow: 0 (the item will not grow).
  • flex-shrink: 1 (the item will shrink if necessary).
  • flex-basis: auto (the item will use its natural size).

Syntax Rules

  • Only works on direct children of a flex container.
  • One value like flex: 1 sets flex-grow; shrink defaults to 1 and basis to 0%.
  • Two values: flex: 1 200px means grow/shrink and basis.
  • Common keywords: auto (= 1 1 auto) and none (= 0 0 auto).
  • The property is not inherited; set it on each flex item you want to control.

⚡ Quick Reference

QuestionAnswer
Initial value0 1 auto
Applies toFlex items (direct children of flex containers)
InheritedNo
AnimatableNo
Most common valueflex: 1

💎 Property Values

The flex shorthand expands into three longhand properties:

ValueDescription
flex-growA non-negative number. Default is 0.
flex-shrinkA non-negative number. Default is 1.
flex-basisA length, percentage, or auto. Default is auto.

Common Shorthand Patterns

ShorthandExpands toTypical use
flex: 11 1 0%Equal flexible columns
flex: 22 1 0%Twice the grow share of flex: 1
flex: auto1 1 autoFlexible but respects content size
flex: none0 0 autoFixed size, no grow or shrink
flex: 0 0 200pxgrow 0, shrink 0, basis 200pxFixed sidebar width

flex vs display: flex

PropertyApplied toPurpose
display: flexParent containerCreates a flex formatting context for children
flexChild flex itemsControls how each child grows, shrinks, and sizes

👀 Live Preview

Three flex items with flex: 1, flex: 2, and flex: 1 — the middle item gets twice the space:

Item 1
Item 2
Item 3

Examples Gallery

In this example, we’ll create a simple flex container with three items and use the flex property to control their growth — plus equal columns, a fixed sidebar, and a navbar layout.

🛠 Basic Flex Item Sizing

Start with the reference example — three items with different flex grow values.

Example 1 — Proportional Flex Growth

Give the middle item twice as much grow factor as the others using flex: 2.

flex-proportional.html
<style>
  .container {
    display: flex;
  }
  .item1 { flex: 1; background: lightcoral; }
  .item2 { flex: 2; background: lightblue; }
  .item3 { flex: 1; background: lightgreen; }
</style>

<div class="container">
  <div class="item1">Item 1</div>
  <div class="item2">Item 2</div>
  <div class="item3">Item 3</div>
</div>
Try It Yourself

How It Works

Extra space is divided in a 1:2:1 ratio. Item 2 receives twice as much width as items 1 and 3 because its flex-grow value is 2.

Example 2 — Equal-Width Columns

Use flex: 1 on every column to split space evenly.

flex-equal.css
.column {
  flex: 1;
}
Try It Yourself

How It Works

All three columns have the same flex-grow value, so leftover horizontal space is shared equally.

🗃 Layout Patterns

Combine flex values for sidebars, navbars, and mixed fixed/flexible regions.

Example 3 — Fixed Sidebar with Flexible Main

Lock the sidebar width with flex: 0 0 200px and let the main area fill the rest with flex: 1.

flex-sidebar.css
.sidebar {
  flex: 0 0 200px;
}

.main {
  flex: 1;
}
Try It Yourself

How It Works

0 0 200px means no grow, no shrink, and a 200px starting width. The main area absorbs all remaining space.

Example 4 — Navbar with flex: none Logo

Keep the logo at its natural size with flex: none while navigation links share space with flex: 1.

flex-navbar.css
.logo {
  flex: none;
}

.nav-links {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
}
Try It Yourself

How It Works

The logo never grows or shrinks. The nav links container expands to push links to the right side of the bar.

♿ Accessibility

  • Maintain logical DOM order — flex changes visual order with CSS, but screen readers follow source order unless you use order carefully.
  • Do not shrink text below readable sizes — very small flex-basis values can squeeze content on mobile.
  • Test keyboard focus — reordered flex items should still receive focus in a sensible sequence.
  • Use semantic landmarks — wrap nav regions in <nav> and main content in <main>.
  • Allow wrapping on small screens — pair with flex-wrap: wrap on the container when needed.

🧠 How flex Works

1

Parent becomes a flex container

display: flex turns direct children into flex items on the main axis.

Container
2

flex-basis sets starting size

Each item gets an initial width or height along the main axis before free space is calculated.

Basis
3

Grow or shrink distributes space

Leftover space is shared by flex-grow values; overflow is reduced by flex-shrink values.

Distribution
=

Responsive flexible layout

Items adapt to container size without fixed pixel widths everywhere.

🖥 Browser Compatibility

The flex property is widely supported in modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. This broad compatibility makes Flexbox a reliable choice for layout design across different platforms and devices.

Baseline · Universal support

Flexbox everywhere

The flex shorthand is supported in all current browsers. Flexbox is a standard layout tool for modern web development.

99% Universal support
Google Chrome 29+ · Desktop & Mobile
Full support
Mozilla Firefox 28+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Microsoft Edge 12+ · All versions
Full support
Opera 17+ · Modern versions
Full support
flex property 99% supported

Bottom line: Flexbox is safe for all modern projects. Use flex: 1 confidently for responsive column layouts.

🎉 Conclusion

The flex property is an essential part of the Flexbox layout model, providing a powerful and flexible way to control the size and distribution of flex items within a container.

By understanding and utilizing the flex property, you can create responsive and dynamic layouts that adapt to different screen sizes and content variations. Experiment with different flex values to see how they affect the layout and design of your web projects.

💡 Best Practices

✅ Do

  • Set display: flex on the parent before using flex on children
  • Use flex: 1 for equal-width flexible columns
  • Use flex: 0 0 200px for fixed sidebars or tool panels
  • Combine with gap for spacing between flex items
  • Learn the longhand properties (flex-grow, flex-shrink, flex-basis) for fine control

❌ Don’t

  • Apply flex without a flex container parent
  • Assume flex: 1 and flex: auto behave identically
  • Use flex for full-page two-dimensional grids — CSS Grid may be clearer
  • Forget mobile wrapping when items cannot fit on one row
  • Rely on flex alone without testing overflow on small screens

Key Takeaways

Knowledge Unlocked

Five things to remember about flex

Use these points when building Flexbox layouts.

5
Core concepts
02

0 1 auto

Default value.

Default
📈 03

flex: 1

Equal columns.

Common
📏 04

flex: none

Fixed size.

Pattern
🗃 05

Flex items

Direct children.

Scope

❓ Frequently Asked Questions

The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis. It controls how a flex item grows, shrinks, and sets its starting size inside a flex container.
The initial value is 0 1 auto, which means the item will not grow, can shrink if needed, and uses its natural content size as the basis.
flex: 1 is shorthand for flex: 1 1 0%, meaning the item can grow and shrink equally with other flex: 1 items and starts from a zero basis so space is shared evenly.
display: flex turns an element into a flex container. flex is applied to child flex items to control how each child uses available space.
Use flex: none (0 0 auto) when an item should keep its natural size and neither grow nor shrink, such as a fixed-width sidebar icon or logo.

Practice in the Live Editor

Open the HTML editor, set display: flex on a container, and try flex: 1 and flex: 2 on child items.

HTML Editor →

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.

5 people found this page helpful