Flex Grow and Shrink Visualizer — See How Flexbox Distributes Space

Flex Grow and Shrink Space Distribution Visualizer · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

What's included

Features

Spec-based calculation
Flexbox 9.7 resolution with freezing.
Grow formula shown
basis + free × grow / Σgrow.
Shrink weighted by basis
shrink × basis proportions.
Fractional factor rule
Sums below 1 hand out part of the space.
Clamp and re-share
Items frozen at 0 and space redistributed.
Real browser comparison
A live flex container rendered beside the math.
Measured verification
getBoundingClientRect widths checked.
No libraries
Plain HTML, CSS and JavaScript.

About this UI Snippet

Flexbox Space Distribution — The Math Behind flex-grow and flex-shrink

Screenshot of the Flex Grow and Shrink Space Distribution Visualizer snippet rendered live

Most people learn that flex-grow: 2 means "gets twice as much". That's only half true, and flex-shrink works differently again, which is why flex layouts sometimes do surprising things. This visualizer calculates the distribution by hand, following the algorithm in the CSS Flexbox specification, and renders a real flex container next to it so you can check the math against the browser.

Start from the bases

Every item starts at its flex-basis. Adding them up and comparing with the container width gives either positive free space (the container is bigger) or negative free space (the content overflows).

Growing: split the leftover space

With positive free space, each item gets a share proportional to its flex-grow: basis + free × grow / Σgrow. So flex-grow: 2 means twice the share of the *extra* space, not twice the size. An item with grow 0 keeps its basis. If the grow factors add up to less than 1, only that fraction of the free space is handed out.

Shrinking: weighted by size

With negative free space, the reduction is shared in proportion to flex-shrink × flex-basis, not flex-shrink alone. A 400px item with shrink 1 gives up twice as much as a 200px item with shrink 1. This rule prevents small items from collapsing to nothing before large ones have shrunk at all.

Freezing and re-sharing

If the calculation pushes an item below zero, the spec freezes it at its minimum and runs the distribution again among the remaining items. Try a high flex-shrink on a small item in a narrow container to see an item clamped and the others adjusted.

Why min-width: 0

Real flex items default to min-width: auto, which stops them shrinking below their content. The demo sets min-width: 0 on the real items so the browser uses the same pure formula as the math. In your own layouts, that default is often why an item refuses to shrink.

Checked against the browser

The browser's measured widths are printed under the explanation with a match indicator.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this visualizer into an AI assistant like Claude and ask it to explain, with your current numbers, why flex-shrink is weighted by flex-basis. Ask it to add max-width constraints (which also freeze items), min-width: auto behaviour using real text content, gaps between items, or a mode for flex-basis: 0 versus auto. It can also help debug a real layout by modelling your items here first.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a flexbox space distribution visualizer in plain HTML, CSS and JavaScript.

Requirements:
- A container width slider and three items, each with editable flex-basis, flex-grow and flex-shrink.
- Calculate each item's final width by hand following the CSS Flexbox flexible length algorithm with min sizes of 0: positive free space is shared by flex-grow (handing out only the fraction equal to the sum of grow factors if it is below 1); negative free space is removed in proportion to flex-shrink × flex-basis; items that would go below 0 are frozen at 0 and the remaining space is re-shared.
- Draw the calculated widths as positioned bars, and below them render a real flex container of the same width whose items use the same flex values with min-width: 0.
- Measure the real items with getBoundingClientRect and show whether they match the calculation.
- Explain the result in words with the actual numbers and formula used, including any clamped items.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="fx">
  <div class="fx-top">
    <h2>Where does the space go? flex-grow vs flex-shrink</h2>
    <p>The math below is calculated by hand and checked against the browser's real layout of the same flex container.</p>
  </div>
  <label class="fx-width">Container width <input type="range" id="fxW" min="200" max="760" value="620"><output id="fxWOut"></output></label>
  <div class="fx-items" id="fxItems"></div>
  <div class="fx-stage">
    <div class="fx-label">Your math</div>
    <div class="fx-calc" id="fxCalc"></div>
    <div class="fx-label">The browser</div>
    <div class="fx-real" id="fxReal"></div>
  </div>
  <div class="fx-explain" id="fxExplain" aria-live="polite"></div>
</div>

Step by step

How to Use

  1. 1
    Drag the container widthSwitch between growing and shrinking by making it larger or smaller than the bases.
  2. 2
    Edit each itemChange flex-basis, flex-grow and flex-shrink.
  3. 3
    Read the mathThe explanation shows the formula with your numbers.
  4. 4
    CompareThe real flex container below should match the calculated one.
  5. 5
    Force a clampGive a small item a large shrink in a narrow container.

Real-world uses

Common Use Cases

Learning flexbox properly
Understand the numbers, not just the effect.
Debugging layouts
Why a sidebar shrinks more than expected.
Design system work
Choose sensible flex values for components.
Teaching CSS
Demonstrate grow versus shrink live.
Interview preparation
A common front-end question.
Related: Flexbox Alignment Visualizer
Alignment rather than sizing: Flexbox Alignment Visualizer.
Related: CSS Grid Template Areas Visualizer

Got questions?

Frequently Asked Questions

The free space (container size minus the sum of flex-basis values) is divided in proportion to each item's flex-grow value and added to its basis. flex-grow: 2 gets twice the share of the extra space, not twice the final size.

When items overflow, the overflow is removed in proportion to flex-shrink multiplied by flex-basis. Larger items therefore shrink more than smaller items with the same flex-shrink value.

Flex items default to min-width: auto, which prevents them from shrinking below their minimum content size. Set min-width: 0 (or overflow: hidden) on the item to allow it to shrink further.

Only that fraction of the free space is distributed. For example, two items with flex-grow 0.25 each receive half of the free space between them, and the rest stays empty.

It is shorthand for flex-grow: 1, flex-shrink: 1 and flex-basis: 0%. With a zero basis, all space is free space, so items with flex: 1 end up equal in size regardless of content (subject to minimum sizes).