CSS・Arranging and positioning

Make it look good on phones too: media queries

Looks great on a computer, but cramped on a phone... Solve it with "media queries," which switch the appearance to match the screen width.

You open the page you built on a phone, and the cards you’d lined up side by side are crushed together—struggling with phone support is a road everyone walks in web-making. Screen widths differ wildly across computers, tablets, and phones. That’s why there’s a mechanism to switch your CSS to match the screen width. It’s the media query.

Let’s write one

@media (max-width: 600px) {
  h1 {
    font-size: 24px;
  }
}
PartMeaning
@mediaThe signal for “switch depending on the screen”
(max-width: 600px)Condition: when the screen width is 600px or less
Inside { }CSS that only works when the condition matches

Read out loud: “if the screen width is 600px or less, make the h1 text 24px.” On a wide screen the normal CSS applies, and the moment it drops to phone size, the rules inside here override.

The most common use: turning a row into a stack

The most common time to reach for a media query is switching between a row on wide screens and a stack on phones. You combine it with Flexbox.

.cards {
  display: flex;
  gap: 16px;
}

@media (max-width: 500px) {
  .cards {
    flex-direction: column;
  }
}

Normally display: flex lays them in a row. Once the width drops to 500px or less, flex-direction: column re-lays them vertically—a single line of override makes it readable on phones too.

Let’s see it in motion

The media query is alive inside this box. On a computer they sit in a row, on a phone they stack—at the width of the screen you’re reading on right now, the look should be changing.

The prerequisite for it to work: viewport

For a media query to work correctly, the HTML side needs an instruction saying “on a phone, display at the actual screen width.” That’s the viewport meta tag written in the head. Without it, the phone shrinks the whole page, and your hard-earned media query never fires.

HTMLGet the contents of head in orderThe lesson where we wrote the viewport meta tag. That “magic spell” turned out to be the switch for phone support

You can combine two boundaries into a range

Joining min-width and max-width with and lets you write a media query that only works within a range of “at least X and at most Y.”

@media (min-width: 501px) and (max-width: 900px) {
  .cards {
    flex-direction: column;
  }
}

This means “only when the width is between 501px and 900px,” used when you want separate CSS for phones, tablets, and computers.

Summary of this lesson

  1. With @media (max-width: 600px) { … }, you can write CSS that only works when the screen width matches the condition
  2. The most common use is switching a row (flex) into a stack (column) on phones
  3. As a prerequisite, the HTML head needs the viewport meta tag

Try it

The “media queries” you learned this time won’t be used on Shima’s page (it’s built mainly around a single column, so it doesn’t break easily on phones to begin with). It’s a tool for when you build wide-screen layouts, like lining up many cards in a row. Keep it in mind, and use it when you need it.

FAQ

What if my media query isn't working?
First, check that the viewport meta tag is in the HTML head. Without it, the phone shrinks the whole page to fit, and the media query switch won't work as you expect. Next, check for a missing closing bracket, or that it isn't written above an existing rule and being overridden.
Should I use max-width or min-width?
You can build it either way, but for beginners the clearest approach is "write the computer version normally, then add phone-specific overrides with max-width." Once you're used to it, try "mobile-first" too, where you start from the phone and add wider-screen versions with min-width.
I wrote several media queries, but they don't take effect in the order I expected
In CSS, when rules have equal strength, the one written later wins. Media queries are no exception, so if you write a narrow-range rule first and a wide-screen one later, it can get overridden. Don't break the basic order of writing wide screens first, then narrow screens.