CSS Units Explained: px vs em vs rem vs %, vw, vh
px, em, rem, %, vw, vh, dvh and ch compared live, which ones respect the user's font size, and what to use for type and layout.
CSS units: common questions
What is the difference between em and rem?
em is relative to the current element's font size, so it compounds when nested. rem is always relative to the root element's font size.
Related chapters
- CSS Box Model Explained Visually: Margin, Border, Padding
- CSS Position Explained: Relative, Absolute, Fixed, Sticky
- CSS Pseudo-Classes: :hover, :has(), :nth-child Explained
- CSS ::before and ::after Pseudo-Elements Explained
- CSS Transitions Explained: Duration, Easing, Delay
- CSS Animations and @keyframes, with Examples
- CSS Flexbox Explained Visually, with a Playground
- Flexbox Not Working? min-width: 0 and Other Surprises
- CSS Grid Explained Visually: Columns, Areas, auto-fit
- CSS Selectors and Specificity Explained
- CSS Backgrounds and Borders: Gradients, border-radius
- CSS Shapes: clip-path, shape-outside and Triangles
- CSS Visual Effects: box-shadow, filter, blend modes
Keep learning
Overview
Chapter 11 · units Every unit is “this many of what?” px is a CSS pixel. rem is the root font size. em is the current element’s font size, and for font-size itself it is the parent’s. The iframe below is a real document, so the root slider is an honest rem.
1. Move the root, then the parent
The blue line should change only with the root. The green line should change only with the parent. The gold line should ignore both. Then drag the frame: clamp() grows and then stops.
The references
% on width is the containing block’s width. % on font-size behaves like em. vw and vh are the viewport. dvh is the visible viewport, which stays clear of a mobile browser’s chrome. ch is the width of the zero, a useful line length. fr exists inside grid. line-height with a unitless number multiplies the element’s own font size, so it stays in proportion.
2. A percentage height needs a parent height
100vh measures the large viewport. 100dvh measures the slice that is on screen. The drawing is the idea, scaled down. Use rem for type and spacing so a reader’s root font size still works. Use %, fr, and minmax for layout. Use px for hairline borders. clamp() is a fluid size with a floor and a ceiling, which covers a lot of breakpoints on its own.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.