Most things that go wrong in responsive CSS come down to unit choice: fonts fixed in px so user settings are ignored, vw-only text that won't grow on zoom, or a 100vh layout that hides buttons under the mobile address bar. This guide collects 10 unit rules you can apply right away, centered on rem, em, vw and clamp(), with examples for each. If you want the fundamentals of each unit first, start with the CSS units guide.
At a glance
| Rule | Recommended unit or technique | Related tool |
|---|---|---|
| 1. Respect the root font size | html { font-size: 100%; } |
PX to REM converter |
| 2. rem for type and spacing tokens | CSS variables + rem | PX to REM converter |
| 3. em for spacing that grows with text | em for button padding and icon sizes | PX to EM converter |
| 4. Unitless line-height | line-height: 1.6 |
Line height calculator |
| 5. em for letter-spacing | letter-spacing: -0.02em |
Letter spacing converter |
| 6. clamp(rem + vw) for fluid type | clamp(min, rem + vw, max) |
clamp generator |
| 7. em for media queries | em media queries, container queries and cqi | PX to EM converter |
| 8. Avoid 100vh and 100vw | svh, dvh, 100% | Viewport units guide |
| 9. Use logical properties | margin-inline, padding-block, inset |
- |
| 10. @layer and minification | @layer, minify before deploying |
CSS minifier and formatter |
1. Respect the root font size
Browsers apply the default font size the user picked in their settings (16px out of the box) to the html element. Override it in px with html { font-size: 16px; } and 1rem stays locked at 16px even for a user who bumped their default font to 20px — which defeats the point of using rem. Keep the distinction in mind: browser zoom scales px too, but the font-size setting ignores px.
/* Avoid: overrides the user's font setting */
html {
font-size: 16px;
}
/* Recommended: use the browser default as-is */
html {
font-size: 100%; /* 1rem = 16px by default; if the user picks 20px, 1rem = 20px */
}
Some projects use html { font-size: 62.5%; } (1rem = 10px) to make the math easier. Because it's a percentage it still follows user settings, but any element without its own font size — and third-party widgets — shrinks to 10px. It's safer to leave the root at 100% and let the PX to REM converter do the math.
2. Use rem for type and spacing tokens
Define font sizes and spacing as CSS variables (custom properties) in rem and you manage the design system in one place, with everything scaling by the same ratio when the user changes their font setting. Values that shouldn't grow, like border widths, belong in px.
:root {
/* Font sizes (at a 16px root) */
--font-size-sm: 0.875rem; /* 14px */
--font-size-base: 1rem; /* 16px */
--font-size-lg: 1.125rem; /* 18px */
--font-size-xl: 1.5rem; /* 24px */
/* Spacing */
--space-1: 0.25rem; /* 4px */
--space-2: 0.5rem; /* 8px */
--space-4: 1rem; /* 16px */
--space-6: 1.5rem; /* 24px */
--space-8: 2rem; /* 32px */
/* Values that must not change stay in px */
--border-width: 1px;
--radius: 8px;
}
.card {
padding: var(--space-6);
font-size: var(--font-size-base);
border: var(--border-width) solid #d0d0d0;
border-radius: var(--radius);
}
The PX to REM converter makes it easy to turn design px values into rem tokens.
3. Use em for spacing that grows with text
Values that should stay proportional to the text size — button padding, icon sizes, corner radius — go in em. When you build size variants (small and large buttons), changing font-size alone makes everything else follow. But nesting font-size itself in em compounds the multiplier, so split the work: rem for font-size, em for internal spacing.
.btn {
font-size: 1rem; /* 16px */
padding: 0.75em 1.5em; /* 12px 24px */
border-radius: 0.5em; /* 8px */
}
.btn svg {
width: 1em; /* the icon grows with the text */
height: 1em;
}
.btn--small {
font-size: 0.875rem; /* 14px → padding shrinks to 10.5px 21px automatically */
}
.btn--large {
font-size: 1.25rem; /* 20px → padding grows to 15px 30px automatically */
}
To calculate em values from the parent's font size, use the PX to EM converter.
4. Write line-height without a unit
Give line-height an em or % value and the px value computed on the parent is inherited as-is, so lines can overlap in headings larger than the body text. A unitless number is inherited as a ratio and recalculated against each element's own font size. Values around 1.6–1.8 are common for body text.
body {
font-size: 1rem;
line-height: 1.6; /* 16px × 1.6 = 25.6px */
}
h2 {
font-size: 1.75rem; /* 28px */
line-height: 1.3; /* 28px × 1.3 = 36.4px */
}
/* Avoid: the computed value is inherited as-is */
.article {
font-size: 1rem;
line-height: 1.6em; /* locked at 25.6px; a child h2 (28px) overlaps unless you set its own */
}
If the design gives line height in px, divide line height by font size to get the ratio: a 16px font with a 24px line height is 24 ÷ 16 = 1.5. The line height calculator converts Figma values directly.
5. Use em for letter-spacing
Set letter-spacing in px and the spacing stays fixed while the font size changes, so in headings that grow with clamp() the tracking gets relatively tighter or looser. In em it scales with the font size and keeps the design's proportions. If the design specifies tracking in %, divide by 100; if in px, divide by the font size.
/* Design: 32px font, -2% tracking */
.title {
font-size: 2rem;
letter-spacing: -0.02em; /* 32px × -0.02 = -0.64px */
}
/* Design: 14px font, 0.7px tracking */
.label {
font-size: 0.875rem;
letter-spacing: 0.05em; /* 0.7 ÷ 14 = 0.05 */
}
Paste a tracking value from Figma or Photoshop into the letter spacing converter and get the em value instantly.
6. Use clamp(rem + vw) for fluid type
Font sizes set in vw alone get too small on very narrow screens and don't grow when the browser is zoomed. Use clamp() to set a minimum and maximum, and always mix rem and vw in the middle value so the rem portion grows on zoom.
/* Heading: 24px on a 375px screen → 40px on a 1440px screen */
h1 {
font-size: clamp(1.5rem, 1.148rem + 1.502vw, 2.5rem);
}
/* Section spacing: 32px on a 375px screen → 64px on a 1440px screen */
.section {
padding-block: clamp(2rem, 1.296rem + 3.005vw, 4rem);
}
/* Avoid: the middle value is vw only, so zoom has no effect */
.title-bad {
font-size: clamp(1rem, 2.5vw, 2rem);
}
The middle value comes from this formula:
slope (vw) = (max px − min px) ÷ (max screen − min screen) × 100
= (40 − 24) ÷ (1440 − 375) × 100 = 1.502vw
intercept (rem) = (min px − slope × min screen ÷ 100) ÷ 16
= (24 − 1.502 × 3.75) ÷ 16 = 1.148rem
check: at 1440px, 1.148 × 16 + 1.502 × 14.4 ≈ 40px
Enter the minimum and maximum screen sizes and font sizes in the clamp generator and it calculates these values for you. The difference between clamp() and vw is covered in CSS clamp() vs vw, and the zoom problem in VW units and web accessibility.
7. Use em in media queries
An em in a media query is based on the browser's default font size (usually 16px), not html's font-size. So 768px becomes 48em, and when users enlarge their default font, the points where the layout changes move with it, keeping the layout intact at large text sizes. When something should respond to the width of the area a component sits in rather than the page, use container queries (supported by all major browsers since 2023) and the cqi unit.
/* 768px ÷ 16 = 48em, 1024px ÷ 16 = 64em */
.card-list {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
@media (min-width: 48em) {
.card-list { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 64em) {
.card-list { grid-template-columns: repeat(3, 1fr); }
}
/* Components decide based on the width of the area they sit in */
.card {
container-type: inline-size;
}
@container (min-width: 25rem) { /* card at least 400px wide */
.card__body {
display: grid;
grid-template-columns: 8rem 1fr;
gap: 1rem;
}
}
.card__title {
/* 18px at a 300px card width, 24px at 600px and up */
font-size: clamp(1rem, 0.75rem + 2cqi, 1.5rem);
}
To convert breakpoints to em, set the base to 16px in the PX to EM converter.
8. Avoid 100vh and 100vw
In mobile browsers, 100vh equals the height with the address bar collapsed (lvh), so the bottom of the screen is cut off while the address bar is showing. 100svh fits on screen even with the address bar expanded, and 100dvh tracks the actual height every time the bar collapses or expands. Because dvh changes during scrolling and triggers layout recalculation, use svh for ordinary sections and dvh for full-screen app layouts where a bottom button must always be visible. Also, 100vw includes the width of the vertical scrollbar, so it causes horizontal scrolling wherever the scrollbar takes up space.
.hero {
min-height: 100svh; /* fits on screen even with the address bar expanded */
}
.app-shell {
display: grid;
grid-template-rows: auto 1fr auto;
height: 100dvh; /* follows the real height as the address bar changes, so bottom buttons stay visible */
}
.full-bleed {
width: 100%; /* 100vw includes the scrollbar width and causes horizontal scroll */
}
See the viewport units guide for the different viewport units, and 6 cases where you should NOT convert px to vw for values that shouldn't be converted to vw.
9. Use logical properties
Write margin-inline instead of margin-left and margin-right, padding-block instead of padding-top and padding-bottom, and inset instead of top, right, bottom and left. The code gets shorter, and it automatically adapts to right-to-left languages and vertical writing modes. The ideas of inline (the direction text flows) and block (the direction lines stack) also connect to units like cqi. All major browsers support them.
/* Physical properties */
.content {
max-width: 40rem;
margin-left: auto;
margin-right: auto;
padding-top: 1.5rem;
padding-bottom: 1.5rem;
}
/* Logical properties */
.content {
max-inline-size: 40rem;
margin-inline: auto;
padding-block: 1.5rem;
}
.overlay {
position: fixed;
inset: 0; /* top, right, bottom, left: 0 */
}
10. Order styles with @layer, minify before deploying
Selector specificity compares three components — (ID, class, element) — from left to right, so no number of chained classes can beat a single ID. Once you start lengthening selectors or adding !important to win overrides, even changing your unit tokens gets hard. Declare the order of your style groups up front with @layer (supported by all major browsers since 2022), and later layers win regardless of specificity. Remember too that unlayered styles take priority over every layer.
@layer reset, base, components, utilities;
@layer reset {
*, *::before, *::after { box-sizing: border-box; }
}
@layer base {
html { font-size: 100%; }
body { line-height: 1.6; }
}
@layer components {
.card .title { font-size: var(--font-size-lg); } /* specificity (0, 2, 0) */
}
@layer utilities {
.text-sm { font-size: var(--font-size-sm); } /* (0, 1, 0), but wins as the later layer */
}
Before deploying, strip whitespace and comments to shrink the file. The CSS minifier and formatter minifies your code in one step, or reformats minified code so it's readable again.
Basic accessibility checks
Even with the right units, you lose accessibility if you overlook color, focus and motion. Body text needs a contrast ratio of at least 4.5:1 against its background (AA), and large text at least 3:1. White text on the commonly used #2196f3 is 3.12:1, which falls short, so use a darker button background like #1976d2 (4.6:1). Check it instantly with the contrast checker.
.btn-primary {
background: #1976d2; /* 4.6:1 with white text (passes AA) */
color: #fff;
transition: background 0.3s;
}
.btn-primary:hover {
background: #1565c0; /* 5.75:1 */
}
/* Shown only on keyboard (and similar) focus (supported by all major browsers) */
.btn-primary:focus-visible {
outline: 2px solid #1976d2;
outline-offset: 2px;
}
/* Respect users who turned on reduced motion */
@media (prefers-reduced-motion: reduce) {
.btn-primary {
transition: none;
}
}
Naming and frameworks
Class naming conventions like BEM, utility frameworks like Tailwind CSS and linters like Stylelint are choices to make based on your team. Whichever you pick, the unit rules above still apply. Tailwind's default spacing and font sizes are rem-based too, and since v4 these tokens are defined with @theme inside your CSS file. What matters is applying the rules consistently within a project.
Summary
The core of responsive CSS units is respecting both user settings and screen size. Leave the root font at 100%, use rem for type and spacing, em for spacing and letter-spacing that should scale with text, and unitless numbers for line-height. Handle values that change with the screen with a clamp() that mixes rem and vw, media queries with em, and heights with svh or dvh, and you'll avoid most problems. Convert design px values right away with the PX to VW converter, the PX to REM converter and the clamp generator.