Sub topic 6
Common mistakes and how to avoid them
This chapter assumes you know the default breakpoints and how to apply prefixes such as md: to utilities. It also assumes you are comfortable with flexbox, grid and media queries. Most responsive bugs in Tailwind come from a small set of wrong mental models. Each section below names a mistake, shows why it fails, and gives the fix.
Mistake 1: reading `sm:` as "small screens"
Prefixes in Tailwind mean "at this width and wider". They do not mean "on this kind of device". The sm: prefix compiles to @media (min-width: 40rem) (640px in the default theme). It does not target phones.
<!-- Intent: centered on phones, left-aligned on larger screens -->
<p class="sm:text-center">Welcome back</p>This markup does the opposite of the intent. On a 375px phone no prefixed rule applies, so the text is left-aligned. From 640px upward it becomes centered. The unprefixed utility is the phone style, and the prefixed one is the change you make when the screen grows.
<p class="text-center sm:text-left">Welcome back</p>Write the smallest layout first with no prefix. Then add prefixes only where the layout must change as the screen widens.
Mistake 2: forgetting the viewport meta tag
Without the tag below, mobile browsers pretend the screen is about 980px wide and then scale the page down. Your phone then matches md: styles, and your layout looks like a shrunken desktop page.
<meta name="viewport" content="width=device-width, initial-scale=1">Put it in the <head> of every page. Most framework starters include it already, so check it first when a layout looks right in desktop devtools but wrong on a real phone.
Mistake 3: designing desktop-first inside a mobile-first tool
Some developers build the desktop layout and then undo it with max-* variants:
<div class="flex flex-row max-md:flex-col">
<aside>Menu</aside>
<main>Content</main>
</div>This works, but it reverses the model the framework is built around. As the page grows, you end up with many max-* overrides that are hard to read. The mobile-first form is shorter to reason about:
<div class="flex flex-col md:flex-row">
<aside>Menu</aside>
<main>Content</main>
</div>Reserve max-* variants for cases where a style truly applies only below a width, as covered in the chapter on ranges.
Mistake 4: expecting HTML class order to decide conflicts
Tailwind generates CSS in its own order. The order you write classes in the class attribute has no effect. Variants such as md: are emitted after unprefixed utilities, so md:p-8 p-4 and p-4 md:p-8 behave the same.
Order matters in one place: two utilities that set the same property at the same breakpoint.
<!-- Which display wins? The one generated later, not the one written later. -->
<div class="md:hidden md:flex">Ambiguous</div>Do not rely on that. Each element should have at most one utility per property per breakpoint. If you see two, one of them is a mistake.
Mistake 5: not resetting a style at a larger width
A prefixed utility continues to apply at every wider breakpoint. If you set md:flex-row and want the layout to stack again at xl, you must say so.
<div class="flex flex-col md:flex-row xl:flex-col">
<div>A</div>
<div>B</div>
</div>The same idea applies to visibility. To show an element only on mid-size screens, hide it again where it should disappear:
<div class="hidden md:block xl:hidden">Mid-size only</div>When a style "leaks" to larger screens, you usually forgot an override at the next breakpoint. When it needs to be limited to a window of widths, stack md:max-xl: instead, which is shorter and states the intent.
Mistake 6: building class names dynamically
Tailwind finds classes by scanning your source files for complete strings. It does not run your code. A class assembled from pieces never appears in the scan, so its CSS is never generated.
// Broken: "md:grid-cols-3" never appears as a whole string in the source
function gridClass(n) {
return `grid grid-cols-1 md:grid-cols-${n}`;
}In development you may see nothing wrong for a while, because the failure shows when the class was never written anywhere else in your project. Keep every class name as a complete literal and select between them:
const cols = {
2: "md:grid-cols-2",
3: "md:grid-cols-3",
4: "md:grid-cols-4",
};
function gridClass(n) {
return `grid grid-cols-1 ${cols[n]}`;
}
console.log(gridClass(3)); // grid grid-cols-1 md:grid-cols-3The same rule applies to breakpoint prefixes. Never build ${bp}:hidden from a variable.
Mistake 7: fixed widths that cause horizontal scrolling
A fixed pixel width does not shrink with the screen. A 600px element on a 375px phone forces the page to scroll sideways.
<!-- Overflows on phones -->
<div class="w-[600px]">Panel</div>
<!-- Fills the screen on phones, stops growing at 36rem -->
<div class="w-full max-w-xl">Panel</div>Prefer fluid widths (w-full, max-w-*, grid and flex sizing) as the base style. Use breakpoints to change how much space a block takes, not to rescue a rigid size. Wide tables, long unbroken words and <pre> blocks are other common sources of overflow. Wrap tables in a container with overflow-x-auto, and use break-words on user-generated text.
Mistake 8: misunderstanding where `max-*` ends
The max-md: variant applies below 768px, and md: applies at 768px and above. The two do not overlap and leave no gap, so they split the width axis cleanly at one point. Problems arise when you mix variants from different breakpoints without checking the edges.
<!-- Visible on phones, hidden from 640px up -->
<div class="max-sm:block hidden">Wrong idea</div>The example above is confusing: hidden is the base style, so the element is hidden everywhere except below sm. The clearer way to say "visible only on the smallest screens" is:
<div class="sm:hidden">Visible below 640px</div>If you need a window, name both ends:
<div class="md:max-lg:hidden">Hidden from 768px up to, but not including, 1024px</div>Mistake 9: using viewport breakpoints for a component that does not follow the viewport
A card grid that uses md:grid-cols-2 assumes the card area is as wide as the screen. Put the same component in a narrow sidebar on a 1400px monitor, and it still gets two columns because the viewport is wide, even though the sidebar is 300px.
When the right measure is the parent's width, use a container query:
<div class="@container">
<div class="grid grid-cols-1 @md:grid-cols-2">
<div>Card</div>
<div>Card</div>
</div>
</div>In Tailwind v4, @container is built in, and @md: responds to the container's width. In v3, container queries need the @tailwindcss/container-queries plugin. Use viewport breakpoints for page-level structure such as the header, the sidebar and the page grid. Use container queries for reusable components that appear in different places.
Mistake 10: relying on hover for touch screens
Responsive design includes input type, not just width. A md:hover:bg-blue-700 style that adds a hover effect only on wider screens still assumes a mouse. Tablets at md width are often touch-only, and a tap can leave a "sticky" hover state on older setups. Tailwind v4 applies hover: only when the primary input device supports hover, which reduces this problem. In v3, you can opt in the same way with the hoverOnlyWhenSupported future flag.
Never put essential information or controls behind hover. Treat hover as decoration.
Mistake 11: overriding the default screens by accident
In a Tailwind v3 config, where you place the key decides whether you add a breakpoint or replace them all.
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
// Replaces sm, md, lg, xl and 2xl entirely
screens: {
tablet: '700px',
},
},
};After this change, md: and lg: stop working, and the build does not warn you. The classes simply produce no CSS. To keep the defaults and add one, put the entry under extend:
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
screens: {
'3xl': '1920px',
},
},
},
};In Tailwind v4, the equivalent addition lives in CSS and keeps the defaults:
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
}Another trap is ordering. Because prefixes are min-width rules, custom breakpoints should be listed from smallest to largest in v3, or the larger styles can lose to the smaller ones.
Mistake 12: testing only by dragging a desktop window
Resizing a desktop browser window tests widths, but it misses touch behavior, real device pixel ratios, address-bar resizing and the viewport meta tag. A reliable routine takes only a few minutes:
- Open devtools device mode and test at widths just below and at each breakpoint you use, such as 639, 640, 767, 768, 1023 and 1024.
- Check for horizontal scrolling at 320px, the narrowest common width.
- Rotate to landscape, since a phone in landscape can cross
smor evenmd. - Open the page on one real phone before shipping.
A quick diagnostic flow
When a style does not appear at the width you expect, work through the checks in this order.
Summary table
| Symptom | Likely cause | Fix |
|---|---|---|
| Phone shows the desktop layout, scaled down | Missing viewport meta tag | Add `width=device-width, initial-scale=1` |
| Style appears on big screens but not on phones | Used `sm:` to mean "small" | Unprefixed class for phones, prefix for larger |
| Class has no effect, no error | Class name built from string pieces | Use full literal class names |
| `md:` and `lg:` stopped working | `theme.screens` replaced the defaults | Move entries under `theme.extend.screens` |
| Style leaks to wide screens | No override at the next breakpoint | Add a reset or use `md:max-xl:` |
| Page scrolls sideways on phones | Fixed pixel widths or wide content | `w-full max-w-*`, `overflow-x-auto` |
| Component wrong inside a sidebar | Viewport breakpoints used for a component | Use `@container` and `@md:` |
Exercise
A developer writes <div class="sm:flex-col md:flex-row"> expecting a column on phones and a row on tablets. On a 400px-wide phone the children sit in a row. Explain why, and write the corrected class list.
Show answer
Flex containers default to flex-direction: row, and sm:flex-col starts only at 640px, so at 400px no rule applies. The base style is also not flex, so the element is not even a flex container below 640px. The corrected list is flex flex-col md:flex-row: the unprefixed classes set the phone layout, and md:flex-row changes it from 768px upward.
Exercise
A component library renders <div class={grid md:grid-cols-${columns}}>. In production the columns do not change when columns is 3. Why, and how do you fix it?
Show answer
Tailwind scans source files for complete class strings and does not run the code. The string md:grid-cols-3 never appears in the source, so no CSS is generated for it. Fix it by mapping each value to a full literal class, such as { 2: "md:grid-cols-2", 3: "md:grid-cols-3" }, and looking the class up with columns.
Exercise
After a config edit, md: and lg: utilities produce no styles, but a new tablet: utility works. What is the likely cause and the fix?
Show answer
The new tablet entry was placed under theme.screens instead of theme.extend.screens, which replaced all default breakpoints. Move the entry under extend to keep sm through 2xl alongside tablet.