Lesson 17 of 23
In progress
Units, fluid sizes and responsive defaults
Choose between rem, em, percentage, vw and ch, size text fluidly with clamp, and make media behave on small screens.
A layout that only works at one width is not finished. Units decide whether text respects a reader’s settings, whether an image overflows its column and whether a hero section fits a phone screen.
What you will learn
- Pick the right unit for text, spacing, containers and media.
- Size text fluidly with
min,maxandclamp. - Apply the responsive defaults that prevent most overflow.
The units that matter
pxis an absolute CSS pixel. Fine for borders and hairlines, poor for text.remis relative to the root font size, so it follows the reader’s preference.emis relative to the current element’s font size, useful for component-relative padding.%is relative to the parent’s size, useful for widths.vwandvhare percentages of the viewport, useful for full-bleed sections.chis the width of a zero in the current font, useful for line length.
Editable. Runs in your browser.
<div class="box">
<h2>Relative sizing</h2>
<p>The box is 80 percent wide but never wider than 60 characters.</p>
</div>
Press Run to see the output.
Percentage padding is calculated from the container’s width, not its height, which surprises people trying to make a box taller. Use padding-block with rem for vertical space in most cases.
80% with max-width is a robust pattern: fluid on small screens, capped on large ones. width: 80vw would ignore the scrollbar and cause horizontal overflow on some platforms.
Fluid type with clamp
clamp(minimum, preferred, maximum) keeps a value inside a range while letting it scale.
Editable. Runs in your browser.
<h1>Fluid, but bounded</h1>
<p>This text grows with the viewport and stops before it becomes uncomfortable.</p>
Press Run to see the output.
The preferred value uses vw plus a rem base so it still scales when the user zooms text. A pure vw value does not respond to zoom, which is an accessibility problem.
Do not make body text fluid across a huge range. Sizes between 1rem and 1.125rem are plenty; headings can move more.
Media defaults
Two rules prevent most responsive breakage:
img, video, svg { max-width: 100%; height: auto; }
max-width: 100% stops media overflowing its container. height: auto preserves the aspect ratio when the width shrinks. With width and height attributes in the HTML, the browser can still reserve space.
For a full-height section on mobile, 100vh is unreliable because browser chrome changes the visible area. 100dvh uses the dynamic viewport height and is the better choice where supported, with 100vh as a fallback.
Breakpoints come from content
Do not pick device widths from a list. Resize the window and watch for the point where the layout becomes cramped or the line length becomes unreadable. That is your breakpoint.
A common pair is around 40rem for a single-column-to-two-column change and 64rem for wider spacing, but the numbers matter less than the reason. Media queries are covered properly in lesson 20.
Text, zoom and overflow
Test at 200 percent zoom. Fixed widths, fixed heights and white-space: nowrap are the usual culprits when content is clipped.
Editable. Runs in your browser.
<p>A long value such as <span class="tag">a-very-long-unbroken-identifier-that-would-overflow</span> wraps instead of forcing the page sideways.</p>
Press Run to see the output.
overflow-wrap: anywhere lets an unbroken string break. It is the right fix for user-generated content such as URLs and IDs, where you cannot insert spaces.
Using vw for font sizes without a rem component means the text does not grow when the user zooms, because viewport units are tied to the viewport rather than the font setting. Always include a rem term in fluid type, as the clamp example does.
Write a hero section with a fluid heading between 2rem and 3.5rem, body text at 1rem, padding that scales with the viewport but stays between 1rem and 3rem, and an image that never exceeds its container.
Show solution
Editable. Runs in your browser.
<section class="hero">
<h1>Responsive by default</h1>
<p>Fluid where it helps, bounded where it matters.</p>
<img src="https://picsum.photos/id/1016/900/400" alt="A canyon at sunset" width="900" height="400">
</section>
Press Run to see the output.
Every value has a floor and a ceiling, and the heading includes a rem term so zoom still works. The image is capped at its container’s width.
Recap
remfor text and spacing,%withmax-widthfor containers,pxfor hairlines.clamp()gives fluid sizes with a floor and a ceiling; include aremterm so zoom works.max-width: 100%on media andoverflow-wrapon long strings prevent most horizontal overflow.
Next, you will build two-dimensional layouts with CSS Grid.
Should I use 100vh for a full-screen hero?
What is the difference between em and rem?
Progress is saved in this browser only. No account, no email.