Sub topic 4
Applying breakpoint prefixes
This chapter assumes you know basic HTML and CSS, including flexbox and grid. It also assumes you have used Tailwind utility classes such as flex, p-4 and text-lg, and that you know what a CSS media query is. Earlier chapters covered the viewport meta tag, the mobile-first model and the default breakpoint values. Here you apply them.
What a breakpoint prefix does
A breakpoint prefix is a label placed before a utility class, separated by a colon. It makes the utility apply only when the viewport is at least a certain width. The prefix md: in md:flex-row means "use flex-row when the viewport is at least the md width".
Under the hood, Tailwind wraps the utility in a min-width media query. For the default md breakpoint, the generated CSS is roughly:
@media (min-width: 48rem) {
.md\:flex-row {
flex-direction: row;
}
}Because the query uses min-width, the style stays active at every larger width too. Once a viewport is wide enough for md:, it is also wide enough for sm:. The prefix sets a lower bound and never an upper one.
The default breakpoints are:
| Prefix | Minimum width | Media query |
|---|---|---|
| `sm:` | 640px (40rem) | `@media (min-width: 40rem)` |
| `md:` | 768px (48rem) | `@media (min-width: 48rem)` |
| `lg:` | 1024px (64rem) | `@media (min-width: 64rem)` |
| `xl:` | 1280px (80rem) | `@media (min-width: 80rem)` |
| `2xl:` | 1536px (96rem) | `@media (min-width: 96rem)` |
There is no prefix for the smallest screens. A utility with no prefix applies at every width, and that is where you put your mobile styles.
The layering model
Think of a class list as layers. The unprefixed class is the bottom layer. Each prefixed class is added on top when the viewport grows past its threshold.
<h1 class="text-2xl font-bold md:text-4xl lg:text-5xl">Responsive heading</h1>The text is text-2xl below 768px, text-4xl from 768px to 1023px, and text-5xl from 1024px up. font-bold has no prefix, so it holds at all widths.
When two utilities set the same property and both match, the larger breakpoint wins. Tailwind emits the prefixed rules in order from smallest breakpoint to largest. At equal specificity, later CSS overrides earlier CSS. You do not need to write the classes in any particular order in the HTML. The order in the generated stylesheet decides.
Worked example: a card grid
Start with the smallest screen and add columns as space grows.
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div class="rounded border p-4">One</div>
<div class="rounded border p-4">Two</div>
<div class="rounded border p-4">Three</div>
</div>Reading the classes from left to right:
grid-cols-1gives one column at every width.sm:grid-cols-2switches to two columns from 640px.lg:grid-cols-3switches to three columns from 1024px.
The gap-4, rounded, border and p-4 classes are unprefixed, so they never change. Only the property that needs to differ gets a prefix.
You do not need to repeat a utility at every breakpoint. Writing grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 is redundant, because md:grid-cols-2 repeats what sm: already set. Add a prefix only where the value changes.
Worked example: a navigation that changes direction
Layout direction is a common thing to change. Stack on small screens, lay out in a row on larger ones.
<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>Here flex and flex-col apply by default, so the links stack with an 8px gap. From 768px, md:flex-row replaces the direction, md:items-center aligns items on the cross axis, and md:gap-6 widens the gap to 24px.
Notice that md:items-center is added only at md. In a column, the default items-stretch is what you want. In a row, you usually want vertical centering. A prefix lets each layout have its own alignment rules.
Prefixes work on any utility
Breakpoint prefixes are not limited to layout. You can put them on any utility: spacing, color, typography, display, position, and more.
<section class="px-4 py-8 md:px-8 md:py-16">
<p class="text-base text-gray-700 lg:text-lg">Body copy</p>
<img class="hidden md:block" src="/hero.png" alt="Product screenshot">
</section>The padding grows at md. The paragraph grows its text at lg. The image is hidden by default and becomes block from md. Showing and hiding by breakpoint is a standard use, but remember that a hidden image can still be downloaded by the browser, because display: none does not stop an img from loading in every case. Use it for layout, not to save bandwidth.
Combining a breakpoint with a state variant
Prefixes can be combined with state variants like hover:, focus: and dark:. They stack from left to right, and the breakpoint conventionally comes first.
<button class="bg-blue-600 md:hover:bg-blue-700">Save</button>The button is blue at every width. The darker hover color applies only when the viewport is at least md and the pointer is over the button. This is useful because hover has little meaning on touch devices.
A later chapter covers stacking in more detail. For now, remember that each variant adds a condition, and all conditions must be true for the utility to apply.
Overriding at a larger breakpoint
Because prefixed utilities override unprefixed ones, you can reset a value at a larger width. To undo a mobile style, set a different value.
<div class="w-full md:w-1/2 lg:w-auto">
Full width, then half, then natural width
</div>At lg, w-auto replaces w-1/2. The same idea applies to hidden and block, flex-col and flex-row, or p-4 and p-0. There is no "undo" keyword. You override with the value you want.
Common mistakes with prefixes
Reading `sm:` as "small screens"
sm: means "640px and wider". It does not mean "only small screens". If you write sm:text-center expecting phones to be centered, nothing happens on a 375px phone. The style starts at 640px and continues upward.
To style small screens, use the unprefixed class. To style only below a width, use a max-* variant, covered in a later chapter.
<!-- Wrong: centers text only at 640px and wider -->
<p class="sm:text-center">Hello</p>
<!-- Right: centers on mobile, left-aligns from 640px -->
<p class="text-center sm:text-left">Hello</p>Writing desktop styles first
If you start from a desktop design and add prefixes to shrink it, you fight the model. Start with the unprefixed mobile layout and add prefixes for larger widths. Your class lists stay shorter and easier to read.
Dynamic class names
Tailwind scans your source files for complete class names. It cannot see a class assembled at runtime.
// Broken: Tailwind never sees "md:grid-cols-3" as a whole string
const cols = 3;
const className = `md:grid-cols-${cols}`;
// Works: full class names appear in the source
const colClasses = {
2: "md:grid-cols-2",
3: "md:grid-cols-3",
};
const className2 = colClasses[cols];With the first version, the CSS for md:grid-cols-3 may not be generated, and the layout will silently fail.
Forgetting the viewport meta tag
Without this tag in the document head, many mobile browsers render the page at a virtual width of around 980px and scale it down. Your md: and lg: styles then fire on phones.
<meta name="viewport" content="width=device-width, initial-scale=1">Missing space or wrong colon
The prefix and utility are joined with a colon and no spaces. md: flex and md-flex are not valid. Tailwind will not generate anything for them.
Checking your work
A quick way to confirm a prefix is working is to resize the browser and watch the layout change at the expected widths. Browser developer tools show the active media query on each rule in the Styles panel. A rule that appears struck through has been overridden by another that matched. Later chapters cover testing in more detail.
Summary of the working model
- Unprefixed utilities apply at all widths.
- A prefix such as
md:applies from that width upward, usingmin-width. - When utilities conflict, the largest matching breakpoint wins.
- Only add a prefix where the value changes.
- Prefixes work with every utility and combine with state variants.
Exercise
A paragraph has the classes sm:text-center. A user opens the page on a phone with a 400px-wide viewport. Is the text centered? Rewrite the classes so the text is centered below 640px and left-aligned from 640px up.
Show answer
No. sm: applies only from 640px upward, so on a 400px viewport the utility has no effect. Use text-center sm:text-left. The unprefixed text-center applies at all widths, and sm:text-left overrides it from 640px.
Exercise
Write the classes for a div that uses a one-column grid by default, two columns from 768px, and four columns from 1280px. Which prefixes do you use, and is a lg: class needed?
Show answer
grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4. You use md: for 768px and xl: for 1280px. No lg: class is needed because the value does not change at 1024px. The two-column layout from md: continues through that width.