CSS Units Explained: A Practical Guide
If you’ve ever wondered why a layout looks great on your laptop but breaks on a phone, the answer often lies in the CSS units you choose. Units tell the browser how big something should be – whether it’s a font, a margin, or an image. Picking the right unit can make your site flexible, faster, and easier to maintain.
In this guide we’ll walk through the most common units, show you real‑world scenarios, and give you a quick checklist so you never have to guess again.
Why Choose the Right Unit?
Every unit behaves differently when the viewport changes, when the user changes browser settings, or when a parent element resizes. Using a fixed px
value can lock your design in place, causing overflow on small screens. Relative units like em
or rem
scale with font size, which helps accessibility – users who increase default text size will still see a balanced layout.
Performance also matters. When you rely on too many calc()
expressions or custom properties, the browser does extra work. Simpler units keep rendering fast, especially on low‑end devices.
Common Units and When to Use Them
Pixels (px) – Use for precise UI details that shouldn't change, such as border thickness or an icon that matches a design system. Keep them limited to decorative parts.
Relative EM (em) – Ties to the font size of the element itself. Great for components that need to grow or shrink with their own text, like button padding that follows the button’s font.
Root REM (rem) – Based on the root html
font size. Perfect for site‑wide spacing, headings, and layout grids because you can change the entire scale by adjusting one value in the root.
Viewport Width/Height (vw / vh) – Measures a percentage of the viewport. Handy for full‑screen hero sections or typography that should scale with the screen width. Remember that on mobile browsers the address bar can shrink the viewport, so test on real devices.
Percentage (%) – Works relative to the parent element’s dimensions. Use for fluid columns, image containers, or any layout where the child should fill a portion of its parent.
CSS calc()
– Combines units, e.g., calc(100% - 2rem)
. Use sparingly for complex spacing that can’t be expressed with a single unit.
Here’s a quick cheat sheet you can copy into your stylesheet:
/* Base font size – adjust for accessibility */
html { font-size: 100%; }
/* Layout spacing – use rem for consistency */
section { padding: 2rem 1rem; }
/* Component padding – em for button‑specific scaling */
button { padding: 0.5em 1em; }
/* Full‑width hero – vw for dynamic height */
.hero { height: 60vh; }
Tip: Start every new project with a html { font-size: 62.5%; }
rule. That makes 1rem equal 10px, turning conversions into simple math (1.6rem = 16px, etc.).
Testing is key. Open DevTools, toggle device toolbar, and watch how elements react when you switch between px
, em
, and rem
. If something jumps or cuts off, you probably used the wrong unit.
Finally, remember that consistency beats cleverness. Pick a handful of units, document the rules in your style guide, and stick to them. Your code stays readable, and future developers (or your future self) won’t have to chase down why a particular element was sized with vh
instead of rem
.
With these basics in hand, you can build layouts that feel right on any device, stay accessible, and load quickly. Happy styling!