Sub topic 1
What mobile-first means and why it works
Mobile-first CSS means you write the base styles for the narrowest screen, then add rules for wider screens with min-width media queries. This chapter explains the idea, compares it with desktop-first, and shows a first working example.
Prerequisites
This chapter assumes you already know:
- Basic HTML structure.
- CSS selectors, the box model and the cascade.
- How to open your browser's developer tools.
You do not need to know Flexbox or Grid in depth yet. Later chapters cover layout in detail.
The core idea
A mobile-first stylesheet has two parts:
- Base styles with no media query. They apply to every screen and are written for a small one.
- Enhancements inside
@media (min-width: ...)blocks. They apply only once the viewport is at least that wide.
The base styles describe the simplest layout: one column, full-width blocks, content stacked in source order. Wider screens then add to that layout. They do not have to undo it.
Desktop-first for comparison
A desktop-first stylesheet starts with the multi-column layout. It then uses max-width queries to take features away as the screen gets narrower.
| Approach | Base styles target | Queries used | What a query does |
|---|---|---|---|
| Mobile-first | Small screens | `min-width` | Adds layout for wider screens |
| Desktop-first | Large screens | `max-width` | Removes or overrides layout for narrower screens |
Both approaches can produce the same result on a given screen. They differ in what happens when something goes wrong, and in how much code you write.
Why mobile-first works
- Less overriding. A single column needs very little CSS, because block elements already stack. Desktop-first rules must be reset on small screens, and resets are easy to get wrong.
- A safe default. If a media query fails to match, or a browser does not support a newer feature, the user still gets a usable single-column page.
- Less work for phones. A phone parses only the base rules plus any queries that match. It does not need to apply and then cancel desktop rules.
- Content comes first. Starting small forces you to decide what matters most, because there is little room for anything else.
A first example
This stylesheet gives a single column by default and adds a second column only when there is room.
/* Base: small screens */
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Wider screens: add, don't undo */
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}Below 48em (768px at the default font size), the grid has one implicit column, so children stack. At 48em and above, the second rule applies. It sets two columns and increases the padding. Only the differences appear in the query.
The cascade does the work here. Both rules have the same specificity, so the later one wins when its query matches. Keep your min-width blocks after the base rules they extend.
The order of the queries
When you have several breakpoints, list them from smallest to largest. Each query then builds on the one before it.
Common mistakes
- Using
max-widthfor the main layout. This quietly turns the stylesheet into desktop-first. Reservemax-widthfor rare cases where a rule must apply only to small screens. - Putting media queries before the base rules. Equal-specificity base rules that come later will override the query.
- Repeating base declarations in every query. If a value does not change, leave it out. A query should contain only what changes.
- Forgetting the viewport meta tag. Without it, phones lay the page out at a wide virtual width and your small-screen styles never apply. The next chapter covers this.
Exercise
A desktop-first stylesheet sets .sidebar { width: 30%; float: left; } and then uses @media (max-width: 40em) { .sidebar { width: 100%; float: none; } }. How would you write the same behavior mobile-first?
Show answer
Put the small-screen layout in the base rule and add the wide layout in a min-width query: .sidebar { width: 100%; } and @media (min-width: 40em) { .sidebar { width: 30%; float: left; } }. The base rule needs no float: none because floating is never set on small screens, so there is nothing to undo.
Exercise
Why does a mobile-first stylesheet still give a usable page if a media query does not match?
Show answer
The base styles already describe a complete single-column layout. The queries only add enhancements, so if none match, the page keeps the simple layout.