beginner

Responsive Design: One Page, Every Screen

Make a layout work on phones, tablets and desktops: the viewport tag, fluid units, media queries, mobile-first CSS, flexible images and clamp() for fluid type. Practise by making a card grid respond to screen width.

40 min

1. People do not all see your page the same way

Your page will be opened on a 360px phone, a 768px tablet, a 1440px laptop and a 4K monitor, in portrait and landscape, with the user’s own font size. A responsive page adapts instead of breaking. In many markets most visitors are on phones, so this is not an extra, it is the main case.

Three ingredients make a page responsive:

  1. A flexible grid and flexible units, so content reflows.
  2. Flexible media, so images never overflow.
  3. Media queries, so the layout can change at the points where it needs to.

2. The viewport tag

Put this in every page’s <head>:

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

Without it, phones assume a desktop-width page (about 980px) and shrink everything to fit, so your carefully written media queries never fire. With it, one CSS pixel is a sensible size on the device.

3. Units that flex

Unit Relative to Good for
% The parent’s size Widths of columns and containers
rem The root font size (usually 16px, and it respects the user’s browser setting) Font sizes, spacing, most things
em The element’s own font size Padding that should scale with the text
vw / vh 1% of the viewport width / height Large fluid sizes, full-height sections
fr A share of leftover space in a grid Grid columns
ch The width of the “0” glyph Line length (max-width: 65ch) for comfortable reading

Avoid fixed px widths for containers: width: 960px forces a horizontal scroll bar on a 360px phone. Prefer max-width with a flexible width:

.container {
  width: min(100% - 2rem, 70rem);   /* fills small screens with a 1rem margin each side, caps at 70rem */
  margin-inline: auto;
}

4. Flexible images

img, video {
  max-width: 100%;
  height: auto;       /* keep the proportions */
  display: block;
}

For photos that should show a different file or size per screen, use srcset and sizes (the browser downloads the smallest file that fits), and always set width and height attributes so the browser can reserve space and the page does not jump as images load:

<img src="photo-800.jpg"
     srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
     sizes="(min-width: 60rem) 50vw, 100vw"
     width="800" height="500" alt="Students building a network lab">

5. Media queries

A media query applies CSS only when a condition is true:

@media (min-width: 40rem) {
  .cards { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 64rem) {
  .cards { grid-template-columns: repeat(4, 1fr); }
}

Other useful conditions: (prefers-color-scheme: dark) for a dark theme, (prefers-reduced-motion: reduce) to calm animations for people who ask for it, (orientation: landscape), print, and (hover: hover) for devices with a real pointer.

Mobile-first

Write the base CSS for the smallest screen (usually a simple single column) and use min-width queries to add layout as space becomes available. The opposite approach (desktop first, then max-width queries to undo things) tends to produce more CSS and a worse phone experience, because phones load and then override desktop rules.

Choose breakpoints from your content, not from device names: widen the browser until the layout starts to look wrong, and put a breakpoint there. Using rem or em in queries respects users who enlarge their text.

6. Layout that adapts without queries

Modern CSS often removes the need for a query:

/* As many 16rem-wide columns as fit; they share leftover space */
.cards {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}

/* Flex items that wrap */
.row { display: flex; flex-wrap: wrap; gap: 1rem; }
.row > * { flex: 1 1 14rem; }

And fluid type with clamp():

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

Container queries (@container) go one step further: a component responds to the width of its container rather than the whole screen, which makes reusable components truly portable. They are supported in all current browsers.

7. Test on real sizes

  • Use the browser’s device toolbar (DevTools, then the phone/tablet icon) and drag the width from 320px up to 1600px, watching for horizontal scrolling, overlapping text and tiny tap targets.
  • Make touch targets at least about 44 by 44 CSS pixels.
  • Test with a larger browser font size and in landscape.
  • Try on a real phone. Emulators miss touch behaviour, slow networks and keyboard overlap.

8. Your turn: make the cards responsive

The page below shows a set of .card items in a single column. Make the .cards container:

  • a grid with a gap,
  • 1 column by default (mobile-first),
  • 2 columns from 40rem wide, and 4 columns from 64rem wide, using two min-width media queries,
  • and make the images flexible with max-width: 100%.
Mobile-first card grid
PREVIEW

Your tasks

  • ○.cards is a grid
  • ○.cards has a gap
  • ○A min-width media query for 40rem (or 640px) exists
  • ○A min-width media query for 64rem (or 1024px) exists
  • ○The 40rem rule sets 2 columns and the 64rem rule sets 4
  • ○Images use max-width: 100%

9. What you learned

You set the viewport, chose flexible units, made media flexible and wrote mobile-first media queries. You also saw that modern CSS (auto-fit, clamp(), min()) can adapt with no query at all. Next, you will bring pages to life with animation.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for Responsive Design: One Page, Every Screen. What would you like to know?
📝

Knowledge Check

1 of 4

What does <meta name="viewport" content="width=device-width, initial-scale=1"> do?

Report a problem

About: /lessons/css-responsive-design