People visit on phones, tablets, laptops and huge monitors. Responsive design means one codebase that adapts to all of them. This chapter covers the tools that make it painless — plus the modern CSS features that remove whole categories of old hacks.
Responsive design
Step one is the viewport meta tag from the HTML course — without it nothing else works. Step two is designing with fluid, not fixed, sizes: percentages, fr, max-width, and flexible grids that wrap. Media queries handle what's left.
Media queries & mobile-first
A @media block applies styles only when a condition is true — usually a minimum screen width. The professional habit is mobile-first: write the phone layout as the base, then add complexity at larger widths:
/* Base: phone. One column. */.grid { display: grid; grid-template-columns: 1fr; gap: 16px; }/* Tablet and up: two columns */@media (min-width: 640px) {.grid { grid-template-columns: repeat(2, 1fr); }}/* Desktop and up: three columns */@media (min-width: 1024px) {.grid { grid-template-columns: repeat(3, 1fr); }}
Custom properties (CSS variables)
Define a value once and reuse it everywhere. Custom properties live on a selector (commonly :root) and are read with var():
:root {--brand: #2563eb;--text: #0f172a;--radius: 12px;--space: 16px;}.button {background: var(--brand);border-radius: var(--radius);padding: var(--space);}/* Change the theme by overriding variables — every use updates at once */[data-theme="dark"] { --brand: #4f7cff; --text: #f8fafc; }
This is exactly how a design system stays consistent — and how dark mode is often built. One variable change re-themes the whole site.
clamp, transitions & more
Three modern features you'll use constantly:
/* Fluid font size: min 1.75rem, scale with viewport, max 3rem —responsive typography with no media queries */h1 { font-size: clamp(1.75rem, 5vw, 3rem); }/* Smooth hover: animate only cheap properties */.button {transition: background 0.2s ease, transform 0.2s ease;}.button:hover { transform: translateY(-2px); }/* Respect users who ask for less motion */@media (prefers-reduced-motion: reduce) {.button { transition: none; }}
clamp(min, preferred, max)— a value that grows with the viewport but stays inside bounds.transition— animate a property change smoothly; animatetransform/opacityfor performance.prefers-reduced-motion— an accessibility media query; honour it and disable big animations.
Practice
Take your programme-card grid and make it fully responsive: one column on phones, two on tablets, three on desktop. Pull your colours and spacing into :root variables, size the heading with clamp(), and add a smooth hover lift that respects prefers-reduced-motion.
Key takeaways
- 01Responsive = one codebase that adapts: viewport meta tag, fluid sizes, and media queries for the rest.
- 02Work mobile-first: base styles for phones, then add complexity with min-width media queries.
- 03CSS custom properties (--name / var()) are design tokens — the basis of consistency and theming.
- 04clamp() gives fluid, bounded values (great for typography) without media queries.
- 05Add transitions for polish, animate transform/opacity, and always honour prefers-reduced-motion.
