Website review
This review may include AI-generated content and is not individually verified. Domain, certificate and other facts come from the observed data on this page.
What is What CSS length unit should you use?
What CSS length unit should you use? is a single-purpose decision aid: an interactive flowchart that walks you through picking a CSS length unit. Instead of listing units in prose, it asks you to follow branches until you land on a recommendation, which suits developers who already know the options and just want a quick tiebreaker.
How it works in practice
The page centers on one flowchart. You click and drag to pan around it and use ctrl/cmd plus the mouse wheel to zoom, so it behaves like a small canvas rather than a scrolling article. There is no long explanation alongside it — the value is in the branching logic itself.
When it helps, and when it doesn't
It fits a concrete scenario: you are styling a component, you are unsure whether a value should be relative or fixed, and you want a consistent rule rather than a gut call. It is less useful if you are new to CSS lengths entirely, because a flowchart tells you what to choose but not why each unit behaves the way it does, and it won't cover every edge case in your layout.
A practical next step
Use the chart to reach a decision, then verify it against your actual constraints — how the value should respond to user font-size settings, container size, or viewport changes. If the chart's answer conflicts with how your layout scales, trust the layout and note the exception. For deeper background on individual units, official references such as MDN Web Docs are the better companion.
How do I choose the right CSS length unit for my project?
Choosing a CSS length unit comes down to one question: should this value scale with the user's font settings, with the viewport, or stay fixed? What CSS length unit should you use? answers it as an interactive flowchart you can pan and zoom, so you follow decision branches rather than reading a long table.
Quick decision guide
- Text and spacing that should respect user preferences: relative units such as
rem(root font size) orem(parent font size).remkeeps sizing predictable across nested components;emcompounds, which is useful for padding that scales with its own component's text. - Layout that should adapt to the screen: viewport units (
vw,vh,dvh,svh) or percentages. Watch out for mobile browser chrome — dynamic viewport units exist precisely becausevhcan misbehave when toolbars show and hide. - Borders, hairlines, and other near-fixed details:
pxis still reasonable, because a 1px border is meant to look like a 1px border. - Container-driven components: container query units (
cqw,cqi) when a component should scale with its container, not the viewport.
A concrete scenario
You are building a card component used in both a narrow sidebar and a wide hero section. If you size its padding in vw, it will look bloated in the hero and cramped in the sidebar. Sizing in rem ties it to user font preferences, which is safer. If the card truly must scale with its slot, container query units are the better fit. That trade-off — user preference versus layout context — is exactly the kind of branch a flowchart handles well.
Next step
Open the flowchart and trace your own case: start from "is this text, spacing, or layout?" and follow the branches to a unit. Then test the result by changing your browser's default font size and resizing the window; a good unit choice survives both.
What are the differences between absolute and relative CSS length units?
Absolute units (px, cm, mm, in, pt, pc) map to a fixed physical or device size, so they stay the same regardless of context. Relative units (em, rem, %, vw, vh, ch, ex, lh, and newer viewport variants) are calculated from something else: the parent font size, the root font size, the containing block, or the viewport. That difference decides how your layout behaves when users zoom, change their browser's default font size, or open the page on a phone.
Where the difference shows up
- Accessibility: If a user raises their browser's base font size,
rem-based text and spacing scale with it. Apx-only layout often stays put, which is exactly the failure mode that makes text hard to read. - Component reuse:
emcompounds through nesting, so a button inside a smaller container shrinks its own padding and text.remstays anchored to the root, which is why it is common for type scales and spacing systems. - Fluid layout:
%,vw, andvhrespond to the container or viewport. They are useful for full-bleed sections, butvwignores scrollbars in some cases andvhcan misbehave on mobile browsers with dynamic toolbars. - Fine detail:
pxremains predictable for hairline borders, shadows, and cases where you want an exact device-pixel result.
Quick comparison
| Unit type | Reference point | Scales with user font size | Typical use |
|---|---|---|---|
Absolute (px, pt, cm, in) |
Fixed size | No | Borders, fine detail, print |
Relative (rem, em, %) |
Root, parent, or container | Yes, for font-based units | Text, spacing, components |
Viewport (vw, vh, vmin, vmax) |
Viewport dimensions | Not directly | Hero sections, fluid type |
Character-based (ch, ex) |
Glyph or x-height | Yes | Measure, form field widths |
A practical next step
Start with rem for font sizes and spacing, % or fr for layout widths, and px for borders and shadows. When you are unsure, test one specific case: set your browser's default font size to "Very Large" and reload. Anything that fails to grow is a candidate for a relative unit.
For a decision aid, the flowchart at What CSS length unit should you use? walks through unit choices interactively. If you want the underlying specifications, MDN Web Docs documents each unit's reference point and browser behavior.
How do CSS length units affect responsive design?
CSS length units determine how sizes scale when the viewport, font size, or user preferences change. That is what makes them central to responsive design: the unit you pick decides whether a value stays fixed or adapts.
The core split
- Absolute units like
pxare fixed. They ignore the user's font-size settings and don't scale with the viewport. Useful for hairlines, borders, and anything that should stay crisp. - Font-relative units like
emandremscale with text.remis tied to the root font size, so it respects a user's browser font-size preference.emcompounds against the parent, which is powerful for component-level scaling but easy to misjudge. - Viewport-relative units like
vw,vh,vmin,vmaxscale with the browser window. They suit full-bleed sections and fluid type, but purevwtext can become unreadably small or absurdly large. - Percentage is relative to the parent's corresponding dimension, so its behavior depends heavily on context.
A practical scenario
You're building a card grid. Padding in rem keeps spacing consistent with the user's text size. A max-width in rem or ch keeps line lengths readable. A hero section's height in vh fills the screen, while clamp() mixing rem and vw gives headings that scale but stay within sane bounds.
The trade-off is always the same: fixed units give you predictable control, relative units give you adaptation. Most responsive layouts mix both.
Deciding quickly
Ask what the value should respond to. If it should follow the user's text settings, use rem. If it should follow the container or viewport, use % or viewport units. If it must never change, use px.
For a visual decision path, What CSS length unit should you use? offers a flowchart covering these choices.
What are common mistakes when using CSS length units?
The most common mistakes come from treating CSS length units as interchangeable when they answer different questions: how big is the text, how big is the viewport, or how big is the parent element. A unit that behaves well in one context often breaks in another.
Frequent pitfalls
- Using
pxfor everything. It is predictable, but it ignores user font-size preferences and does not scale with the rest of a fluid layout. Fine for borders and hairlines; weaker for body text and spacing that should adapt. - Mixing
emcarelessly.emis relative to the current element's font size, so nested elements compound. A2empadding inside an element that already has a larger font can balloon unexpectedly.remavoids this because it is always relative to the root. - Confusing
%on different properties. A percentage width resolves against the containing block's width, but a percentagepaddingormarginalso resolves against width — even for top and bottom. That surprises people who expect vertical percentages to use height. - Assuming viewport units are stable on mobile.
vhcan behave oddly when browser UI (address bars) shows or hides, andvwcan trigger horizontal scrolling if a scrollbar is present.dvh,svhandlvhexist to address the dynamic viewport. - Ignoring
line-heightunitlessness. Settingline-heightinpxoremcan break inheritance; a unitless number scales with each element's own font size and is usually the safer default. - Forgetting accessibility. Locking text in
pxcan prevent users who increase their browser's default font size from seeing larger text.
A practical decision rule
| If you want… | Prefer | Why |
|---|---|---|
| Text and spacing that respect user settings | rem |
Root-relative, predictable, scales |
| Component-local scaling | em |
Relative to the component's own font size |
| Fluid layout tied to the viewport | %, vw, dvh |
Adapts to container or screen |
| Fixed, non-scaling details | px |
Simple and stable for borders, hairlines |
As a next step, try the same component twice — once with all px values, once with rem and % — and resize the browser text setting. The difference in readability and layout stability will show you where each unit belongs. For a visual decision aid, What CSS length unit should you use? walks through choosing a unit with a flowchart.
How do CSS length units impact accessibility?
CSS length units affect accessibility mainly through how they respond to user font-size settings, zoom, and viewport changes. Units tied to the user's font size (such as rem and em) preserve text scaling; fixed units (such as px) do not scale with browser font-size preferences, which can make text too small for readers who have increased their default size.
The practical distinctions:
remscales with the root font size, so it respects browser and OS font-size settings. This is usually the safest choice for font sizes, spacing, and layout widths.emscales relative to the parent element's font size. Useful for components that should scale as a block, but nesting can compound sizes unexpectedly.pxis absolute in CSS terms and ignores user font-size preferences. Fine for borders or hairlines, risky for body text.- Viewport units (
vw,vh) respond to screen size, not user text preferences. Text sized only invwcan become unreadably small or large and may not respond to zoom consistently. ch,exrelate to character or x-height metrics and can support readable line lengths, but they are less predictable across fonts.
For a concrete scenario: a reader with low vision sets their browser default font to 24px. A page using font-size: 1rem grows accordingly; a page using font-size: 16px stays fixed and may be difficult to read. The same principle applies to spacing and container widths—if everything is in px, the whole layout stays small even when the user asks for larger text.
A useful next step is to open the browser's font-size settings, set a larger default, and reload your page. If text and layout scale, your unit choices are supporting accessibility; if they stay fixed, audit where px is used for text and spacing. For a quick decision aid, the flowchart at What CSS length unit should you use? maps common CSS situations to suitable units.
User reviews (0)