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:
- Absolute units (px) โ Fixed size, always the same
- Relative to parent (em, %) โ Scale with parent size
- Relative to root (rem) โ Scale with root font size
- Viewport-relative (vw, vh, vmin, vmax) โ Scale with screen size
- CSS math functions (calc, min, max, clamp) โ Combine units and do math
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:
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.
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.
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:
Interactive: CSS math functions
Resize your browser window to see clamp(), min(), and max() in action. Watch how the boxes scale smoothly.
When to use each unit
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.
The safest choice for responsive design. Set html { font-size: 16px; } once, then use rem everywhere. Scales consistently and predictably.
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.
Perfect for column widths, flexible layouts, and sizing relative to container. width: 50% means half of parent's width.
Great for hero sections, full-screen designs, and making elements scale with screen size. vmin is perfect for maintaining aspect ratios across devices.
The modern way to do responsive typography and spacing. clamp(16px, 3vw, 32px) scales smoothly and stays within bounds. No media queries needed!
Build a fluid layout using ONLY relative units and CSS math. No px allowed!
- Use
remfor all typography (headings, paragraphs) - Use
%orvwfor 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
rem relative to?calc(100% - 200px) do?em instead of rem?vmin refer to?clamp() take?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 โ