Nishaglobal Education logo

Responsive design and media queries

Make pages that work on phones, tablets and laptops: the viewport meta tag, mobile-first CSS, media queries, flexible images and testing with your browser's device mode.

Lesson 12 of 1340 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Add the viewport meta tag and explain why it is needed
  • Write mobile-first CSS with min-width media queries
  • Make images flexible and test layouts at phone and desktop sizes
Outline
  1. What you'll learn and why it matters
  2. Step 1: the viewport meta tag
  3. Step 2: media queries
  4. Step 3: think mobile first
  5. Step 4: flexible images
  6. Step 5: test at different sizes
  7. Practice
  8. Common mistakes
  9. Recap
  10. Quiz
  11. Mark lesson complete

What you'll learn and why it matters

More than half of all web visits come from phones. A responsive page rearranges itself to fit any screen: one column on a phone, more columns on a laptop. Search engines also rank pages that work well on phones more highly. You already met some responsive tools (max-width, flex-wrap, auto-fit); this lesson adds the rest.

Step 1: the viewport meta tag

Phones pretend to be about 980px wide unless you tell them otherwise, and then shrink the whole page so the text is tiny. This line in the <head> tells them to use their real width:

Add to the head of every page
<meta name="viewport" content="width=device-width, initial-scale=1">

Step 2: media queries

A media query wraps CSS rules that apply only when a condition is true, usually a minimum screen width.

Phonenarrower than 700pxheadercard 1card 2card 3footerWide screen700px and widerheadercard 1card 2card 3footer@media (min-width: 700px) { … }Same HTML; the media query changes the layout.
The same HTML shown two ways. On a phone narrower than 700px, the header, three cards and footer stack in one column. At 700px and wider, a media query places the three cards side by side between the header and footer.
CSS
.cards {
  display: grid;
  gap: 1rem;
}

.card {
  padding: 1rem;
  background-color: #dbeafe;
  border: 2px solid #2563eb;
}

@media (min-width: 700px) {
  .cards {
    grid-template-columns: repeat(3, 1fr);
  }
}
Show the HTML this CSS styles
<div class="cards">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
</div>

Browser preview

Use the preview buttons. At Phone (360px) the cards stack. At Laptop (1024px) they sit in a row, because 1024px is wider than 700px and the media query switches on. Fit shows the preview at the width of this lesson's column (but never narrower than a 320px phone), so what you see there depends on your screen.

Step 3: think mobile first

Mobile-first means writing the simple, one-column phone layout as your normal CSS, then adding min-width media queries to enhance it for bigger screens. It keeps phone CSS simple and fast, and most layouts naturally start as one column.

CSS
.page {
  display: grid;
  gap: 8px;
}

.page > * {
  padding: 1rem;
  background-color: #f1f5f9;
  border: 1px solid #94a3b8;
}

@media (min-width: 640px) {
  .page {
    grid-template-columns: 1fr 3fr;
    grid-template-areas:
      "header header"
      "sidebar main"
      "footer footer";
  }

  .page header { grid-area: header; }
  .page aside { grid-area: sidebar; }
  .page main { grid-area: main; }
  .page footer { grid-area: footer; }
}
Show the HTML this CSS styles
<div class="page">
  <header>Green Leaf Café</header>
  <aside>Opening hours</aside>
  <main>Today's specials</main>
  <footer>12 Park Road, Pune</footer>
</div>

Browser preview

On a phone everything stacks in the HTML's order. From 640px, the layout from the Grid lesson takes over.

Common breakpointRoughly
min-width: 640pxLarge phones turned sideways, small tablets
min-width: 768pxTablets
min-width: 1024pxLaptops and desktops

Step 4: flexible images

A large image can be wider than a phone screen and cause sideways scrolling. This rule makes every image shrink to fit its container while keeping its shape:

Add near the top of your stylesheet
img {
  max-width: 100%;
  height: auto;
}
CSS
img {
  max-width: 100%;
  height: auto;
}

.narrow {
  max-width: 220px;
  border: 2px dashed #64748b;
}
Show the HTML this CSS styles
<div class="narrow">
  <img src="images/cafe.svg" alt="A cup of tea on a café table" width="600" height="400">
</div>

Browser preview

The image file is 600px wide, but it shrinks to the 220px box. Keeping the width and height attributes in the HTML lets the browser reserve the right space before the image loads.

Step 5: test at different sizes

  1. Open your page in Chrome, Edge or Firefox and press F12 (or right-click → Inspect) to open the developer tools.
  2. Press Ctrl+Shift+M (Cmd+Shift+M on a Mac) to switch on device mode, also called responsive design mode.
  3. Pick a phone from the list, or drag the edge of the page to change the width.
  4. Check: no sideways scrolling, text big enough to read, buttons big enough to tap, nothing overlapping.

Practice

Guided exercise

A responsive navigation bar

On phones, stack the site name above the links. From 600px, put the name on the left and the links on the right.

  1. Normal (phone) CSS: make the links a flex row with a gap, and remove list bullets.
  2. Add @media (min-width: 600px).
  3. Inside it, make .site-header a flex container with justify-content: space-between and align-items: center.
Show the solution
Solution
.site-header {
  padding: 1rem;
  background-color: #1e293b;
  color: white;
}

.site-header ul {
  display: flex;
  gap: 1rem;
  list-style: none;
  padding: 0;
}

.site-header a {
  color: white;
}

@media (min-width: 600px) {
  .site-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
Show the HTML this CSS styles
<header class="site-header">
  <strong>Asha Designs</strong>
  <nav>
    <ul>
      <li><a href="work.html">Work</a></li>
      <li><a href="about.html">About</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </nav>
</header>

Browser preview

How to get there: The phone layout is the default: block elements stack, so the name sits above the links. The media query only adds the side-by-side layout when there is room for it.

Your turn

Bigger headings on bigger screens

Make the h1 1.75rem on phones and 3rem from 900px wide. Also add the flexible image rule.

Show the solution
Solution
h1 {
  font-size: 1.75rem;
}

img {
  max-width: 100%;
  height: auto;
}

@media (min-width: 900px) {
  h1 {
    font-size: 3rem;
  }
}
Show the HTML this CSS styles
<h1>Welcome to my portfolio</h1>
<img src="images/project.svg" alt="A web page I built, shown on a laptop" width="600" height="400">

Browser preview

How to get there: Mobile first: the small size is the normal rule and the media query raises it on wide screens. The image rule stops the 600px image from overflowing a phone.

Common mistakes

Missing the viewport meta tag

You see: The page looks tiny on a phone and media queries seem to be ignored

Fix: Add <meta name="viewport" content="width=device-width, initial-scale=1"> to the head.

Forgetting the brackets or the closing } of a media query

You see: The rules inside never apply, or every rule after it breaks

Fix: Write @media (min-width: 700px) { … } and check the braces match.

Desktop first with lots of max-width overrides

You see: Long, confusing CSS that is hard to fix on phones

Fix: Write phone styles first, then add min-width queries.

Fixed widths in px on big boxes

You see: Sideways scrolling on small screens

Fix: Use max-width, %, fr and flexible images.

Testing only on your own laptop

You see: Problems on phones go unnoticed

Fix: Use device mode (Ctrl+Shift+M) and a real phone.

Recap

  • Add the viewport meta tag to every page.
  • Media queries apply rules only when a condition is true, such as (min-width: 700px).
  • Mobile first: phone styles by default, min-width queries for larger screens.
  • img { max-width: 100%; height: auto; } keeps images inside their container; test with device mode.

Key terms

Responsive design
Designing pages that adapt their layout to any screen size.
Media query
A CSS block that applies only when a condition, such as a minimum width, is true.
Breakpoint
A width at which a media query changes the layout.
Mobile first
Writing phone styles as the default and adding styles for larger screens.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. What does the viewport meta tag do?

  2. 2. When do the rules inside `@media (min-width: 768px) { … }` apply?

  3. 3. In mobile-first CSS, where does the one-column phone layout go?

  4. 4. Which rule stops large images causing sideways scrolling?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.