Mobile-first design and media queries

Build pages that work beautifully on a small Android phone and a wide laptop screen.

Module 4: Responsive design · Lesson 12 of 25 · 16 min read · Free

In Nigeria, most people browse on phones. A site that only works on a laptop loses most of its visitors. Responsive design means one page that adapts to every screen:

One page, three screens

Three foundations

  1. The viewport meta tag in every page (lesson 1.3) — otherwise phones pretend to be a desktop and zoom out.
  2. Flexible layouts — %, fr, max-width, flexbox and grid instead of fixed pixel widths.
  3. Media queries — CSS that applies only when the screen matches a condition.

Media queries

/* base styles: phones */
.products { display: grid; grid-template-columns: 1fr; gap: 12px; }

/* from 600px wide: tablets */
@media (min-width: 600px) {
  .products { grid-template-columns: repeat(2, 1fr); }
}

/* from 1000px wide: laptops */
@media (min-width: 1000px) {
  .products { grid-template-columns: repeat(4, 1fr); }
}

The rules inside @media (min-width: 600px) { … } apply only when the window is at least 600px wide.

Mobile first

Write the phone styles first (no media query), then add min-width media queries for bigger screens. Why?

  • Phone layouts are simpler (usually one column), so the base CSS stays short.
  • You design for most of your users first.
  • Small screens force you to decide what really matters.
<style>
  body { font-family: system-ui, Arial, sans-serif; margin: 0; }
  header { background: #08783f; color: #fff; padding: 12px 16px; display: flex; flex-direction: column; gap: 6px; }
  header nav a { color: #fff; margin-right: 12px; }
  .features { display: grid; gap: 12px; padding: 16px; }
  .features div { background: #ecfdf5; border-radius: 10px; padding: 14px; }
  @media (min-width: 500px) {
    header { flex-direction: row; justify-content: space-between; align-items: center; }
    .features { grid-template-columns: repeat(3, 1fr); }
  }
</style>
<header><strong>GreenRide</strong><nav><a href="#">Ride</a><a href="#">Drive</a><a href="#">Business</a></nav></header>
<section class="features">
  <div><b>Fair prices</b><p>See the price first.</p></div>
  <div><b>Safe drivers</b><p>Verified and rated.</p></div>
  <div><b>Always on</b><p>24/7, rain or shine.</p></div>
</section>

The preview box is narrow, so you see the phone layout. Change 500px to 300px in the media query and watch it switch to the wide layout.

Choosing breakpoints

Don't design for specific phone models. Start narrow, widen the window, and add a breakpoint where the design starts to look wrong — lines too long, cards too stretched. Common starting points: 600px, 900px, 1200px.

Check yourself

Answer in your head first, then open each question to see the answer and why.

  1. In mobile-first CSS, which media queries do you mostly use?

    • A min-width, adding styles for bigger screens
    • B max-width only
    • C print
    • D None
    Show the answer

    A) min-width, adding styles for bigger screens — Base styles are for phones; min-width queries add wider layouts.

  2. When do the rules inside @media (min-width: 600px) apply?

    • A When the viewport is at least 600px wide
    • B When it's narrower than 600px
    • C Only on phones
    • D Always
    Show the answer

    A) When the viewport is at least 600px wide — min-width means "this wide or wider".

  3. How should you choose breakpoints?

    • A Where your design starts to look wrong as the screen widens
    • B One for every phone model
    • C Always exactly 320px and 1920px
    • D At random
    Show the answer

    A) Where your design starts to look wrong as the screen widens — Let the content decide.

More free lessons: What front-end developers do · Set up VS Code, Chrome and Live Server · Text, links and images · Selectors, colours and fonts · Variables, data types and operators · The DOM: find and change page elements · Git and GitHub