Sub topic 3
Fluid Typography and Adaptive Components
Assume you know basic HTML, CSS selectors, the box model, and browser developer tools. This chapter shows how typography, input methods, component containers, preferences, performance, and testing fit into a mobile-first stylesheet.
Fluid typography with `clamp()`
Fluid typography changes smoothly as the viewport grows. clamp(minimum, preferred, maximum) sets a lower limit, a flexible value, and an upper limit.
body {
font-size: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
}
h1 {
font-size: clamp(2rem, 1.4rem + 3vw, 4rem);
}The browser keeps body text between 1rem and 1.25rem. It uses the middle expression while there is enough space. Relative units such as rem respect the user's root font size. Avoid making text depend only on viewport width.
Designing for touch and pointer capability
Touch users need controls large enough to activate without precision. A minimum block size of 2.75rem is a useful starting point. Do not make hover the only way to discover or operate a control.
.button {
min-block-size: 2.75rem;
padding-inline: 1rem;
}
@media (hover: hover) and (pointer: fine) {
.button:hover {
background: #e8eefc;
}
}The media query applies hover styling only when the primary input supports accurate hovering.
Container queries for reusable components
A viewport breakpoint describes the whole page. A container query describes the available space around one component. This helps a card work in both a sidebar and a main column.
.card-list {
container-type: inline-size;
}
.card {
display: block;
}
@container (min-width: 40rem) {
.card {
display: flex;
gap: 1rem;
}
}Respecting user preferences
Media features can reflect operating-system settings. Disable motion when a user requests reduced motion.
.banner {
animation: slide-in 400ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
.banner {
animation: none;
}
}Keeping mobile pages fast
Use responsive images with srcset and sizes so the browser downloads an appropriate file. Include dimensions to reserve space and prevent layout shift.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 48em) 50vw, 100vw"
width="800"
height="600"
alt="A hiker on a mountain ridge">Also compress images, avoid unnecessary fonts, and remove expensive animations on small devices.
Testing on emulators and real devices
Use browser developer tools to test narrow, wide, and intermediate widths. Check for horizontal scrolling, cramped controls, unreadable text, and layout changes between breakpoints. Test keyboard navigation, touch behavior, reduced motion, and slow network conditions. Emulators reveal layout problems, but real devices reveal touch, performance, and browser differences.
Exercise
Why should a component use a container query instead of a viewport media query when its layout depends on its parent column?
Show answer
A container query responds to the component's available width, so the component remains reusable in differently sized page regions.