Skip to content
Tutorials Center
My learning

Topics

Lesson 83 of 139 ยท Responsive Design

Mobile-First CSS

Write responsive CSS mobile-first. Start with small-screen styles, add min-width breakpoints, choose breakpoints from your content, and build a complete page.

There are two ways to write responsive CSS. You can design the big desktop layout first and then write media queries that squeeze it down for smaller screens. Or you can write the small-screen version first and then add media queries that expand it for bigger ones. The second approach is called mobile-first, and it is the standard way to write responsive CSS today.

This lesson explains why mobile-first works better, how to pick breakpoints that suit your content instead of particular devices, and then walks through building a small page from one column to three.

Desktop-first versus mobile-first

Here is the same two-column layout written both ways. Desktop-first starts with columns and then undoes them:

CSS
/* Desktop-first */
.layout {
  display: grid;
  grid-template-columns: 1fr 3fr;
  gap: 2rem;
}
@media (width < 700px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
}

Mobile-first starts with the simple version and adds columns when there is room:

CSS
/* Mobile-first */
.layout {
  display: grid;
  gap: 1rem;
}
@media (width >= 700px) {
  .layout {
    grid-template-columns: 1fr 3fr;
    gap: 2rem;
  }
}

They look similar at this size, but the difference grows with every component. Mobile-first has real advantages:

  • Small screens are the default. Normal block flow is already a single column. A lot of your mobile CSS is simply the browser's defaults, so there is less to write.
  • You add, you don't undo. Desktop-first queries are full of resets like float: none, width: auto and display: block. Mobile-first queries only add things, which is shorter and easier to read.
  • The simplest layout is the fallback. If a media query fails or isn't supported, the user gets a working single-column page, not a broken multi-column one.
  • It forces priorities. A narrow screen makes you decide what really matters. Those decisions improve the wide layout too.

In practice, mobile-first means your media queries mostly use width >= (or min-width), and they get larger as you go down the stylesheet.

Your first mobile-first component

Let's start small. A product card stacks its image and text on narrow screens, then puts them side by side once there is room.

A mobile-first card

The breakpoint is small because the preview pane is small. On a real page with this card in a wide column, you might use a larger value. The query responds to the preview's width, so drag the divider to see it switch.

Choosing breakpoints by content

A common mistake is to pick breakpoints from a list of devices: "320 for phones, 768 for tablets, 1024 for laptops". Devices change every year, and there are thousands of sizes in between. Your content doesn't care which device it's on. It only cares how much room it has.

A better method is to let the content tell you:

  1. Build the narrow layout and open it in a browser.
  2. Slowly widen the window.
  3. Watch for the moment the design starts to look wrong: lines of text become too long to read comfortably, a card becomes awkwardly wide, there is lots of empty space beside a single column.
  4. Add a breakpoint right there, and adjust the layout.
  5. Keep widening and repeat.

Most pages end up with only two or three breakpoints, and many components need none at all thanks to intrinsic tools like flex-wrap and auto-fit grids from Responsive Design Principles.

Line length is the most reliable signal. Body text is comfortable at roughly 45 to 75 characters per line. When a single column gets wider than that, it's time to cap its width or add a second column. The Fluid Typography lesson returns to this idea.

Find the breakpoint yourself

Add a fifth link and the right breakpoint changes. That's the point: breakpoints belong to content, so they move when content moves. This is also why many teams give each component its own breakpoints instead of using one global set. Container queries, in the next lesson, take that idea even further.

Naming and organizing breakpoints

If you do want a few shared breakpoints, name them by size, not by device: "small", "medium", "wide" rather than "phone" and "tablet". CSS custom properties can't be used inside media query conditions, so teams usually keep a short list in a comment or in their build tooling.

Keep each media query next to the component it changes, rather than collecting every query in one block at the bottom of the file. With CSS Nesting you can even write the query inside the rule:

CSS
.product {
  padding: 12px;

  @media (width >= 440px) {
    display: grid;
    grid-template-columns: 160px 1fr;
  }
}

Building a mobile-first page

Now a complete page. It has a header with navigation, a short intro, a set of feature cards and a footer. We will build it in three stages, each one layering on top of the last.

Stage 1: the base styles. Everything stacks. We set colors, spacing and typography, and the cards are just blocks. This is a perfectly good phone layout already.

Stage 2: a medium breakpoint. When there is room, the header puts the logo and the navigation on one line, and the cards form two columns.

Stage 3: a wide breakpoint. Three card columns, and the content is capped at a comfortable width and centered.

A page built mobile-first

Drag the preview from very narrow to as wide as it goes, and watch it pass through the three stages. Look at how short the two media queries are: they only describe what changes. Everything else is inherited from the base styles.

Try an experiment: in Stage 3, delete grid-template-columns: repeat(3, 1fr); and instead change the base .features rule to grid-template-columns: repeat(auto-fit, minmax(min(140px, 100%), 1fr));. You can then remove the card columns from Stage 2 as well. That is the intrinsic approach replacing two breakpoints with one rule. Mobile-first and intrinsic design work best together.

When max-width queries still make sense

Mobile-first doesn't ban width < queries. They're useful for styles that should apply only to small screens and would otherwise need undoing:

CSS
/* Only narrow screens get a full-width button. */
@media (width < 440px) {
  .checkout-button { width: 100%; }
}

Writing it this way avoids setting width: 100% in the base and resetting it to auto later. The rule of thumb: write the narrow layout as your base, use >= queries to enhance it, and use < or range queries for narrow-only tweaks.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In mobile-first CSS, what do the base styles (outside any media query) describe?

    Mobile-first starts with the small-screen layout as the default, then uses media queries to add complexity as space grows.
  2. 2. Which kind of media query do you mostly use when writing mobile-first?

    Mobile-first enhances the base styles as the viewport grows, so most queries say 'at least this wide'.
  3. 3. What is the best way to choose a breakpoint?

    Content-based breakpoints stay correct no matter which devices exist. Device widths change constantly and leave odd sizes in between.
  4. 4. Why do mobile-first stylesheets tend to be shorter?

    Normal flow is already a single column, and enhancement queries only describe what changes, so there are no resets like float none or width auto.

Summary

  • Mobile-first means writing the small-screen layout as the base and adding width >= media queries to enhance it.
  • It produces shorter, clearer CSS with a working fallback, because queries add instead of undo.
  • Choose breakpoints where your content starts to look wrong, not from a list of devices; most pages need only two or three.
  • Keep media queries next to the components they change, nesting them if you like.
  • Combine mobile-first with intrinsic layouts, and use width < queries for narrow-only tweaks.

Next: Container Queries, where components respond to the size of their container instead of the whole screen.