Sub topic 2
Thinking mobile-first
This chapter assumes you know basic HTML and CSS, including flexbox and grid, and what a media query is. You should also recognize utility classes such as flex, p-4 and text-lg.
What mobile-first means in Tailwind
Tailwind is mobile-first. An unprefixed utility such as grid-cols-1 applies at every screen width, starting at the smallest. A prefixed utility such as md:grid-cols-2 applies at that breakpoint and everything wider.
Under the hood, the md: prefix wraps the rule in a min-width media query. Roughly:
.grid-cols-1 {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
@media (min-width: 48rem) {
.md\:grid-cols-2 {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}Because the query is min-width, the prefixed rule never switches off when the screen gets larger. It stays in effect until a larger breakpoint overrides it.
The workflow
Style the smallest layout first, using no prefixes. Then widen the screen and add prefixed utilities only where the design needs to change.
- Write the base design with unprefixed classes. This is your phone layout.
- Resize until the layout looks awkward.
- Add one breakpoint prefix to fix it.
- Repeat at the next awkward width.
Here is a navigation bar built this way:
<nav class="flex flex-col gap-2 md:flex-row md:items-center md:gap-6">
<a href="/">Home</a>
<a href="/docs">Docs</a>
<a href="/pricing">Pricing</a>
</nav>Below md, the links stack in a column with a small gap. From md upward, md:flex-row replaces flex-col, and md:gap-6 replaces gap-2. md:items-center is new, because the stacked layout did not need it.
Unprefixed means "all sizes"
A common misreading is that unprefixed utilities are "for mobile only". They are not. They are the default for every width, and prefixed utilities override them upward. The class text-center centers text on a phone and on a 4K monitor, unless a prefixed class says otherwise.
This also explains why you rarely need to undo a style at small sizes. Add the style where it should begin.
<!-- Correct: centered on small screens, left-aligned from sm up -->
<p class="text-center sm:text-left">Welcome back</p><!-- Wrong if you want centered text only on phones:
sm:text-center also centers on every wider screen -->
<p class="sm:text-center">Welcome back</p>The second example centers text only at 640px and wider. On a phone it is left-aligned, the opposite of what the author likely intended.
Why this approach works
- Smaller base styles. Phone layouts are usually simpler, often a single column. Starting there keeps the base class list short.
- Overrides move in one direction. Each breakpoint builds on the one before it, so you can predict which rule wins at any width.
- Progressive enhancement. A browser that fails to match any media query still shows the base layout, which is the most usable one.
For this to work on real phones, the page needs the viewport meta tag from the previous chapter. Without it, mobile browsers report a desktop-sized width and the base styles never get used the way you expect.
<meta name="viewport" content="width=device-width, initial-scale=1">A worked example
Consider a heading that should grow with the screen:
<h1 class="text-2xl font-bold md:text-4xl lg:text-5xl">Responsive heading</h1>The widths resolve like this:
| Screen width | Active text size classes | Result |
|---|---|---|
| Under 768px | `text-2xl` | 2xl size |
| 768px to under 1024px | `text-2xl`, `md:text-4xl` | 4xl size, because `md:` overrides the base |
| 1024px and wider | `text-2xl`, `md:text-4xl`, `lg:text-5xl` | 5xl size, because `lg:` overrides `md:` |
font-bold has no prefix, so the heading is bold at every width.
Exercise
A card should have p-2 padding on phones and p-8 padding on screens at the lg breakpoint and wider. Someone wrote class="lg:p-2 p-8". What does this do, and how do you fix it?
Show answer
It does the opposite. The unprefixed p-8 applies at every width, and lg:p-2 shrinks the padding at lg and wider, so phones get large padding and desktops get small padding. The fix is class="p-2 lg:p-8": the small value is the base, and the large value is added at lg.