Write Mobile-First Media Queries

In the last lesson, you opened garden-practice/ in device mode at 320 pixels and listed what was built for a wide screen. On the home page, the welcome and the hero photo are squeezed into a sliver beside Tomás’s note, because the .home grid gives the note 250 pixels at every width. Rosa’s visitors read that page on their phones.

What the page needs is a layout that changes with the width of the screen. In this lesson, you’ll write a media query, rebuild the home page’s grid with a mobile-first layout, and predict which rules apply at a given width. Start with the tool itself, a rule that applies only when the screen is wide enough.

Add a Condition to Your CSS

One stylesheet serves every screen, from a 320-pixel phone to a 1280-pixel laptop. To give each one a different layout, you need rules that apply only at some widths. A media query is that condition: the @media at-rule, a condition in parentheses, and a block of rules that apply only while the condition is true.

As a programmer, you can read it as an if around some rules. Here’s one that gives the home page its sidebar only when the screen is at least 45rem wide:

Language: CSS
@media (width >= 45rem) {
  .home {
    grid-template-columns: 1fr 250px;
  }
}

The if comparison holds as far as the condition goes, but it doesn’t account for when the check happens. CSS doesn’t run once, top to bottom. The browser keeps checking the condition, so when you resize the window or zoom in, the block’s rules switch on and off on the spot. There’s also no else. When the condition is false, the rules inside don’t take part.

In the condition, width is the width of the viewport in CSS pixels. On a phone, that width depends on the viewport meta tag you checked in the last lesson, <meta name="viewport" content="width=device-width, initial-scale=1">. Without it, the phone assumes a desktop page. It lays the page out on a layout viewport about 980 CSS pixels wide, then shrinks the result to fit the screen. A query would then see a width of 980, even on a 390-pixel phone.

With the tag, the layout viewport starts at the screen’s width, so width is the phone’s real width in CSS pixels, which is what your queries need to test.

The >= form is the range syntax, which reads like a comparison in code. In older stylesheets, you’ll see (min-width: 45rem), which means the same. The range syntax is Baseline Widely available, so every current browser supports it. MDN’s guide to media queries lists the other conditions you can test.

Why 45rem and not a number of pixels? Inside a media query, 1rem is the browser’s default text size, 16 pixels unless the reader changed it in their settings. It ignores any font-size your stylesheet sets on <html>. So 45rem is 720 pixels for most people. A reader who chose larger text gets a higher breakpoint, the width where the layout changes, so their bigger text doesn’t run out of room.

Start From the Small Screen

Your .home rule assumes a wide screen, and a media query can fix that in two ways. You could keep the two columns as the default and add a query that undoes them below some width. That’s desktop-first, and the small screen’s layout is then built from overrides, each one undoing a wide-screen rule you have to keep track of.

Mobile-first turns this around. The base rules, outside any query, describe the small screen. Each query then adds what extra space allows. The small screen’s layout is usually the plainer one. Here it’s a single column, so the queries only add things and never undo them.

Here’s where the home page starts, at 320 pixels in device mode, as you saw it in the last lesson. The shot shows the whole page, including the part off the screen to the right, so you can see the note’s column beside the welcome:

The whole home page at 320 pixels, about 600 pixels wide, with Tomás's note in a 250-pixel column beside the welcome and the hero photo.
The Home Page Before the Mobile-First Grid

In garden-practice/style.css, find the .home rule in the layout layer. Change its columns to one 1fr column, and add the query right after the rule, still inside @layer layout:

Language: CSS
  .home {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-l);
  }

  @media (width >= 45rem) {
    .home {
      grid-template-columns: 1fr 250px;
    }
  }

The base rule now stacks the welcome and Tomás’s note in one column, with the same gap between them. The query brings the 250-pixel sidebar back from 720 pixels up. That’s roughly where the welcome still keeps about 400 pixels beside the note, enough for its text and the hero photo. For now, use 45rem as the breakpoint. Finding the right width by watching the content is the next lesson’s job.

Save and check the home page in device mode at 320 pixels. Tomás’s note now appears below the photo instead of beside the welcome. The page still scrolls sideways, though, and the welcome now runs off the right edge with the menu.

The header row still makes the page about 600 pixels wide, and the single column stretches across all of it. Once you fix the header in the next lesson, the column will fit the screen. Here’s the whole page again, about 600 pixels wide. The 320-pixel screen shows only its left half, up to about the word Volunteers in the menu, and the rest is what you scroll sideways to reach:

The whole home page at 320 pixels, still about 600 pixels wide because of the header row, with the welcome, photo and work day in one column and Tomás's note below them.
The Home Page Stacked in One Column at 320 Pixels

Type 719 as the width, and the page stays stacked. Type 720, and the sidebar appears. In a window 1024 pixels wide, the home page looks as it did before you started, with Tomás’s note in its sidebar:

The home page 1024 pixels wide, with the header on one row and Tomás's note in a sidebar beside the welcome and the hero photo.
The Sidebar Back Beside the Welcome on a Wide Screen

Now try the other check from the last lesson. Close device mode, make the window about 1280 pixels wide, and zoom to 200%. Before you look, decide whether the sidebar stays.

The note drops below the welcome, as on a phone. Zooming to 200% makes each CSS pixel twice as large, so the 1280-pixel window is now only about 640 CSS pixels wide. That’s under the query’s 720 pixels, so the condition turns false and the base rule’s single column takes over.

A reader who zooms in to 200% has text twice as big, so the layout for a narrower screen is the one that suits them.

Before you go on, reset the zoom to 100% with Ctrl+0, or Cmd+0 on macOS, so the widths you see match the ones in the rest of the course.

Keep the Base Rule First

Does it matter where the query goes? A programmer’s instinct says no. The rule inside the query is the more targeted one, so surely it wins when its condition is true.

However, a media query adds no specificity. The .home inside it scores (0, 1, 0), exactly like the .home outside it. When both match, they’re in the same layer with the same specificity, so the cascade’s tiebreaker decides, and the later rule wins.

Your two .home rules in layout show how that plays out. At 320 pixels, the query’s condition is false, so only the base rule applies, and its 1fr gives one column. At 1280 pixels, both rules match and both set grid-template-columns. They tie on layer and specificity, and the query’s rule comes after the base rule, so its 1fr 250px wins.

If you put the query above the base rule, the base rule’s 1fr will come later and win at every width, so the sidebar will never appear. Mobile-first order prevents that problem because you write the base rules first and the queries after them, from the narrowest width up.

That tiebreaker only works inside one layer. A rule in a later layer, such as components, or an unlayered rule, still beats the query’s rule regardless of source order. That’s why the page-level queries go in layout, beside the layout they change.

If a query on your own pages doesn’t switch on when you expect, the mentor can help you work out which width the condition sees:

Before moving on, check that you can read the home page’s rules at a given width:

  1. Mei opens the finished home page on her phone, which is 375 pixels wide. Which grid-template-columns value does .home get?
  2. Priya copies the home page into a page of her own but leaves out the viewport meta tag. On her phone, does she see the stacked layout or the sidebar, and why?
  1. It gets 1fr, so the page stays stacked. At 375 pixels, the viewport is under 45rem wide, so the query’s condition is false and only the base rule applies.
  2. She sees the sidebar, shrunk to fit. Without the tag, the phone lays the page out about 980 CSS pixels wide, so width >= 45rem is true and the desktop layout applies, with tiny text.

The home page now starts from the small screen and gains its sidebar from 45rem. But where exactly should a breakpoint go? In the next video, Find Breakpoints Where Content Breaks, you’ll make the header’s row mobile-first and find its breakpoint by watching the content as the window narrows.

Become a Member to join the conversation.