OnlineCBT Logo OnlineCBT
CSS Tutorial
CSS · LESSON 26

Flex Grow, Shrink, and Basis

By Ratan Updated: 15 Oct 2026

Control individual item sizing dynamically using flex-grow, flex-shrink, and flex-basis shorthand.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Distribute space from an initial size

The flex shorthand sets grow, shrink and basis. Basis supplies an initial main size before flexible space is distributed. Grow shares positive free space; shrink participates when items must become smaller. These numbers are factors, not direct percentages of the container.

.layout { display: flex; gap: 1rem; }
.sidebar { flex: 0 0 14rem; }
.content { flex: 1 1 0; min-width: 0; }

The sidebar neither grows nor shrinks from its 14rem basis. Content takes remaining space and can shrink. This exact arrangement still needs a responsive fallback when the viewport cannot fit the sidebar and useful content together.

Grow ratio experiment

.a { flex: 1 1 0; }
.b { flex: 2 1 0; }

With equal relevant constraints and zero bases, the items share distributable free space in a 1:2 ratio. This is not a guarantee that every outer width will be exactly one-third or two-thirds: borders, padding, gaps and minimum/maximum sizes still participate.

Shrink is scaled by size

Negative free space is distributed using the shrink factors together with the flex base sizes. Two differently sized items with shrink:1 do not necessarily lose the same number of pixels. An item's automatic minimum size can also prevent the shrinkage you expected.

Useful shorthand meanings

flex:none represents a non-flexible item. flex:auto allows growth and shrinkage with an auto basis. A common flex:1 pattern gives a flexible item with a zero-like basis, but explicit three-value syntax is easier to reason about while learning.

Practice

  1. Create two items with factors 1 and 2, then add padding and inspect the actual border-box widths.
  2. Change the bases from zero to 12rem and observe that only remaining free space is shared.
  3. Place a long unbroken string in content and compare min-width:auto with min-width:0.
  4. Add a media query that stacks the fixed sidebar above content when space becomes insufficient.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat