Flex Grow Shrink and Basis
Control how flex items grow, shrink, and set their base size using the flex shorthand.
Flex Grow Shrink and Basis is a free CSS Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The flex Shorthand
The flex shorthand on flex items sets three values: flex-grow, flex-shrink, flex-basis.
.item {
flex: 1 1 auto;
/* grow:1 shrink:1 basis:auto */
}flex-basis
flex-basis sets the initial size of a flex item before growing or shrinking. It overrides width (in row direction). auto uses the item's width or content size.
.sidebar { flex-basis: 250px; }
.main { flex-basis: auto; }flex-grow
flex-grow determines how much an item grows relative to siblings when free space is available. A value of 0 means no growth. Higher values receive proportionally more space.
.main { flex-grow: 2; } /* gets 2x more free space */
.aside { flex-grow: 1; } /* gets 1x free space */
.fixed { flex-grow: 0; } /* no growth */flex-grow Example
If a container has 300px of free space and two items with flex-grow 1 and 2: the first gets 100px extra, the second gets 200px extra (1:2 ratio).
flex-shrink
flex-shrink controls how much an item shrinks when there is not enough space. Default is 1 (all items shrink equally). 0 prevents shrinking.
.logo {
flex-shrink: 0; /* never shrinks below flex-basis */
width: 120px;
}
.search {
flex-grow: 1; /* takes remaining space */
flex-shrink: 1;
}Common flex Shorthand Values
Memorize these common patterns:
flex: 1= grow and shrink equally, basis 0%flex: auto= flex: 1 1 autoflex: none= flex: 0 0 auto (rigid)flex: 0 0 200px= fixed 200px, no grow/shrink
flex: 1 — Equal Distribution
Setting flex: 1 on all children divides available space equally. Basis becomes 0%, so items grow from nothing and divide space proportionally.
.equal-columns > * {
flex: 1; /* all columns equal width */
}flex-basis vs width
flex-basis takes precedence over width in the main axis direction. If you set both, flex-basis wins (unless flex-basis is auto, which then defers to width).
Intrinsic vs Extrinsic Sizing
When flex-basis: 0 (as in flex: 1), the item starts from 0 and grows proportionally — true proportional distribution. With flex-basis: auto, items start from their natural content size then grow.
The Holy Trinity Pattern
A classic fixed sidebar + flexible main + fixed sidebar layout:
.layout {
display: flex;
}
.left-sidebar {
flex: 0 0 200px; /* fixed */
}
.main-content {
flex: 1; /* fills remaining space */
}
.right-sidebar {
flex: 0 0 160px; /* fixed */
}min-width and max-width with flex
Use min-width and max-width alongside flex-grow to control growth limits:
.card {
flex: 1 1 280px;
min-width: 200px;
max-width: 400px;
}Quick Check
An item has flex: 0 0 200px. What does this mean?
Recap
flex-grow distributes extra space proportionally. flex-shrink controls how items compress. flex-basis sets the starting size. The shorthand flex: 1 enables equal space distribution. Use flex: 0 0 Xpx for fixed-size items and flex: 1 for flexible items in the same container.
Frequently asked questions
Is the “Flex Grow Shrink and Basis” lesson free?
Yes — the full text of “Flex Grow Shrink and Basis” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.
What will I learn in “Flex Grow Shrink and Basis”?
Control how flex items grow, shrink, and set their base size using the flex shorthand. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Flex Grow Shrink and Basis” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Display Flex and Flex Direction
- Justify-Content and Align-Items
- Flex Wrap and Align-Content
- Flex Grow Shrink and Basis