CSS Responsive Units
Choosing the wrong CSS unit is one of the most common causes of layouts that look
fine on your screen but break on others. CSS has over a dozen unit types — each with a specific purpose.
This chapter covers every unit you'll actually use: absolute px,
relative em and rem, viewport units
vw/vh, percentage %, and the modern fluid functions clamp(), min() and max().
px and when to
avoid item vs rem — the critical differencevw, vh, svh, dvh% and what it's
relative toch, ex, lhclamp(), min(), max()All CSS Units at a Glance
CSS units fall into two broad categories: absolute (fixed, doesn't scale) and relative (scales based on something else — parent, root, viewport, or font):
px — The Absolute Unit
px is the most familiar unit — one CSS pixel. On
regular screens it maps to one physical pixel; on high-DPI (Retina) screens the browser scales it
automatically. The key limitation: it ignores user font-size preferences. If a user sets
their browser default to 20px because they have poor vision, font-size: 16px overrides that completely.
/* ✓ Good uses of px — things that should NOT scale with font */
border { border: 1px solid #e5e7eb; } /* always 1px thin */
box-shadow { box-shadow: 0 2px 8px rgba(0,0,0,.1); }
outline { outline: 2px solid blue; }
media queries { /* @media (min-width: 768px) — predictable breakpoints */ }
letter-spacing { letter-spacing: 1px; }
/* ✗ Avoid px for — things that SHOULD scale */
body { font-size: 16px; } /* ✗ ignores browser font preference */
p { font-size: 14px; } /* ✗ use rem instead */
.btn { padding: 12px 24px; } /* acceptable but em is better */
/* The 62.5% trick (avoid) */
html { font-size: 62.5%; } /* makes 1rem = 10px — but breaks accessibility */
em vs rem — The Most Important Distinction
Both em and rem are relative units, but they reference different base values. The
difference becomes critical with nested elements:
padding: .5em 1em), or icon sizes inside a component.
/* rem — global scale, respects user browser preference */
html { font-size: 100%; } /* 100% = browser default (usually 16px) */
body { font-size: 1rem; } /* = 16px */
h1 { font-size: 2.5rem; } /* = 40px */
h2 { font-size: 2rem; } /* = 32px */
p { font-size: 1rem; } /* = 16px */
small { font-size: .875rem; } /* = 14px */
/* rem — spacing */
.section { padding: 2rem 1.5rem; }
.gap-md { gap: 1rem; }
/* em — component-proportional sizing */
.btn {
font-size: 1rem; /* base */
padding: .5em 1.25em; /* scales with btn font size */
border-radius: .25em; /* proportional radius */
}
.btn-lg { font-size: 1.25rem; } /* padding scales automatically */
.btn-sm { font-size: .875rem; } /* padding scales automatically */
/* margin-bottom in em — relative to own font-size */
h1 { margin-bottom: .5em; } /* half h1's font size */
h2 { margin-bottom: .5em; } /* half h2's font size (different px!) */
Viewport Units — vw, vh, vmin, vmax, svh, dvh
Viewport units are relative to the browser window size. They are powerful for full-screen layouts, fluid typography, and elements that should relate to the overall screen dimensions:
These blocks are sized in vw — resize your browser window to see them reflow
100vh is taller than the visible area because it ignores the
browser address bar — causes a common scroll bug.100svh never
overflows on any mobile browser.min-height: 100dvh./* vw — viewport width */
.hero { width: 100vw; } /* full viewport width */
.sidebar { width: 25vw; } /* quarter of viewport */
/* vh — viewport height */
.hero { height: 100vh; } /* full screen height */
.modal { max-height:80vh; } /* 80% of viewport */
/* Modern viewport units (mobile-safe) */
.page { min-height: 100dvh; } /* dynamic viewport height ← use this */
.safe { height: 100svh; } /* small viewport height — never overflows */
.large { height: 100lvh; } /* large viewport height */
/* vmin — smaller of vw or vh */
.square { width: 50vmin; height: 50vmin; } /* always square */
/* vmax — larger of vw or vh */
.cover { font-size: 6vmax; } /* bigger text on landscape */
/* Fluid font-size with vw (without clamp — use carefully) */
h1 { font-size: 5vw; } /* grows with viewport — no min/max guard */
% — Percentage (Relative to Parent)
Percentage is one of the most used units — but what it's relative to depends on the property. Understanding the reference context prevents common bugs:
| Property | % is relative to… | Example |
|---|---|---|
| width | Parent element's width | width: 50% → half parent width |
| height | Parent element's height (only if parent has explicit height) | height: 100% → needs parent height set |
| padding | Parent element's width (even for top/bottom!) | padding-top: 56.25% → 16:9 aspect ratio trick |
| margin | Parent element's width (same as padding) | margin: 0 auto → centres block |
| font-size | Parent element's computed font-size | font-size: 90% → 90% of parent font |
| line-height | Element's own font-size | line-height: 150% → 1.5× font size |
| transform: translate | Element's own dimensions | translateX(50%) → half own width |
| background-size | Background positioning area | background-size: 50% |
/* Layout widths */
.col-half { width: 50%; }
.col-third { width: 33.333%; }
.full { width: 100%; }
/* Centring with margin auto */
.container { max-width: 1200px; margin: 0 auto; }
/* Aspect ratio trick — padding-top % is relative to width */
.video-wrap {
position: relative;
padding-top: 56.25%; /* 9/16 = 56.25% for 16:9 ratio */
}
.video-wrap iframe {
position: absolute;
inset: 0; /* fills 100% width and height */
width: 100%;
height: 100%;
}
/* Note: Modern CSS can use aspect-ratio: 16/9 instead */
/* Vertical centering with % transform */
.centred {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* -50% of OWN size */
}
/* html font-size as % (recommended for accessibility) */
html { font-size: 100%; } /* inherits browser default — respects user preference */
Typography Units — ch, ex, lh, cap
These units are relative to the current font — useful for precise typographic control:
Width of the "0" character in the current font. Perfect for setting readable line lengths (65–75ch is optimal for body text).
max-width: 65ch — a
comfortable reading width regardless of font size.
p { max-width: 65ch; }
input { width: 20ch; } /* fits ~20 chars */
1lh = the computed line-height of the element. Useful for matching
spacing to the rhythm grid of your typography.
/* margin that aligns to text rhythm */
.section { margin-top: 2lh; }
/* Drop cap height */
.drop-cap::first-letter {
font-size: 3lh;
float: left;
}
ex = x-height of font · cap =
cap-height
clamp(), min(), max() — Fluid Functions
These three CSS functions accept multiple values or calculations, making fluid responsive design possible without media queries:
clamp(.875rem, 1.5vw + .5rem, 1.125rem) — it scales smoothly between a
minimum and maximum size as the viewport changes, never becoming too small to read or too large to look
good.Heading: clamp(1.25rem, 3vw + .5rem, 2.5rem) · Paragraph: clamp(.875rem, 1.5vw + .5rem, 1.125rem)
/* clamp(minimum, preferred, maximum) */
h1 { font-size: clamp(1.75rem, 4vw + 1rem, 3.5rem); }
h2 { font-size: clamp(1.5rem, 3vw + .75rem, 2.5rem); }
p { font-size: clamp(1rem, 1.5vw + .5rem, 1.25rem); }
/* clamp for responsive padding */
.section {
padding: clamp(1.5rem, 5vw, 4rem) clamp(1rem, 4vw, 3rem);
}
/* clamp for fluid layout width */
.container {
width: min(90%, 1200px); /* never wider than 1200px, never narrower than 90% */
margin: 0 auto;
}
/* min() — picks the SMALLER of two values */
.img { width: min(100%, 400px); } /* 100% or 400px, whichever is less */
.sidebar { width: min(25vw, 300px); } /* caps at 300px on wide screens */
/* max() — picks the LARGER of two values */
.panel { padding: max(1rem, 3vw); } /* at least 1rem, grows with viewport */
.avatar { width: max(40px, 5vw); } /* never smaller than 40px */
/* Combining all three */
.card {
padding: clamp(1rem, 3vw, 2rem);
font-size: clamp(.875rem, 1vw + .5rem, 1.125rem);
gap: clamp(.5rem, 2vw, 1.5rem);
}
Quick Decision Guide
| Use case | Recommended unit | Why |
|---|---|---|
| Body font size | rem |
Respects browser default and user preferences |
| Heading font sizes | clamp() with rem+vw |
Fluid scaling between min/max without media queries |
| Button padding | em |
Scales with button's own font-size automatically |
| Page layout spacing | rem |
Consistent across components, respects user font size |
| Max reading width | ch |
Scales with the font — always 65ch regardless of size |
| Full-screen hero | 100dvh |
Correct height on mobile (avoids address bar bug) |
| Fluid container width | min(90%, 1200px) |
Responsive without breakpoints |
| Borders, shadows | px |
Should never scale — always 1px or 2px |
| Media query breakpoints | px or em |
px is predictable; em scales with font (accessibility) |
| Sidebar width | min(25vw, 300px) |
Proportional but capped for very wide screens |
| Aspect ratio video | aspect-ratio: 16/9 |
Modern way; no % padding hack needed |
Try It Yourself
All unit types in one editor — compare px, em, rem, vw, % and clamp() side by side:
Quick Check — Chapter 40
em is relative to the font-size of the
element's parent. rem is relative to the
font-size of the root html element (browser default 16px).The critical difference is compounding. With em, nesting multiplies the size:
— Parent 16px → child at 1.25em = 20px → grandchild at 1.25em = 25px → great-grandchild = 31.25px
With rem, nesting has no effect — every level always refers back to the same html font-size:
— Any level at 1.25rem = 20px, always, regardless of how deeply nested
Practical guidance:
— Use
rem for font sizes, page-level spacing, and anything that
should be consistent globally— Use
em for component-proportional sizing — button padding, icon
sizes, local spacing that should scale when you change the component's font size— Both respect the user's browser font-size preference (unlike px), which makes them the accessibility-friendly choice for font sizes
clamp(minimum, preferred, maximum) sets a value that:— Is never smaller than minimum
— Is never larger than maximum
— Scales fluidly with the preferred expression (usually a vw-based calculation) between those two bounds
Example:
font-size: clamp(1rem, 2.5vw + .5rem, 2.25rem)On a 320px screen: 2.5vw = 8px + 8px (.5rem) = 16px → equals the min of 1rem → uses 1rem
On a 1200px screen: 2.5vw = 30px + 8px = 38px → exceeds the max of 2.25rem (36px) → caps at 2.25rem
In between: smoothly scales
Why better than media queries for typography:
— One declaration replaces 3–4 media query breakpoints
— No "jumps" at breakpoints — truly smooth and continuous scaling
— Works perfectly with any viewport size, including unusual intermediate sizes
— Less code to maintain
100vh is calculated based on the viewport height
when the browser chrome (address bar + tab bar) is hidden — the maximum possible viewport. But
when you first load a page, the address bar is typically visible and takes up 50–80px of space.Result:
100vh is taller than what's actually visible, causing a
vertical scroll just below the fold on mobile — the classic "100vh bug."Solutions:
—
100dvh (dynamic viewport height) — updates in real-time as the
browser UI shows/hides. Best for most cases but causes reflow animations.—
100svh (small viewport height) — always the smallest possible
height (address bar visible). Never overflows, but doesn't expand when scrolling.—
100lvh (large viewport height) — same as old 100vh. Not usually
what you want.Recommendation: Use
min-height: 100dvh for hero
sections and full-page layouts on modern browsers. For maximum compatibility, use JavaScript's window.innerHeight as a CSS custom property fallback.
When you set
html { font-size: 16px }, you override the user's
browser preference with an absolute value — their setting is ignored. Every element sized in rem now
ignores what the user asked for.The correct approach:
—
html { font-size: 100% } — preserves the browser's default
(usually 16px) and respects user changes— Then use
rem for all font sizes throughout the site— When a user sets their browser to 20px, your 1rem body text becomes 20px, 1.5rem headings become 30px — all proportionally correct
The "62.5% trick" (
html { font-size: 62.5% } to make 1rem = 10px)
has the same problem — it scales the user preference rather than preserving it, and causes accessibility
issues.
ch equals the width of the "0" (zero) character in the current font
at the current font-size.Why it's useful: Typography research consistently shows that 45–75 characters per line is the optimal range for comfortable reading. When you set
max-width: 65ch on a paragraph, you're defining the line length in terms
of character counts — which automatically scales with the font size.If you change the font-size from 16px to 20px (e.g., for a user's preference or a larger screen), a pixel-based max-width would stay fixed, fitting fewer characters per line. But
65ch always stays ~65 characters wide, maintaining the intended reading
experience.Practical uses:
—
p { max-width: 65ch } — articles and blog posts—
input { width: 20ch } — text inputs sized for expected content—
.prose { max-width: 70ch } — long-form reading content
font-size: 20px. Its child has
font-size: 1.5em. What is the child's computed font-size?
100vh cause a scroll bug on mobile
browsers?padding-top: 56.25% to an
element, what is 56.25% relative to?- px — absolute, fixed. Use for borders, shadows, breakpoints. Avoid for font sizes and spacing that should scale.
- em — relative to parent font-size. Compounds with nesting. Use for component-local proportional sizing (button padding, icon sizes).
- rem — relative to html root font-size. Never compounds. Use for global font sizes and
spacing. Set
html { font-size: 100% }to respect user browser preferences. - vw / vh — 1% of viewport width/height. Use for full-screen layouts and fluid
typography. Avoid bare
100vhon mobile. - dvh / svh / lvh — modern viewport height variants that account for mobile browser UI.
Use
min-height: 100dvhfor full-screen mobile layouts. - % — relative to parent (width for most properties, own font-size for line-height). Percentage padding is always relative to parent width — even vertical padding.
- ch — width of "0" in current font. Set
max-width: 65chfor readable text — scales with font size automatically. - clamp(min, preferred, max) — fluid value that scales smoothly between bounds. Best for fluid typography with no media queries needed.
- min(a, b) — picks the smaller value.
width: min(100%, 1200px)= responsive container cap. - max(a, b) — picks the larger value.
padding: max(1rem, 3vw)= minimum padding with viewport growth.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.