Sub topic 1
What mobile-first means and why it works
Mobile-first CSS is a way of ordering your stylesheet. You write the base styles for the narrowest screen and add rules for wider screens with min-width media queries. This chapter explains the idea, compares it with desktop-first CSS, and shows why the approach holds up in practice. Later chapters cover the viewport tag, units, breakpoints and layout tools in detail.
Prerequisites
This chapter assumes you already know:
- Basic HTML structure: headings, paragraphs, lists, images and links.
- CSS selectors, the box model and the cascade, including how specificity and source order decide which rule wins.
- Basic Flexbox or Grid. You only need to recognize
display: gridandgap. - How to open your browser's developer tools and inspect an element.
The core idea
A mobile-first stylesheet has two parts:
- Base styles with no media query. They describe the page on a small screen and also apply everywhere else.
- Enhancements inside
@media (min-width: ...)blocks. Each one adds or changes rules once the viewport is at least that wide.
The word "first" describes the order in which you think and write. You solve the hardest layout problem first: little width, one column, touch input and possibly a slow network. You then use extra space when it exists.
The phrase does not mean "design only for phones". A mobile-first site works on desktop. The base styles are the starting point, and the wider layouts are layered on top.
A small example
This stylesheet gives a page a single column by default and a two-column layout on wider screens.
/* 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;
}
}Read it from top to bottom. A grid with one implicit column already stacks its children, so the base rule needs no column definition. At 48em (768px at the default font size) the query matches. It adds two columns and increases the padding. The comment "add, don't undo" is the rule of thumb for the whole approach. Wider-screen rules should add things. They should not cancel base rules.
The same page, desktop-first
Desktop-first CSS reverses the order. The base styles describe the wide layout, and max-width queries remove or simplify it for smaller screens.
/* Desktop-first version of the same layout */
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 1rem;
padding: 2rem;
}
@media (max-width: 47.99em) {
.layout {
grid-template-columns: none;
padding: 1rem;
}
}Both versions render the same result in a browser. The difference shows up in how the code behaves as the project grows. The table compares the two approaches.
| Aspect | Mobile-first (`min-width`) | Desktop-first (`max-width`) |
|---|---|---|
| Base styles | Simple, small-screen layout | Complex, wide-screen layout |
| Media queries | Add features | Undo features |
| Small devices | Parse only the rules they need, with no overrides to cancel | Must apply the wide layout, then cancel it |
| Query ranges | Open-ended upward: "this wide and up" | Open-ended downward: "this narrow and down" |
| Typical bug | A rule added at a breakpoint leaks upward, which is usually desired | Reset rules are forgotten, and small screens inherit wide-screen properties |
Why it works
Less overriding
In the desktop-first example, the query had to write grid-template-columns: none to cancel a value. Every property you set for the large layout needs a matching reset for small screens. Resets accumulate, and they are easy to forget. In the mobile-first version, the small layout needs almost no declarations because the browser's default behavior is already a stacked flow of blocks. Block elements fill the available width and wrap text on their own. Much of mobile-first CSS is simply not writing something.
Constraints first
A narrow screen forces decisions. You must pick what content matters, how long lines can be, and how big a tap target must be. Those decisions improve the wide layout too. A page built by shrinking a desktop design often hides content on small screens, and hiding content is usually a sign that the design was not prioritized.
Cascade-friendly ordering
CSS resolves conflicts by specificity first and source order second. With min-width queries, the rules that apply at larger sizes come later in the file than the rules for smaller sizes. When several queries match, the largest matching query appears last and wins when specificity is equal. This ordering is natural, and it removes a class of bugs where an earlier query accidentally beats a later one.
The diagram below shows how rules stack as the viewport grows.
At 400px wide, only the base styles apply. At 800px, the base and the first query apply. At 1200px, all three apply, and later rules override earlier ones only where they declare the same property.
Better default for slow devices
Browsers still download and parse the whole stylesheet in either approach, so the saving is not file size. The benefit is that a small device renders the base styles without running through override chains. The bigger performance gains come from loading appropriately sized images and avoiding hidden heavy content. Those topics appear in later chapters.
Progressive enhancement
Mobile-first is an application of progressive enhancement. A basic, working experience comes first, and better experiences are added for browsers and devices that can handle them. If a media query is unsupported or fails, the user still gets a usable page, which is the base layout. With desktop-first CSS, a failure leaves a small screen with a wide layout that does not fit.
How the browser decides what applies
A min-width query tests the viewport width. The query (min-width: 48em) matches when the viewport is 48em or wider. In media queries, em is relative to the browser's initial font size, usually 16px, not to the font size you set on body. This is useful. A user who raises their default font size reaches the wider layout at a larger pixel width, which keeps lines of text from becoming too short.
Because the browser evaluates every query against the live viewport, the page reacts when the window resizes or a phone rotates. Nothing in your CSS needs to track the device. You describe conditions, and the browser applies the matching rules.
Common errors
Writing a desktop-first base inside a mobile-first sheet
A frequent mistake is adding fixed widths, multi-column grids or large margins in the base rules and then trying to patch them with max-width queries. This mixes both approaches and makes the cascade hard to follow. Keep the base minimal, and keep the query direction consistent in one stylesheet.
Using `max-width` queries for convenience
A single max-width query for a one-off tweak is sometimes fine. A stylesheet with both min-width and max-width queries at the same breakpoint can produce overlapping ranges. For example, max-width: 48em and min-width: 48em both match at exactly 48em. If you must use both, offset one boundary or use range syntax, as in @media (width >= 48em) and @media (width < 48em). Range syntax is supported in current browsers.
Undoing instead of adding
If a query contains many declarations that reset base values (float: none, width: auto, display: block), the base styles probably contain too much. Move those declarations into the query that actually needs them.
Forgetting the viewport tag
Without the viewport meta tag, mobile browsers lay out the page on a wide virtual viewport, often around 980px, then scale it down. Your min-width queries then match against that wide value, and the small-screen layout never appears. The next chapter covers the fix.
Treating mobile-first as a device list
Do not equate "small" with "phone" or "large" with "desktop". Tablets, foldables, split windows and zoomed browsers all produce unexpected widths. Style for width, not for device names.
When desktop-first is still reasonable
Mobile-first is the default recommendation, not a law. Desktop-first can be the right call when:
- You are maintaining a large existing desktop-first stylesheet, and a rewrite costs more than it saves.
- The product is a desktop-only tool, such as an internal dashboard on fixed hardware.
- A component has a complex wide layout and only a minor reduction for narrow widths.
In these cases, pick one direction and apply it consistently.
A short checklist
- Write base styles without a media query.
- Use
min-widthqueries to add layout. - Avoid resets inside queries. If you need them, check whether the base is too heavy.
- Keep queries in ascending order of width.
- Test at several widths, not only at the breakpoints.
Exercise
A stylesheet sets .sidebar { float: right; width: 30%; } as a base rule, then adds @media (max-width: 40em) { .sidebar { float: none; width: auto; } }. Rewrite it in a mobile-first way and explain why the new version needs fewer overrides.
Show answer
Move the float and width into a min-width query and leave the base rule empty: @media (min-width: 40em) { .sidebar { float: right; width: 30%; } }. The sidebar is a block element, so on small screens it already stacks at full width with no declarations. The original needed two resets (float: none and width: auto) to cancel the wide layout. The rewrite needs none, because the wide layout is only added where it applies.
Exercise
A page uses @media (min-width: 48em) but looks like a desktop page on a phone, and the query seems to match even on a narrow screen. What is the most likely cause?
Show answer
The page is missing <meta name="viewport" content="width=device-width, initial-scale=1">. Without it, the mobile browser uses a wide virtual viewport (often about 980px), so the min-width: 48em query matches and then the browser scales the page down.