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.
Fundamentals
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.
Foundation
📝 Syntax
The syntax for the flex property is:
syntax.css
element{flex:flex-growflex-shrinkflex-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.
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.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Initial value
0 1 auto
Applies to
Flex items (direct children of flex containers)
Inherited
No
Animatable
No
Most common value
flex: 1
Reference
💎 Property Values
The flex shorthand expands into three longhand properties:
Value
Description
flex-grow
A non-negative number. Default is 0.
flex-shrink
A non-negative number. Default is 1.
flex-basis
A length, percentage, or auto. Default is auto.
Common Shorthand Patterns
Shorthand
Expands to
Typical use
flex: 1
1 1 0%
Equal flexible columns
flex: 2
2 1 0%
Twice the grow share of flex: 1
flex: auto
1 1 auto
Flexible but respects content size
flex: none
0 0 auto
Fixed size, no grow or shrink
flex: 0 0 200px
grow 0, shrink 0, basis 200px
Fixed sidebar width
Context
flex vs display: flex
Property
Applied to
Purpose
display: flex
Parent container
Creates a flex formatting context for children
flex
Child flex items
Controls how each child grows, shrinks, and sizes
Preview
👀 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
Hands-On
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.
The logo never grows or shrinks. The nav links container expands to push links to the right side of the bar.
A11y
♿ 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.
Compatibility
🖥 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 Chrome29+ · Desktop & Mobile
Full support
Mozilla Firefox28+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Microsoft Edge12+ · All versions
Full support
Opera17+ · Modern versions
Full support
flex property99% supported
Bottom line: Flexbox is safe for all modern projects. Use flex: 1 confidently for responsive column layouts.
Wrap Up
🎉 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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about flex
Use these points when building Flexbox layouts.
5
Core concepts
🛠01
Shorthand
grow + shrink + basis.
Purpose
⚙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.