Skip to content
Codewilds

Lesson 23 of 29 · Grid & Responsive Design

Responsive Web Design: Media Queries and the Viewport

medium20 XP3 min read

People visit your pages on phones, tablets, laptops and huge monitors. Responsive web design means building one page that adapts its layout to every screen, instead of a separate mobile site. The main tools are the viewport meta tag, flexible widths and CSS media queries.

The viewport meta tag

By default, phones pretend to be about 980px wide and zoom out, so desktop pages still fit on the screen, just tiny. This line in <head> tells the browser to use the real width of the device instead:

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

Every responsive page needs it. Without it, your styles for small screens never kick in on a real phone.

Flexible widths

A fixed width: 960px forces phone users to scroll sideways. Use max-width instead: the element is at most that wide, but it shrinks on smaller screens. Add margin: 0 auto to centre it, because auto side margins share the leftover space equally:

.container {
  max-width: 960px;
  margin: 0 auto;  /* 0 top and bottom, auto left and right */
  padding: 0 16px; /* breathing room on phones */
}

Relative units help too. % is relative to the parent's width, rem to the root font size (usually 16px, so 2rem is 32px), and vw is 1% of the viewport width. Font sizes in rem stay consistent across the page and respect the reader's browser font settings.

Media queries

A media query applies CSS only when a condition is true, for example when the screen is narrow:

.layout {
  display: grid;
  grid-template-columns: 2fr 1fr;
}

@media (max-width: 600px) {
  .layout {
    grid-template-columns: 1fr; /* one column on phones */
  }
}
  • max-width: 600px matches screens 600px wide or narrower.
  • min-width: 768px matches screens 768px wide or wider.

The width at which the layout changes is called a breakpoint. Pick breakpoints where your design starts to look cramped, not for specific devices.

Mobile first

Many developers write the phone layout first, as the default, and add min-width queries for bigger screens:

.layout { display: grid; gap: 24px; } /* phones: one column */

@media (min-width: 768px) {
  .layout { grid-template-columns: 2fr 1fr; } /* tablets and up */
}

Both directions work. Mobile-first CSS often stays simpler: small screens get the simple layout, and big screens get the additions.

Common mistakes

  • Forgetting the viewport meta tag. The page looks right in a narrow desktop window but stays zoomed out on a real phone.
  • Putting the media query before the normal rule. A media query doesn't make a rule stronger. If the normal rule comes later in the file, it wins. Write media queries after the rules they change.
  • Using width instead of max-width. A fixed width can't shrink, so the page scrolls sideways on phones.

Assignment

Make the realm news page responsive:

  1. Add the viewport meta tag to <head>, with content="width=device-width, initial-scale=1".
  2. Give .container a max-width of 800px and centre it with margin: 0 auto.
  3. Make .layout a grid with two columns, 2fr 1fr, and a 24px gap, so the article and the sidebar sit side by side.
  4. Set the h1 font size to 2.5rem.
  5. Add a media query for screens 600px wide or narrower, @media (max-width: 600px), that switches .layout to one column (1fr) and sets the h1 to 1.75rem.

The tests check your page at several widths: 1024px, 700px, 600px and 500px.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 23 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.