Към основното съдържание
← 🎨 CSS as a design system·Модул B2 · Урок 9
ЗадачаBuild a sidebar+main layout. .layout is display: flex. .sidebar has flex: 0 0 240px (fixed 240px). .main has flex: 1 (fills the rest). Both have padding 16px and a background (sidebar #f5f5f5, main white).

flex-grow, flex-shrink, flex-basis (при деца)

100 XP8 min
Теория

Tell flex children how to consume space

Three properties on children:

.item {
  flex-grow: 1;       /* how much of EXTRA space this gets (proportionally) */
  flex-shrink: 1;     /* how much it SHRINKS when over-budget (proportionally) */
  flex-basis: auto;   /* starting size before grow/shrink kicks in */
}

The shorthand: flex

.item { flex: 1; }              /* = flex: 1 1 0       — equal share of space */
.item { flex: 1 1 auto; }       /* equal share, but start from content size */
.item { flex: 0 0 200px; }      /* exactly 200px, never grow, never shrink */
.item { flex: none; }           /* shorthand for 0 0 auto */

Common patterns

.sidebar { flex: 0 0 240px; }   /* fixed-width sidebar */
.main { flex: 1; }              /* fill remaining space */

Sidebar stays exactly 240px wide. Main takes whatever's left.

Proportional grow

.col-a { flex-grow: 1; }
.col-b { flex-grow: 2; }
.col-c { flex-grow: 1; }

Extra space splits 1:2:1 — col-b gets twice as much as the others.

The 5-minute rule

If you're stuck wrapping your head around grow/shrink/basis, here's the rule of thumb:

  • flex: 1 on every flex child → they share space EQUALLY.
  • flex: 0 0 Npx → fixed-width that doesn't flex.
  • Combine: one fixed sidebar + flex: 1 main.
🔒

Регистрирай се за да започнеш да кодираш

Теорията е отворена за всички. Редакторът, живият преглед и проверката се отключват с 7-дневен безплатен пробен период — нужна е карта.

Регистрация — безплатен пробен период →

Първите 10 урока във всеки път са безплатни. Без карта.

← НазадСледващ урок →

Един съвет за Python или уеб на ден — на имейл

Кратко, ръчно написано, без спам. Отписване с едно кликване.