Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

CSS
CSS
0/10 completed
Beginner
Selectors and propertiesColors and fontsThe box modelDisplay and positioning
Intermediate
FlexboxCSS GridResponsive design
Advanced
Transitions and animationsCSS variables and architecture
Pro
Modern CSS: :has(), container queries and layers
Tracks / CSS / Intermediate

Responsive design

More than half of all web traffic comes from phones. Responsive design means one website that adapts to every screen, from a 320px phone to a 4K monitor.

Step 0: the viewport meta tag

Without this tag, phones pretend to be about 980px wide and shrink everything to fit. Always include it:

html
<meta name="viewport" content="width=device-width, initial-scale=1">

Fluid by default

Most responsiveness comes from not fixing sizes:

css
img, video { max-width: 100%; height: auto; }   /* media never overflows */
.container { width: min(100% - 2rem, 1100px); margin-inline: auto; }  /* fluid with a cap */
.prose { max-width: 65ch; }                     /* comfortable reading line length */

Use flexbox wrapping and auto-fit grids, and many layouts need no media queries at all.

Media queries

Media queries apply CSS only when a condition is true:

HTML · CSS · live preview
Preview

Mobile-first

Write the base styles for small screens, then add min-width queries to enhance the layout for larger ones:

css
/* ✓ Mobile-first */
.nav { flex-direction: column; }
@media (min-width: 768px) { .nav { flex-direction: row; } }

/* ✗ Desktop-first means overriding everything for mobile */
.nav { flex-direction: row; }
@media (max-width: 767px) { .nav { flex-direction: column; } }

Why mobile-first?

  • Mobile layouts are simpler, so you add complexity instead of undoing it.
  • It's faster on phones, which load fewer overrides.
  • It forces you to prioritise the important content.

Common breakpoints (choose them by where your content breaks, not by device): 640px, 768px, 1024px, 1280px.

Fluid typography with clamp()

HTML · CSS · live preview
Preview

Responding to user preferences

HTML · CSS · live preview
Preview

Other useful media features: hover: hover (the device has a real mouse), pointer: coarse (touch), orientation: landscape, and print.

Touch-friendly design

  • Make tap targets at least 44×44px.
  • Don't rely on :hover for important content, because touch screens have no hover.
  • Leave enough space between links so fingers don't hit the wrong one.

Responsive images recap

html
<img src="photo-800.jpg"
     srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
     sizes="(min-width: 800px) 50vw, 100vw"
     alt="…" width="800" height="533">

(See the HTML track's Images, video and embeds lesson.)

Testing

  • DevTools device mode (Ctrl/⌘ + Shift + M in Chrome) previews phone sizes.
  • Test on a real phone too. Emulators don't show performance or touch issues.
  • Zoom the page to 200%: the layout should still work.

Practice

Make this blog layout responsive, mobile-first: one column on phones; on screens wider than 900px, the article (max 65ch) next to a 280px sidebar. Use clamp() for the heading size, and make the nav links stack on mobile and sit in a row on desktop.

HTML · CSS · live preview
Preview
✎ Check your understanding

1.What does mobile-first CSS mean?

2.What does clamp(1rem, 3vw, 2rem) return on a very wide screen?

← CSS Grid