Lesson 41 +10 XP

Units

CSS Units

Absolute units (fixed sizes)

  • px (1/96th of an inch), the most common.
  • in, cm, mm, pt, pc.

Pixels rule the web for exact sizes.

Relative units

UnitRelative toUses
%the parentwidth-based layouts
emthe current element's font-sizesizes that scale with text
remthe ROOT (html) font-sizemodern, predictable
vw1% of viewport widthfull-screen hero
vh1% of viewport heightfull-height sections
vminsmaller of vw/vh
vmaxlarger of vw/vh
chwidth of "0" glyphtypography measures

rem vs em

Beware the difference:

  • 1em = parent's font-size, compounds (em in em grows!).
  • 1rem = <html> root font-size, always constant.

Best practice: rem for spacing/font, em careful for nested sizes. Fonts default to 16px.

Percentages

  • % of the parent for width/height/padding.
  • Relative size for font-size.

Fluid type

vw-based font sizes scale with screen; clamp() (next lesson) tames them.

TL;DR

  • px fixed. rem & em relative to root/parent font.
  • vw/vh relative to viewport.
  • % relative to parent.
  • rem for predictable spacing.
  • ch for measuring text widths.