Lesson 4 of 12 · 9 min · HTML and CSS

Responsive design: one page for every screen

Covers: Current HTML and CSS specifications. Updated 13 Nov 2026. Part of the Frontend Developer roadmap.

What you will learn

  • Why the viewport meta tag is required
  • Mobile-first design with min-width media queries
  • Fluid layouts, spacing and type with clamp()
  • Responsive images: srcset, sizes, <picture>, object-fit
  • Container queries and user-preference media queries; test properly

In simple words

People open your page on a small phone, a tablet, a laptop and a wide monitor. Responsive design means writing one page that rearranges itself to suit each screen, instead of building separate sites. In India, most visitors are on phones, often mid-range Android phones on slower networks, so the small screen is where you start, not the afterthought.

Words to know

  • Viewport: the visible area of the page in the browser window.
  • Breakpoint: a screen width at which the layout changes.
  • Media query: a CSS condition, such as a minimum width, that switches styles on or off.
  • Mobile-first: writing the small-screen styles first, then adding enhancements for larger screens.

The viewport meta tag

Without it, a phone pretends to be a wide desktop (about 980px) and shrinks your page to fit. Always include this in <head>:

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

Do not add user-scalable=no or maximum-scale=1: they stop people zooming, which fails accessibility guidelines.

Mobile-first and media queries

Write base styles for the smallest screen, with no query. Then add min-width queries that enhance the layout as space grows:

/* base: phones */
.cards { display: grid; gap: 1rem; }
.page  { padding: 1rem; }

/* tablets and up */
@media (min-width: 40rem) {                 /* about 640px */
  .cards { grid-template-columns: repeat(2, 1fr); }
}

/* laptops and up */
@media (min-width: 64rem) {                 /* about 1024px */
  .cards { grid-template-columns: repeat(3, 1fr); }
  .page  { padding: 2rem; }
}

Why mobile-first: phones download less CSS that they have to override, the simplest layout (one column) is the default, and large screens get the extras. Common breakpoints (about 640, 768, 1024 and 1280 pixels) are only a guide: choose breakpoints where your content starts to look wrong, not for specific devices. Use em or rem in media queries so they respect user font-size settings.

Fluid layouts

Avoid fixed widths. Let content flow:

img, video { max-width: 100%; height: auto; }          /* media never overflows its container */

.container {
  width: min(100% - 2rem, 70rem);                      /* up to 70rem wide with 1rem margins */
  margin-inline: auto;
}

p { max-width: 65ch; }                                 /* comfortable line length for reading */

Flexbox with wrapping and Grid with repeat(auto-fit, minmax(240px, 1fr)) (the previous lesson) give responsive layouts without any media query. Use percentages, fr, rem and min()/max()/clamp() instead of fixed pixel widths.

Fluid typography and spacing with clamp(min, preferred, max):

h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3rem); }
.section { padding-block: clamp(2rem, 6vw, 5rem); }

The heading grows smoothly with the viewport but never below or above the limits. Combine rem with vw in the middle term so users' zoom and text-size settings still work.

Responsive images

A 2000px photo is wasted on a 360px phone and slows the page. Let the browser choose:

<img
  src="/img/hero-800.jpg"
  srcset="/img/hero-400.jpg 400w, /img/hero-800.jpg 800w, /img/hero-1600.jpg 1600w"
  sizes="(min-width: 64rem) 50vw, 100vw"
  width="800" height="450"
  alt="Students practising mock interviews"
  loading="lazy" decoding="async">
  • srcset lists the available files and their widths; sizes tells the browser how wide the image will be displayed, so it picks the smallest adequate file (and considers screen density).
  • <picture> with <source> changes the image itself for different screens (art direction) or serves modern formats:
<picture>
  <source type="image/avif" srcset="/img/hero.avif">
  <source type="image/webp" srcset="/img/hero.webp">
  <img src="/img/hero.jpg" alt="..." width="800" height="450">
</picture>
  • Always set width and height (or aspect-ratio in CSS) to avoid layout shift.
  • object-fit: cover makes an image fill a fixed box without distortion.
  • Use SVG for logos and icons (scales perfectly, small file), and CSS for simple shapes.
  • Use loading="lazy" for images below the fold, and fetchpriority="high" for the main hero image.

Container queries

A media query asks about the screen; a container query asks about the component's own space. A card in a narrow sidebar can show one layout and the same card in a wide main area another:

.card-wrap { container-type: inline-size; }

@container (min-width: 28rem) {
  .card { display: grid; grid-template-columns: 8rem 1fr; gap: 1rem; }
}

Container queries are supported in all current major browsers and are the right tool for reusable components.

User-preference queries

Respect what the user has asked their device for:

@media (prefers-color-scheme: dark)   { :root { --bg: #111; --text: #eee; } }
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
@media (prefers-contrast: more)       { :root { --border: #000; } }
@media (hover: hover) and (pointer: fine) { .btn:hover { background: #3949ab; } }   /* hover only where it exists */
@media print { nav, aside { display: none; } }

Touch devices have no hover: never hide essential content behind :hover. Make touch targets at least about 44 by 44 CSS pixels (the WCAG 2.2 minimum is 24 by 24; larger is kinder on phones).

Responsive navigation and tables

  • A horizontal menu of six links will not fit on a phone: collapse it into a button that toggles a list (a <button aria-expanded> and CSS), or use a bottom navigation bar for apps.
  • Wide tables: wrap in a container with overflow-x: auto, or restack rows as cards on small screens.
  • Forms: one column on phones, labels above fields, large inputs with the right type and inputmode so the correct keyboard appears.
  • Avoid horizontal scrolling of the whole page: it is almost always a fixed-width element, a wide image or long unbreakable text (overflow-wrap: anywhere).

Testing

  • Browser DevTools device mode (Ctrl+Shift+M): resize, choose device presets, throttle the network and CPU.
  • A real phone, ideally a mid-range Android, on a real mobile connection. Emulators do not show how it feels.
  • Test portrait and landscape, 200 percent zoom, larger default text, dark mode and a keyboard.
  • Look for: horizontal scrolling, overlapping text, tiny tap targets, images that are too large, text under 16px that triggers zoom in form fields.
  • Lighthouse (in DevTools) flags a missing viewport tag, small tap targets and illegible text.

Common mistakes

  • No viewport meta tag.
  • Desktop-first CSS with many max-width overrides.
  • Fixed pixel widths and heights.
  • Huge images served to phones, and no width/height.
  • Hiding important content on mobile instead of reorganising it.
  • Hover-only menus.
  • Only testing in a resized desktop window.

Try it yourself

  1. Take a fixed-width layout and make it fluid with max-width, percentages and min().
  2. Write the base (phone) styles for a three-card section, then add min-width queries for two and three columns.
  3. Add srcset and sizes to a hero image and check in DevTools' Network panel which file loads at 400px and at 1400px.
  4. Make the heading and section padding fluid with clamp().
  5. Test the page on a real phone and write down three problems you found.

Check yourself

Why is the viewport meta tag needed?

Without it, mobile browsers render the page at a desktop width and scale it down; the tag makes the layout width match the device width.

What does mobile-first mean in CSS?

Writing the base styles for small screens and adding min-width media queries to enhance the layout for larger ones.

What do srcset and sizes do?

srcset lists image files with their widths; sizes states how wide the image will display, so the browser can download the smallest file that looks sharp.

What is the difference between a media query and a container query?

A media query responds to the viewport or device; a container query responds to the size of a component's container.

Interview questions

How do you make a website responsive?

Use the viewport meta tag, a mobile-first approach with min-width media queries, fluid layouts (flexbox, grid, %, rem, clamp()), responsive images with srcset and sizes, flexible media, and test on real devices.

What is the difference between em and rem in media queries and font sizes?

rem is relative to the root font size and so is predictable; em is relative to the current element and compounds with nesting. In media queries, both respect user font settings, unlike px.

How do you serve different images to different devices?

With srcset and sizes for resolution switching, or <picture> with <source> for different formats or crops.

Summary

  • Always include the viewport meta tag; design mobile-first with min-width queries at content-driven breakpoints.
  • Prefer fluid layouts (flex, grid, min(), clamp()) to fixed widths.
  • Serve appropriately sized images with srcset, sizes and <picture>, and set dimensions.
  • Use container queries for components and user-preference queries; test on real phones.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on HTML, CSS and how the web works for Frontend Developer and scores every answer, with what to fix.