Przejdź do treści głównej
← 🎨 CSS as a design system·Moduł B3 · Lekcja 3
ZadanieBuild a grid with columns 1fr 3fr 1fr (narrow, wide, narrow). .grid has display: grid, grid-template-columns: 1fr 3fr 1fr, gap: 12px. Three .cell divs.

Jednostka fr: przestrzeń ułamkowa

75 XP6 min
Teoria

fr = a unit that means "share of available space"

grid-template-columns: 1fr 1fr;          /* two equal columns */
grid-template-columns: 1fr 2fr;          /* second column is 2x first */
grid-template-columns: 1fr 1fr 1fr 1fr;  /* four equal columns */
grid-template-columns: repeat(4, 1fr);   /* same — shorthand */

After fixed-pixel tracks reserve their space, fr units split what's left. So:

grid-template-columns: 200px 1fr 1fr;

With a 1000px container: 200px goes to col 1. The remaining 800px splits into two 400px columns.

fr beats % when you have gaps

Compare:

/* % math: each col is 50% — but gap is INSIDE the container, so children overflow */
grid-template-columns: 50% 50%;
gap: 16px;

/* fr math: 1fr accounts for the gap. Container width = col1 + gap + col2 */
grid-template-columns: 1fr 1fr;
gap: 16px;

With %, the math is wrong when gaps exist. With fr, it's correct. Use fr for grids.

Decimal fractions

grid-template-columns: 0.5fr 1fr 0.5fr;  /* narrow, wide, narrow */

Works. The shares are 0.5 + 1 + 0.5 = 2 total parts; the wide column gets half (1/2), each narrow gets a quarter.

🔒

Zarejestruj się, aby zacząć kodować

Teoria jest dostępna dla każdego. Edytor, podgląd na żywo i check odblokowują się z 7-dniowym darmowym okresem próbnym — wymagana karta, anuluj w dowolnym momencie.

Zarejestruj się — darmowy okres próbny →

Pierwsze 10 lekcji w każdej ścieżce jest darmowych. Karta nie jest wymagana.

← PoprzedniaNastępna lekcja →

Codziennie jedna wskazówka Python lub web — na maila

Krótko, ręcznie pisane, bez spamu. Wypisanie w jedno kliknięcie.