Sub topic 6
Common mistakes and how to avoid them
This chapter assumes you know basic HTML and CSS, including flexbox and grid. It also assumes you have used Tailwind utilities such as flex, p-4 and text-lg, and that you know what a media query is. Most responsive bugs in Tailwind come from a few wrong ideas about how breakpoint prefixes work. Each section below shows the mistake, why it happens, and the fix.
Mistake 1: Treating sm: as "small screens"
Many developers read sm: as "on small screens" and write this:
<div class="sm:text-center">Centered on phones?</div>This does not center text on phones. Tailwind is mobile-first, so sm: means "at the sm breakpoint and wider". The default sm breakpoint is 640px. A phone narrower than that never matches the rule.
The fix is to put the mobile style on the unprefixed utility. Then use prefixes to change it at larger widths.
<div class="text-center sm:text-left">Centered on phones, left-aligned from 640px</div>Mistake 2: Styling mobile with a prefix and desktop without
A related error is to write the desktop design first and then try to undo it on small screens. This often leads to long lists of overrides.
<nav class="flex-row max-md:flex-col">
<a href="/">Home</a>
<a href="/docs">Docs</a>
</nav>This works, but it fights the mobile-first model. The base style is the desktop one, and you patch the small screens afterward. As the component grows, you need more max-* overrides.
A cleaner version starts from the smallest screen:
<nav class="flex flex-col md:flex-row">
<a href="/">Home</a>
<a href="/docs">Docs</a>
</nav>Use max-* variants only when you need to limit a style to a range. The later chapter on max-* variants covers that case.
Mistake 3: Forgetting the viewport meta tag
If your breakpoints never seem to fire on a real phone, check the document head first. Without the tag below, mobile browsers pretend the screen is about 980px wide. Every phone then matches sm:, md: and often lg:.
<meta name="viewport" content="width=device-width, initial-scale=1">Desktop browser testing can hide this problem, because resizing a desktop window gives true widths. Test on a real device or use device mode in your browser's developer tools.
Mistake 4: Assuming breakpoints apply in a fixed order
Tailwind generates its CSS so that larger breakpoints come later and win over smaller ones. Two rules for the same property can still surprise you. Consider this:
<div class="p-4 md:p-8 lg:p-6">Padding</div>At 1024px and wider, the padding is p-6, not p-8. Each larger prefix replaces the smaller one for the same property. This is correct behavior, but it is easy to forget when you skim a long class list. When a value looks wrong, find the largest prefix that matches the current width. That one wins.
Mistake 5: Using a viewport breakpoint for a component that lives in different spaces
A card grid may sit in a full-width page in one place and in a narrow sidebar in another. A viewport breakpoint like md:grid-cols-2 looks at the whole window, not the space the component has. In a sidebar, the two columns can get squeezed even on a wide screen.
For components like this, use a container query. Mark the parent with @container and use a container variant on the children (Tailwind v4):
<div class="@container">
<div class="grid grid-cols-1 @md:grid-cols-2">
<div>Card</div>
<div>Card</div>
</div>
</div>Use viewport breakpoints for page-level layout, such as the navigation bar and the page columns. Use container queries for reusable components.
Mistake 6: Expecting dynamic class names to work
Tailwind scans your source files for complete class names. It cannot see names built from string pieces. This code fails because the final class never appears in the source as written:
<div class="grid md:grid-cols-${columns}">...</div>Write out the full class names instead, or map values to complete strings in your code:
const columnClasses = {
2: "md:grid-cols-2",
3: "md:grid-cols-3",
};
const classes = `grid ${columnClasses[3]}`;Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Style does not show on phones | Used `sm:` for mobile | Move the style to the unprefixed utility |
| Breakpoints fire at wrong widths on a phone | Missing viewport meta tag | Add the meta tag to the head |
| Many `max-*` overrides | Desktop-first thinking | Start with the mobile layout |
| Component looks cramped in a sidebar | Viewport breakpoint on a reusable component | Use `@container` and `@md:` |
| Responsive class has no effect | Class name built dynamically | Use complete class names |
Exercise
A developer writes <p class="sm:hidden">Tap to open the menu</p> to show a hint only on phones, but the hint is hidden on phones and visible on desktops. Why does this happen, and what is the fix?
Show answer
sm:hidden means "hidden at 640px and wider", so the paragraph is hidden from the sm breakpoint upward. Below 640px, no rule matches, so it shows. The result is the reverse of what the developer wanted on desktop only if desktop is meant to hide it. The simple fix for "show only on phones" is sm:hidden itself, which does hide it on wider screens. If the hint also disappears on phones, check that the viewport meta tag is present. Without it, a phone reports a width near 980px and matches sm:.
Exercise
Rewrite <div class="flex-row max-md:flex-col"> so it follows the mobile-first approach.
Show answer
<div class="flex flex-col md:flex-row">. The base style is the stacked mobile layout, and md:flex-row switches to a row from 768px upward.