Sub topic 4
Applying breakpoint prefixes
This chapter assumes you know basic HTML and CSS, including flexbox and grid. It also assumes you know what a media query is and have used Tailwind utilities such as flex, p-4 and text-lg. You should also know the mobile-first model: an unprefixed utility applies at every width.
What a prefix does
A breakpoint prefix is a variant you put in front of a utility, separated by a colon. md:flex means "apply flex when the viewport is at least the md width." Tailwind compiles it to a min-width media query. The utility has no effect below that width, and it keeps applying at every larger width unless a larger prefix overrides it.
The default breakpoints are:
| Prefix | Minimum width | Media query |
|---|---|---|
| `sm` | 640px | `@media (min-width: 640px)` |
| `md` | 768px | `@media (min-width: 768px)` |
| `lg` | 1024px | `@media (min-width: 1024px)` |
| `xl` | 1280px | `@media (min-width: 1280px)` |
| `2xl` | 1536px | `@media (min-width: 1536px)` |
Mobile browsers only report their real width if the page has the viewport meta tag. Without it, prefixes will seem to misfire on phones.
<meta name="viewport" content="width=device-width, initial-scale=1">Changing a value at one breakpoint
Start with the base style for the smallest screens. Then add prefixed utilities that change only what must change.
<h1 class="text-2xl font-bold md:text-4xl lg:text-5xl">Responsive heading</h1>Here is what the browser applies at each width:
| Viewport width | Font size |
|---|---|
| Under 768px | `text-2xl` |
| 768px to 1023px | `text-4xl` |
| 1024px and wider | `text-5xl` |
font-bold has no prefix, so it applies everywhere. Each prefixed class overrides the previous value because its media query is later in the generated CSS and has the same specificity.
Changing layout direction
Prefixes work on any utility, including ones that change layout mode. This navigation stacks on phones and becomes a row from md upward.
<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 classes in effect are flex flex-col gap-2. From md up, flex-row, items-center and gap-6 take over. Note that flex is not repeated, because it already applies at all widths.
Changing column counts
Grids use the same pattern. Set the one-column base first, then raise the count.
<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>This gives one column under 640px, two from 640px, and three from 1024px. grid-cols-1 is technically optional, since a grid defaults to one column. Writing it makes the intent visible to the next reader.
Common errors
- Using
sm:to mean "on small screens."sm:means 640px and wider. To style only phones, write the base utility and override it at a larger prefix. - Forgetting the base style. If you write only
md:flex-row, the element has no special behavior belowmd. That may be fine, but check that the default still looks right. - Missing the viewport meta tag. Phones then render at a virtual width of about 980px, so
mdandlgrules may fire on a phone. - Writing prefixes out of order.
lg:text-2xl md:text-4xlstill works because Tailwind orders the CSS itself. It is harder to read, though.
Try it
Edit the grid example so it shows two columns from md instead of sm, and four columns from xl. Resize the browser window and watch where the columns change. Open the developer tools to check the widths at each change.
Exercise
A card has the classes block md:flex lg:block. What display value does it have at 900px wide, and at 1100px wide?
Show answer
At 900px it is flex, because md:flex applies from 768px and lg:block has not started. At 1100px it is block, because lg:block starts at 1024px and overrides md:flex.
Exercise
You write <div class="sm:text-center"> and test on a 400px wide phone. The text is not centered. Why, and how would you center it only on phones?
Show answer
sm: applies from 640px and wider, so it does nothing at 400px. To center on phones, use the unprefixed text-center. If it should return to left alignment on wider screens, add sm:text-left.