Sari la conținutul principal
← 🎨 CSS as a design system·Modul B4 · Lecția 2
SarcinăSet h1 to 2rem, h2 to 1.5rem, body to 1rem, .button padding to 0.5em 1em (em scales with button's font-size).

Unități de dimensionare: px vs em vs rem

75 XP6 min
Teorie

Three units, three behaviors

.a { font-size: 16px; }     /* absolute — always 16 pixels */
.b { font-size: 1em; }      /* relative to PARENT's font-size */
.c { font-size: 1rem; }     /* relative to ROOT (<html>)'s font-size */

When to use each

  • `rem` — body text, headings, anything where you want predictable scaling tied to the user's preferred font size. Default unit in 2026.
  • `em` — when you want a property to scale with the LOCAL font-size. Classic: padding: 0.5em 1em on a button — the padding grows when the button text grows.
  • `px` — when you NEED a fixed value: borders (1px solid), shadows, sub-pixel adjustments.

Why rem won

The user can override the browser's default font-size in settings (a11y feature). Sites that use px everywhere ignore it. Sites that use rem automatically scale.

By default, 1rem = 16px. If the user bumps to 20px in their browser, every rem value in your CSS scales 25%.

:root { font-size: 100%; }      /* default — respects user setting */

NEVER do :root { font-size: 14px } to "make everything smaller" — you've just broken every user's a11y preference.

em compounds in nested elements

.parent { font-size: 1.2em; }     /* 1.2 × parent → 19.2px */
.parent .child { font-size: 1.2em; }   /* 1.2 × 19.2 → 23px (compounded!) */

Nested ems multiply. Use rem to keep sizes predictable.

🔒

Înregistrează-te pentru a începe să programezi

Teoria e accesibilă tuturor. Editorul, previzualizarea live și verificarea se deblochează cu o probă gratuită de 7 zile — card necesar, anulabilă oricând.

Înregistrare — probă gratuită →

Primele 10 lecții din fiecare traseu sunt gratis. Fără card.

← AnterioarăLecția următoare →

Primește un sfat Python sau web pe zi — prin email

Scurt, scris de mână, fără spam. Dezabonare cu un click.