Skip to content
skillbld
HTML & CSS · 14-day planAll days
Day 12 of 14 30 min

Make pages that work on any screen, from a small phone to a wide monitor. Add the viewport meta tag, design mobile-first, change layouts with media queries, use fluid sizes with percentages, clamp and max-width, and make images and navigation adapt.

By the end of today you can

  • Add the viewport meta tag and explain what it does
  • Design mobile-first and add media queries for larger screens
  • Change a Grid layout and a navbar at a breakpoint
  • Use fluid sizes with %, max-width and clamp()
  • Test pages at different widths

Most visitors to your site will be on a phone. Responsive design means one page that adapts to every screen: a single column on a phone, more columns on a laptop. Today you'll learn the tools.

Mind map · click a dot to fold, drag to pan

Step 1: the viewport tag

Every responsive page needs this in its <head>:

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

Without it, phones pretend to be a 980px-wide desktop and shrink the whole page down until the text is tiny. With it, the page is laid out for the phone's real width. (The previews in this course add it for you.)

Step 2: fluid by default

Flexible units let layouts adapt without any special rules:

index.html · edit it, the preview updates
Page preview· 320px wide

Switch between Phone, Tablet and Desktop above the preview. No media queries yet, and it already works at every width:

  • max-width instead of width for containers.
  • img { max-width: 100%; height: auto; } for images.
  • clamp(min, preferred, max) for sizes that scale within limits.

Step 3: media queries

Sketch
Mobile-first responsive design. The base CSS styles a phone: one column of stacked cards and a stacked menu. At a min-width: 600px media query, the cards form two columns. At min-width: 1000px, three columns and a horizontal menu. Each media query adds to the styles before it as the screen gets wider.

A media query applies CSS only when a condition is true, usually the window width:

index.html · edit it, the preview updates
Page preview· 320px wide

Switch widths: one column on a phone, two on a tablet, three on a desktop.

Mobile-first

The base CSS (outside any media query) is for phones. Each min-width query adds to it for bigger screens. Starting small keeps the CSS simpler: phones get the plain one-column layout, and you add columns as space appears.

QueryApplies when the window is…
@media (min-width: 600px)600px or wider
@media (max-width: 599px)599px or narrower
@media (prefers-color-scheme: dark)The user chose dark mode
@media (prefers-reduced-motion: reduce)The user wants less animation

Breakpoints come from your content. Don't chase device sizes; make the window narrower and add a breakpoint where the layout starts to look wrong.

A responsive page layout

Yesterday's grid-areas layout, now mobile-first:

index.html · edit it, the preview updates
Page preview· 320px wide

On a phone the areas stack (with the main content before the sidebar); from 700px they sit side by side.

A navbar that adapts

index.html · edit it, the preview updates
Page preview· 320px wide

On phones the links wrap below the logo; on wider screens they sit on the right. (A "hamburger" menu that opens and closes needs a little JavaScript, a topic for the JavaScript course.)

Testing

  • Use the width buttons on these previews.
  • On a computer, open DevTools → device toolbar (Ctrl+Shift+M / Cmd+Shift+M) to try phone sizes, or just drag your browser window narrower.
  • Check on a real phone: tap targets should be at least about 44×44px, and text at least 16px.

Recap

  • Always include the viewport meta tag.
  • Be fluid: max-width, %, img { max-width: 100% }, clamp().
  • Media queries: @media (min-width: 600px) { … }.
  • Mobile-first: base styles for phones, min-width queries add for bigger screens.
  • Breakpoints come from your content, not from device lists.

Tomorrow: positioning, transitions and making pages work for everyone.

Knowledge check

Test what you learned

5 questions · pick an answer to see if you're right

0/5
  1. 1

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

  2. 2

    What does mobile-first mean?

  3. 3

    When does @media (min-width: 768px) { … } apply?

  4. 4

    What does font-size: clamp(1.5rem, 5vw, 3rem) do?

  5. 5

    Where should breakpoints (the widths in media queries) come from?

Was this lesson helpful?

Need a breather? Take a two-minute break

Keep going