Responsive Design

Build layouts that work from small phones to wide monitors using fluid sizing, media queries and responsive images.

Beginner · about 5 hours · difficulty 2/5

Learn first: CSS Grid

Responsive design means one codebase that adapts to any screen, input method and user preference. Most traffic to consumer sites is mobile, so this is not optional.

Principles

  • Mobile first: write base styles for small screens, then add @media (min-width: 48rem) rules as space grows. It keeps CSS simpler than overriding desktop styles downward.
  • Fluid over fixed: percentages, fr, minmax() and clamp() reduce the number of breakpoints you need.
  • Choose breakpoints where your content breaks, not at specific device widths.
  • Tap targets should be at least about 44px; hover is not available on touch devices, so never hide essential actions behind it (@media (hover: hover) helps).

Responsive media

  • img { max-width: 100%; height: auto; } stops overflow.
  • srcset and sizes let the browser download the right resolution; <picture> swaps formats or art direction.

User preference queries

  • prefers-color-scheme for dark mode.
  • prefers-reduced-motion to tone down animation for people with vestibular disorders.
  • prefers-contrast for higher contrast.

Testing

Use DevTools device mode, but also test on a real phone: viewport height with browser chrome (100vh vs 100dvh), virtual keyboards and touch behaviour differ from emulation.

Move on when a page you built has no horizontal scroll at 320px wide, reads well at 1440px, and respects dark mode and reduced motion.

Key concepts

  • Mobile-first media queries
  • Content-driven breakpoints
  • Fluid layouts and clamp()
  • Responsive images with srcset and sizes
  • prefers-color-scheme and dark mode
  • prefers-reduced-motion
  • Touch targets and hover media
  • Device mode and real-device testing

Mini project

Build a responsive blog article page with a collapsible nav, fluid typography, a responsive hero image using srcset, and a dark theme driven by prefers-color-scheme.

Check yourself

Why is mobile-first CSS usually shorter than desktop-first CSS?

Free resources