Sub topic 2
Thinking mobile-first
This chapter assumes you know basic HTML and CSS, including flexbox and grid. It also assumes you know common Tailwind utilities such as flex, p-4 and text-lg, and what a CSS media query is. You will learn how Tailwind decides which styles apply at which width, and how to write markup that follows that model instead of fighting it.
The core rule
Tailwind's responsive system rests on one rule: an unprefixed utility applies at every width, and a prefixed utility applies at that width and wider.
Take this class list:
<p class="text-sm md:text-lg">Hello</p>text-smhas no prefix, so it applies on every screen.md:text-lgapplies only when the viewport is at least 768px wide. At that point it overridestext-sm.
The second class compiles to a min-width media query. The generated CSS looks like this:
.text-sm {
font-size: 0.875rem;
line-height: 1.25rem;
}
@media (min-width: 768px) {
.md\:text-lg {
font-size: 1.125rem;
line-height: 1.75rem;
}
}(Tailwind v4 writes the breakpoint as 48rem, which is the same width at the default font size.)
"Mobile-first" therefore does not mean "mobile only". It describes the order in which you write styles. You first write the styles for the narrowest screen, with no prefix. Then you add prefixed utilities that change things as the screen grows.
Why min-width and not max-width
Tailwind could have generated max-width queries and called the unprefixed styles "desktop". It uses min-width for three reasons.
- The cascade stays simple. Every prefixed rule adds to or overrides the rules below it. With
min-width, a wider screen matches every smaller breakpoint too, so styles build up in layers. - Small screens load less CSS logic. The base styles are usually the simplest layout, such as a single column. A phone does not have to evaluate overrides meant for a wide layout.
- It matches how layouts grow. A single column is the natural fallback. Adding columns as space appears is easier than removing them as space disappears.
The next chapter lists all the default breakpoints. For now, you only need the first few widths to follow the examples.
| Prefix | Minimum width | Generated query |
|---|---|---|
| (none) | 0 | No media query |
| `sm:` | 640px | `@media (min-width: 640px)` |
| `md:` | 768px | `@media (min-width: 768px)` |
| `lg:` | 1024px | `@media (min-width: 1024px)` |
Layers, not switches
A useful mental picture is a stack of layers. The base layer is always present. Each breakpoint adds a layer on top, and a layer applies only when the viewport is wide enough.
At a viewport of 800px, the base, sm: and md: layers are all active. The lg: layer is not. If two active layers set the same property, the wider breakpoint wins. Tailwind sorts its generated media queries by width, so this holds no matter what order you write the classes in.
Look at this heading:
<h1 class="text-2xl font-bold md:text-4xl">Welcome</h1>Here is what each width gets.
- At 400px, only
text-2xlandfont-boldmatch. - At 900px,
text-2xlstill matches, butmd:text-4xloverrides it.font-boldstays.
You never repeat font-bold at the larger size. Anything you do not override carries upward.
A worked example
This is a complete page you can save as index.html and open in a browser. It uses the Tailwind v4 browser build from a CDN, which is fine for experiments but not for production.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mobile-first card</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="bg-gray-50 p-4 md:p-8">
<article class="flex flex-col gap-4 rounded-lg border bg-white p-4 md:flex-row md:items-center md:gap-8 md:p-6">
<div class="h-40 w-full rounded bg-blue-200 md:h-32 md:w-48 md:shrink-0"></div>
<div>
<h2 class="text-xl font-bold md:text-2xl">Mobile-first card</h2>
<p class="mt-2 text-gray-600">
On a phone the image sits above the text. From 768px up, it moves to the left.
</p>
</div>
</article>
</body>
</html>Follow the card through two widths.
Below 768px, only the unprefixed classes apply. The article is a flex container with flex-col, so the blue box stacks above the text. The box is h-40 w-full, so it spans the card. This is a complete, usable layout, and it needs no breakpoint at all.
At 768px and above, the md: classes join in. md:flex-row replaces flex-col, so the items sit side by side. md:items-center aligns them vertically. The box changes to md:h-32 md:w-48, and md:shrink-0 stops it from being squeezed by the text. Padding and gap grow too.
Notice what each md: utility does. Each one either overrides a base value (flex-row over flex-col, md:w-48 over w-full) or adds a property that only makes sense in the wider layout (md:items-center, md:shrink-0). Neither kind needs the base layer to be undone by hand.
Undoing a style at a larger width
Because layers only add, you cannot "remove" a base utility with a prefix. You have to set a replacement value. The usual pairs are:
| Base (small screens) | Larger screens | Effect |
|---|---|---|
| `flex-col` | `md:flex-row` | Stack, then row |
| `hidden` | `md:block` | Hidden, then shown |
| `block` | `md:hidden` | Shown, then hidden |
| `grid-cols-1` | `lg:grid-cols-3` | One column, then three |
| `w-full` | `md:w-1/2` | Full width, then half |
To hide something on small screens and show it on larger ones, you write hidden md:block. If the element needs to be flex when visible, write hidden md:flex. The pair must set the same property, display, so that the larger layer can override it.
<aside class="hidden md:block">Sidebar, visible from 768px</aside>
<button class="md:hidden">Menu button, visible below 768px</button>The viewport meta tag matters here
All of this depends on the browser reporting the real device width. Without the tag below, many mobile browsers pretend to be about 980px wide and shrink the page to fit. Your md: and lg: styles would then apply on a phone.
<meta name="viewport" content="width=device-width, initial-scale=1">Put it in the <head> of every page. The example above already includes it.
Common mistakes
Using `sm:` to mean "on small screens"
This is the most frequent mistake. The prefix names a breakpoint, not a device category. sm: means "from 640px up".
<!-- Wrong: this centers text only from 640px up, not on phones -->
<p class="sm:text-center">Centered on phones?</p>
<!-- Right: center by default, then override at larger widths -->
<p class="text-center sm:text-left">Centered on phones, left-aligned from 640px</p>When a style seems to be ignored on a phone, check whether you put a prefix on it. If it should apply on phones, it probably belongs in the base layer.
Designing for desktop first, then patching
Some people write the desktop layout first and add max-* variants to repair small screens. That works, but it fights the default direction. It often leads to longer class lists and conflicting rules. Start from the narrowest layout. If a style is needed only below a width, max-* variants exist for that, and a later chapter covers them.
Assuming a prefix applies only at one width
md:flex-row does not stop at 1024px. It stays active at lg, xl and 2xl unless something wider overrides it. If you want a different value at lg, set it explicitly:
<div class="flex flex-col md:flex-row lg:flex-col">...</div>This gives a column below 768px, a row from 768px to 1023px, and a column again from 1024px.
Forgetting that you test the viewport
Breakpoints respond to the browser viewport width, not the physical device or the width of a parent element. A narrow desktop browser window triggers the base layer. A component placed in a narrow sidebar on a wide screen still sees the wide viewport. Chapters later in this tutorial show container queries as the fix for that case.
Putting the base value after the prefix
Class order inside the class attribute does not decide which style wins. md:p-8 p-4 behaves the same as p-4 md:p-8. Readability is the only reason to write base classes first, so do it consistently.
A workflow that follows the model
- Open your browser's responsive mode at about 360px wide.
- Build the layout with no prefixes until it looks right and works there.
- Slowly widen the window. When the layout starts to look stretched or wasteful, stop.
- Add the smallest prefixed change that fixes it, such as
md:flex-row. - Continue widening, and repeat at the next break.
This method puts breakpoints where your content needs them, not at device widths you guessed in advance. The default breakpoints are only convenient places to attach those changes.
Summary of the model
- No prefix means all widths. A prefix means that width and wider.
- Prefixed rules compile to
min-widthmedia queries, and wider breakpoints win when two rules conflict. - You cannot cancel a base utility. You override it with another value for the same property.
- The viewport meta tag is required for breakpoints to match real device widths.
- Build the narrowest layout first, then add breakpoints only where it breaks.
Exercise
A developer wants a paragraph to be centered on phones and left-aligned on screens 768px wide and larger. They write <p class="md:text-center text-left">. Describe what happens at 500px and at 900px, then correct the classes.
Show answer
At 500px the paragraph is left-aligned, because text-left applies at every width and md:text-center does not match. At 900px it is centered, because md:text-center matches and overrides text-left. This is the reverse of what was intended. The fix is <p class="text-center md:text-left">. The base layer centers the text, and the md: layer overrides it from 768px up. Class order does not matter.
Exercise
You need a navigation list that shows as a vertical stack below 1024px and as a horizontal row from 1024px up. Write the container classes, and say which width the row layout first appears at.
Show answer
Use class="flex flex-col lg:flex-row". The base flex-col gives the vertical stack at every width. lg:flex-row replaces it from 1024px and up, so the row layout first appears at exactly 1024px. Spacing can be added the same way, for example gap-2 lg:gap-6.
Exercise
A sidebar must be hidden on screens narrower than 768px and visible, with display: flex, from 768px up. Which classes do you write, and why is md:flex alone not enough?
Show answer
Write hidden md:flex. The base hidden sets display: none at every width. md:flex sets display: flex from 768px up and overrides it. md:flex alone would leave the sidebar with its default display value on small screens, so it would still show there. The base layer must set the hidden state, and the prefixed layer must set the visible state.