CSS units decide how big something is and what that size depends on. A pixel value stays fixed, a percentage depends on the parent, rem depends on the root font size, and viewport units depend on the screen.
Choosing the right unit is a design decision, not just syntax. Use fixed units when a value must be exact, relative units when a layout should adapt, and character-based units when readability matters.
Add one worked example that compares the normal path with the boundary case for CSS Units px, em, rem, vw, vh.
Keep the note tied to a real CSS workflow so the idea is easier to recall later.
CSS Units px em rem vw vh should be studied as a practical CSS lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
Absolute units represent a fixed measurement. They do not scale naturally based on the screen or parent element, so they are best used when exact sizing is truly needed.
Relative units scale in relation to another value, such as the root font size, parent font size, viewport size, or container size. These units are much more useful for responsive design.
| Unit | Relative To | Common Use |
|---|---|---|
| em | Current element font size | Component spacing tied to local text |
| rem | Root font size | Consistent spacing and typography |
| % | Parent size | Fluid widths and layout sizing |
| vw | Viewport width | Full-width sections, fluid typography |
| vh | Viewport height | Hero sections, full-screen panels |
| ch | Width of the "0" character | Readable text line lengths |
Most production styles mix units. A layout might use rem for spacing, percent for fluid width, ch for readable article length, and px for a one-pixel border. The goal is not to avoid px completely, but to use each unit for the job it does best.
html {
font-size: 16px;
}
body {
font-size: 1rem;
line-height: 1.6;
}
h1 {
font-size: 2.5rem;
}
.container {
width: 90%;
max-width: 72rem;
}
.hero {
min-height: 100vh;
}
.article {
max-width: 65ch;
}
Be careful with viewport height on mobile browsers because browser toolbars can change the visible viewport. Newer units like svh, lvh, and dvh can help when supported, but min-height and flexible layouts are often more reliable than forcing exact full-screen panels.
.article {
width: min(100% - 2rem, 68ch);
margin-inline: auto;
padding-block: clamp(2rem, 6vw, 5rem);
}
.article p {
font-size: 1rem;
line-height: 1.7;
}
.hero {
min-height: 100svh;
padding: 2rem;
}
CSS Units px em rem vw vh matters in CSS because it changes how a program is written, tested, or debugged. The page should explain the normal flow first: what the developer writes, what the runtime or platform does, and what result should appear.
When teaching CSS Units px em rem vw vh, avoid stopping at syntax. Show the surrounding decision: why this feature is chosen, what problem it removes, and what would become harder if the feature were not used.
.lesson-box {
display: block;
max-width: 42rem;
padding: 1rem;
}
.lesson-box:empty::before {
content: "CSS Units px em rem vw vh: add visible content";
}
Setting long article text to width: 100vw.
Use a readable max-width such as 65ch or 72ch with auto margins.
Using em everywhere without understanding compounding.
Use rem for global consistency and em only when local scaling is intended.
Forcing mobile sections to exactly 100vh without testing browser chrome.
Use min-height or newer viewport units like svh/dvh where appropriate.
Memorizing CSS Units px em rem vw vh without the situation where it is useful.
Connect CSS Units px em rem vw vh to a concrete CSS task.
<code>rem</code> is usually better for accessibility and consistency because it respects the root font size more naturally.
Because it compounds based on the current element's font size. Nested elements can grow faster than expected if you are not careful.
They are useful, but mobile browser UI can affect viewport height, so test <code>vh</code>-based layouts on real devices.
Explore 500+ free tutorials across 20+ languages and frameworks.