HTML and CSS

Responsive Web Design Using HTML and CSS: A Practical Guide

Build responsive layouts with the viewport meta tag, fluid units, Flexbox, Grid and media queries, with a complete mobile-first example.

More than half of web traffic comes from phones, so a layout that only works on a desktop monitor is a broken layout. Responsive web design means building one page that adapts to any screen size using HTML and CSS alone. This guide covers the techniques that matter most, then puts them together in a complete mobile-first example you can copy.

Step 1: the viewport meta tag

Without this one line, mobile browsers pretend the page is about 980 pixels wide and shrink it down. Put it in the head of every page:

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

Do not add user-scalable=no or maximum-scale=1. Blocking zoom hurts users with low vision.

Step 2: use flexible units

Fixed pixel widths break on small screens. Prefer relative units that scale with their context.

  • % sizes an element relative to its parent.
  • rem scales with the root font size, which respects the user’s browser settings.
  • vw and vh scale with the viewport.
  • ch equals the width of the “0” character, which is perfect for limiting line length.
img, video {
  max-width: 100%;
  height: auto;   /* images never overflow their container */
}

.container {
  width: min(100% - 2rem, 70rem); /* 1rem gutters, never wider than 70rem */
  margin-inline: auto;
}

p {
  max-width: 70ch; /* comfortable reading width */
}

Step 3: fluid typography with clamp()

The clamp(min, preferred, max) function lets text scale smoothly between two limits without any media queries.

h1 {
  font-size: clamp(2rem, 1.2rem + 3vw, 3.5rem);
}

Step 4: Flexbox for one-dimensional layouts

Flexbox arranges items in a row or column and handles spacing and wrapping. It is ideal for navigation bars, button groups and card rows.

.nav {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
}

Step 5: CSS Grid for two-dimensional layouts

Grid controls rows and columns together. One line gives you a card layout that adapts to the available width with no media query at all:

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr));
  gap: 1.5rem;
}

Each card is at least 18rem wide; the browser fits as many as it can per row and stretches them to fill the space. The min(100%, 18rem) part stops cards overflowing on very narrow screens.

Step 6: media queries for the bigger changes

Media queries apply CSS only when a condition is true, such as a minimum screen width. Write your base styles for small screens first (mobile-first) and add enhancements as space allows.

.layout { display: block; }

@media (min-width: 48em) {   /* about 768px */
  .layout {
    display: grid;
    grid-template-columns: 16rem 1fr;
    gap: 2rem;
  }
}

Choose breakpoints where your content starts to look wrong, not at specific device widths. Devices change every year; your content does not. The MDN guide to using media queries covers every available feature.

Complete mobile-first example

This page has a header, a main column with a sidebar that appears on wider screens, and a responsive card grid. Save it as index.html and resize the browser window.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Responsive demo</title>
  <style>
    * { box-sizing: border-box; }
    body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.6; }
    .container { width: min(100% - 2rem, 70rem); margin-inline: auto; }
    header { padding-block: 1rem; background: #1e1b4b; color: #fff; }
    .layout { display: grid; gap: 2rem; padding-block: 2rem; }
    .cards { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); }
    .card { padding: 1rem; border: 1px solid #ddd; border-radius: 12px; }
    @media (min-width: 48em) {
      .layout { grid-template-columns: 1fr 16rem; }
    }
  </style>
</head>
<body>
  <header><div class="container"><strong>My site</strong></div></header>
  <div class="container layout">
    <main>
      <h1>Latest articles</h1>
      <div class="cards">
        <article class="card"><h2>One</h2><p>Short summary.</p></article>
        <article class="card"><h2>Two</h2><p>Short summary.</p></article>
        <article class="card"><h2>Three</h2><p>Short summary.</p></article>
      </div>
    </main>
    <aside><h2>About</h2><p>Sidebar content sits below on phones, beside on tablets and up.</p></aside>
  </div>
</body>
</html>

Responsive images

Large images waste data on phones. Use srcset and sizes so the browser picks the best file, and always set width and height so the page does not jump while loading.

<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(min-width: 48em) 50vw, 100vw"
  width="800" height="533"
  loading="lazy"
  alt="Describe what the photo shows">

Testing your layout

  • Open browser DevTools and use the device toolbar, then drag the width from 320px up to 1440px and watch for overflow, overlapping text and tiny tap targets.
  • Test on at least one real phone. Emulators do not reproduce touch behaviour perfectly.
  • Check that tap targets are at least 44 by 44 pixels and that text stays readable at 200% zoom.
  • Run Lighthouse and look at the mobile performance and accessibility scores.

Common mistakes

  • Forgetting the viewport meta tag, so none of your media queries behave as expected on phones.
  • Hiding content on mobile instead of designing it to fit.
  • Using fixed widths in pixels for containers and images.
  • Writing desktop-first CSS with many max-width overrides, which gets messy fast.
  • Making horizontal scroll appear because one wide element (often a table or code block) is not constrained. Add overflow-x: auto to wrappers around them.

Next steps

Once your layout adapts, add interactivity with the fundamentals from the JavaScript tutorial for beginners. For a broader walkthrough, the free Learn Responsive Design course on web.dev goes further into patterns and accessibility.