CSS Academy · Lesson

Flex Grow Shrink and Basis

Control how flex items grow, shrink, and set their base size using the flex shorthand.

Lesson 4 of 413 steps

Flex Grow Shrink and Basis is a free CSS Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

The flex Shorthand

The flex shorthand on flex items sets three values: flex-grow, flex-shrink, flex-basis.

<div style="display:flex; gap:8px">
    <div class="item">flex: 1 1 auto</div>
    <div style="width:80px">Fixed 80px</div>
  </div>

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.

<div style="display:flex; gap:8px">
    <div class="sidebar">Sidebar (250px basis)</div>
    <div class="main">Main (auto basis)</div>
  </div>

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.

<div style="display:flex; gap:8px">
    <div class="main">Main (grow:2)</div>
    <div class="aside">Aside (grow:1)</div>
    <div class="fixed">Fixed (grow:0)</div>
  </div>

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.

<div style="display:flex; gap:8px">
    <div class="logo">Logo</div>
    <div class="search">Search input takes remaining space</div>
  </div>

Common flex Shorthand Values

Memorize these common patterns:

  • flex: 1 = grow and shrink equally, basis 0%
  • flex: auto = flex: 1 1 auto
  • flex: 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.

<div class="equal-columns" style="display:flex; gap:8px">
    <div class="col">Column A</div>
    <div class="col">Column B</div>
    <div class="col">Column C</div>
  </div>

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:

<div class="layout">
    <div class="left-sidebar">Left</div>
    <div class="main-content">Main</div>
    <div class="right-sidebar">Right</div>
  </div>

min-width and max-width with flex

Use min-width and max-width alongside flex-grow to control growth limits:

<div style="display:flex; gap:8px">
    <div class="card">Responsive Card</div>
  </div>

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.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

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. 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. The CSS Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. Display Flex and Flex Direction
  2. Justify-Content and Align-Items
  3. Flex Wrap and Align-Content
  4. Flex Grow Shrink and Basis
← Back to CSS Academy