How do you build a responsive layout? Explain media queries, container queries and fluid units.
Mobile-first media queries
Write the base styles for small screens, then add @media (min-width: 768px) overrides. Also respond to user preferences: prefers-color-scheme, prefers-reduced-motion, hover: hover and pointer: coarse.
Container queries
@container (min-width: 400px) styles a component by its container's size, not the viewport's, so the same card works in a narrow sidebar and a wide main column. The parent needs container-type: inline-size. Supported in all modern browsers.
Fluid units and functions
remfor type and spacing (respects the user's font size),%,fr,vw/vh.dvh/svh/lvhfix the mobile100vhbug caused by browser toolbars.clamp(min, preferred, max)for fluid typography:font-size: clamp(1rem, 0.8rem + 1vw, 1.5rem).min()andmax()for sizes such aswidth: min(100%, 70ch).
Images
srcset and sizes let the browser pick the right resolution; <picture> switches formats (AVIF, WebP) or art direction. Always set width and height (or aspect-ratio) to prevent layout shift.
Example
.cards { container-type: inline-size; }
.card { display: grid; gap: 12px; }
@container (min-width: 420px) {
.card { grid-template-columns: 120px 1fr; } /* side-by-side when the slot is wide */
}
h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }
.hero { min-height: 100dvh; }
@media (prefers-reduced-motion: reduce) {
* { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}<img src="hero-800.jpg" srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="(min-width: 1024px) 50vw, 100vw" width="1600" height="900" alt="Team at work" />Follow-up questions interviewers ask
Try answering each one before you open it.
Why must the viewport meta tag be present?
Without <meta name="viewport" content="width=device-width, initial-scale=1"> mobile browsers render the page at a desktop width (about 980px) and scale it down, so media queries never match the real device width. Do not disable zoom with user-scalable=no; it fails accessibility guidelines.