Why CSS units matter

CSS units are the foundation of responsive design. The unit you choose determines how an element sizes relative to its parent, the viewport, or the root font size. Pick the wrong unit, and your layout breaks on mobile. Pick the right one, and it scales perfectly.

There are five main categories of units:

  1. Absolute units (px) โ€” Fixed size, always the same
  2. Relative to parent (em, %) โ€” Scale with parent size
  3. Relative to root (rem) โ€” Scale with root font size
  4. Viewport-relative (vw, vh, vmin, vmax) โ€” Scale with screen size
  5. CSS math functions (calc, min, max, clamp) โ€” Combine units and do math
Pro tip:
The best layouts use a mix of units. Use rem for spacing and typography, % for widths, and clamp() for truly fluid sizing.

All CSS units explained

Here's every CSS unit you'll ever use, with explanations and examples:

units.css Every CSS unit explained

Interactive: Unit comparison

Edit the root font-size and watch how em and rem react differently. Notice how px stays fixed, em scales with parent, and rem scales with root.

โœ๏ธ Try It Yourself โ€” Unit Comparison
Preview

em vs rem: The critical difference

em and rem are cousins, but they scale differently:

  • em โ€” Relative to the element's parent font size. Compound: if parent is 1.5em and child is 1.5em, child is actually 1.5 ร— 1.5 = 2.25em.
  • rem โ€” Always relative to the root (html) font size. No compounding. 1rem is always 1rem.
em-vs-rem.css The compounding problem with em
Best practice:
Use rem for global sizing (font, margins, padding). Use em only inside components where size scales with the component's font-size.

CSS math functions: calc(), min(), max(), clamp()

Modern CSS lets you combine units and do math. These functions are powerful for responsive design:

math-functions.css calc(), min(), max(), clamp() examples

Interactive: CSS math functions

Resize your browser window to see clamp(), min(), and max() in action. Watch how the boxes scale smoothly.

โœ๏ธ Try It Yourself โ€” Math Functions Live
Preview

When to use each unit

๐Ÿ“
Pixel (px)
Use for borders, shadows, fine details

Pixels are fixed and don't scale. Use them for borders, box-shadows, and small design details that should stay the same size. Avoid for typography and spacing.

๐Ÿ“–
rem (root em)
Use for typography, spacing, global sizing

The safest choice for responsive design. Set html { font-size: 16px; } once, then use rem everywhere. Scales consistently and predictably.

๐Ÿงฉ
em
Use inside components for relative sizing

Use em for padding inside buttons, spacing inside cards, or any sizing that should scale if the component's font-size changes. Avoid nesting em inside em.

๐Ÿ“ฆ
% (percent)
Use for widths, heights relative to parent

Perfect for column widths, flexible layouts, and sizing relative to container. width: 50% means half of parent's width.

๐Ÿ“ฑ
vw, vh, vmin, vmax
Use for viewport-relative sizing and hero sections

Great for hero sections, full-screen designs, and making elements scale with screen size. vmin is perfect for maintaining aspect ratios across devices.

โšก
clamp()
Use for truly fluid, responsive sizing

The modern way to do responsive typography and spacing. clamp(16px, 3vw, 32px) scales smoothly and stays within bounds. No media queries needed!

Challenge: Fluid Layout with No Pixels

Build a fluid layout using ONLY relative units and CSS math. No px allowed!

  • Use rem for all typography (headings, paragraphs)
  • Use % or vw for widths
  • Use clamp() for responsive padding and margins
  • Set html { font-size: 16px; } once, then forget about pixels
  • Make headings scale fluidly with font-size: clamp(1.5rem, 5vw, 3rem)
  • Create a responsive card layout that adapts to mobile and desktop

Hint: Start with html { font-size: 16px; }. Use rem for font-size (e.g., h1 { font-size: 2rem; }), % for container widths (e.g., width: 90%;), and clamp() for padding (e.g., padding: clamp(1rem, 3vw, 3rem);). Make spacing and fonts scale smoothly as viewport changes!

Key takeaways

  • px is absolute โ€” Fixed size, doesn't scale. Use only for borders and small details.
  • rem is your friend โ€” Always scales from root font-size (16px by default). Use for typography and spacing.
  • em compounds โ€” Scales relative to parent. Useful inside components, dangerous for global typography.
  • % is for layout โ€” Widths, heights, and flexible sizing relative to parent.
  • vw/vh scale with screen โ€” Perfect for viewports, hero sections, and responsive designs.
  • clamp() is modern responsive design โ€” Set min, preferred, and max. No media queries needed for many cases.
  • calc() lets you combine units โ€” width: calc(100% - 20px) is powerful.

Test your knowledge

Question 1 of 5
What is rem relative to?
Question 2 of 5
What does calc(100% - 200px) do?
Question 3 of 5
When should you use em instead of rem?
Question 4 of 5
What does vmin refer to?
Question 5 of 5
What three arguments does clamp() take?
0
/5
Excellent! You master CSS units.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered CSS units.
Responsive design is within reach.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’