Sub topic 1
Why design for small screens first
Mobile-first design means you build the smallest-screen version of a page first, then add layout for larger screens. This chapter covers the working model, how it differs from desktop-first, and the errors that come up most often.
Prerequisites
This chapter assumes you can:
- Write a basic HTML page using semantic elements such as
header,mainandnav. - Write CSS with selectors, understand the box model, and use
px,remand%units. - Open your browser's developer tools and inspect an element.
The working model
Mobile-first is a way of ordering your decisions and your CSS. It is not a separate technology. You follow three steps:
- Decide what a visitor on a small screen needs, and in what order.
- Write base CSS that makes that version work with no media queries.
- Add media queries that apply only when more space is available.
A small screen has little room, so you cannot show everything at once. That limit makes you choose. On a wide desktop layout, it is easy to keep every link, banner and sidebar because there is space for them. The small-screen version exposes which of those items nobody needs.
Mobile-first versus desktop-first
In a desktop-first stylesheet, the base rules describe the wide layout. You then use max-width queries to undo or shrink things for small screens. In a mobile-first stylesheet, the base rules describe the narrow layout. You then use min-width queries to add features.
The two approaches can produce the same result on a laptop. They differ in what the browser does on a phone, and in how much code you maintain.
| Aspect | Desktop-first | Mobile-first |
|---|---|---|
| Base CSS | Wide, multi-column layout | Single column |
| Media queries | `max-width`, which override the base | `min-width`, which add to the base |
| Phone behaviour | Downloads and parses rules it then overrides | Applies only the rules it needs |
| Typical risk | Overflowing content and hidden desktop leftovers | A sparse-looking desktop layout |
Here is the same two-column layout written both ways.
/* Desktop-first: the base is wide, and small screens undo it */
.page {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 2rem;
}
@media (max-width: 40em) {
.page {
grid-template-columns: 1fr;
gap: 1rem;
}
}
/* Mobile-first: the base is narrow, and large screens add columns */
.page {
display: grid;
gap: 1rem;
}
@media (min-width: 40em) {
.page {
grid-template-columns: 2fr 1fr;
gap: 2rem;
}
}The mobile-first version has no rule that exists only to cancel another rule. This is the main maintenance benefit.
Why it pays off
- Focus. A narrow screen forces you to rank content before you decorate it. The next chapter covers how to do that ranking.
- Simpler CSS. A single column is the browser's default flow. You add only what the layout needs, so you write less overriding code.
- Better defaults for constrained devices. Phones often have slower connections and less processing power. A base design that needs little works well there.
- Graceful growth. A page that already works at 320 CSS pixels wide rarely breaks when it gets more room. It may only look sparse until you add columns.
Common errors
- Treating mobile-first as "shrink the desktop design." If you design the wide page first and then remove things, you are still working desktop-first. Start from the content list, not from a desktop mockup.
- Using
max-widthqueries in a mobile-first stylesheet. This brings back the overrides you were trying to avoid. Usemin-widthfor the layout and keepmax-widthfor rare exceptions. - Hiding content on small screens with
display: none. The user still downloads it, and may still need it. Prefer reordering or collapsing it. A later chapter covers progressive disclosure. - Assuming a small screen means a slow connection or one hand. These are tendencies, not facts. Design so the page works under those conditions, but do not block features for users who have more.
What comes next
The following chapters build on this principle. You will rank your content, set the viewport so the browser uses the device width, and then build the single-column base.
Exercise
A stylesheet sets a three-column grid in the base rules and uses @media (max-width: 40em) to switch to one column. Rewrite the approach in a mobile-first way, and name one benefit.
Show answer
Make the base rule a single column, for example display: grid; gap: 1rem;. Then add @media (min-width: 40em) { .layout { grid-template-columns: repeat(3, 1fr); } }. One benefit is that no rule exists only to cancel another, so the CSS is shorter and easier to maintain. Small screens also apply only the rules they need.
Exercise
Why is removing sections from a finished desktop design not the same as designing mobile-first?
Show answer
Mobile-first starts from a ranked list of content and the needs of a small-screen user. Removing sections from a desktop design keeps the desktop priorities and layout assumptions, so the small version is a leftover rather than a deliberate design.