Chapter 10 of 15

Responsive Web Design: Make Websites Work on Mobile

More than half of all visits come from phones. A responsive website changes its layout to fit any screen.

Step 1: the viewport tag

Put this line in the <head> of every page. Without it, phones show a tiny zoomed-out desktop page.

Two phones: one with tiny unreadable text, one with normal readable text
Without the viewport tag, phones show a tiny zoomed-out page.
HTML
<meta name="viewport" content="width=device-width, initial-scale=1">

Step 2: flexible sizes

Do not use fixed widths like width: 1000px. Use flexible ones:

The same page as 3 columns on a laptop, 2 on a tablet and 1 on a phone
A responsive page changes its layout to fit the screen. Same HTML, different CSS.
CSS
.container {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 16px;
}
img { max-width: 100%; height: auto; }

Step 3: media queries

A media query adds CSS only when the screen is a certain size.

A ruler from 0 to 1000 pixels with layers of CSS that switch on at 600 and 1000 pixels
Mobile first: phone styles for all, then extra rules as the screen gets wider.
CSS
/* Phone first: one column */
.cards { display: grid; gap: 16px; }

/* Tablet and up: two columns */
@media (min-width: 600px) {
  .cards { grid-template-columns: 1fr 1fr; }
}

/* Laptop and up: three columns */
@media (min-width: 1000px) {
  .cards { grid-template-columns: 1fr 1fr 1fr; }
}

The sizes where the layout changes are called breakpoints. Do not pick them by phone model. Make the browser narrow and add a breakpoint where your design starts to look bad.

Mobile first

Write the simple phone styles first, with no media query. Then add min-width media queries for bigger screens. This keeps your CSS short and makes phones load fast. Google also ranks sites by their mobile version (it is called mobile-first indexing).

Text that grows with the screen: clamp()

CSS
h1 {
  font-size: clamp(28px, 5vw, 52px);
}

Read it as: "at least 28px, ideally 5% of the screen width, but never more than 52px". The heading grows smoothly from phone to laptop, with no media query at all. Use it for big headings and section spacing.

Phones have no hover

On a phone you cannot hover. If important information only appears on :hover, phone users will never see it. Show important things all the time, and use hover only for small extras like a colour change.

CSS
@media (hover: hover) {
  .card:hover { transform: translateY(-4px); }  /* only on devices with a mouse */
}

Wide tables on small screens

A table with many columns is wider than a phone. Wrap it in a box that scrolls sideways, so the rest of the page stays in place:

HTML
<div class="table-wrap">
  <table>...</table>
</div>
CSS
.table-wrap { overflow-x: auto; }

How to test

  1. Open your page in Chrome. Press F12 (or right-click → Inspect).
  2. Click the small phone-and-tablet icon at the top of the panel.
  3. Pick different phones, or drag the edge to change the width.
  4. Also test on your real phone, because touch feels different from a mouse.
Touch-friendlyMake buttons and links at least 44px tall so fingers can tap them easily. Body text should be at least 16px on phones.

Mini project: make a landing page responsive

Take a simple page with a header, three feature cards and a footer. Make it responsive in four steps:

  1. Add the viewport meta tag.
  2. Wrap the content in .container { max-width: 1100px; margin: 0 auto; padding: 0 16px; }.
  3. Make the cards one column by default, and three columns at min-width: 800px.
  4. Use clamp() for the main heading.
CSS
.features { display: grid; gap: 16px; }
@media (min-width: 800px) {
  .features { grid-template-columns: repeat(3, 1fr); }
}

Test it at 360px, 768px and 1280px wide in the browser's phone view. Nothing should scroll sideways.

Common mistakes

ProblemFix
Page looks tiny on a phoneThe viewport meta tag is missing
Page scrolls sideways on a phoneSomething has a fixed width. Find it in DevTools and use max-width.
Text is too small to readKeep body text at least 16px
Buttons are hard to tapMake them at least 44px tall with space between them

Practice time

  1. Add the viewport tag to all your pages.
  2. Make a card grid: 1 column on phone, 2 on tablet, 3 on laptop.
  3. Test your site in the browser's phone view.
  4. Use clamp() for your h1 and check it on three screen sizes.
  5. Build the <details> mobile menu.

Quick summary

  • Always add the viewport meta tag.
  • Use max-width, %, rem instead of big fixed widths.
  • Write phone styles first, then add min-width media queries.