Sub topic 12
Testing and debugging responsive layouts
This chapter assumes you know the mobile-first model, the default breakpoints (sm 640px, md 768px, lg 1024px, xl 1280px, 2xl 1536px), and how to apply breakpoint prefixes. It also assumes you have used browser developer tools. Here you will learn how to check that a layout works at every width and how to find the cause when it doesn't.
Test at the boundaries
A breakpoint prefix switches on at an exact width. Bugs cluster at those edges, so test just below and just above each breakpoint you use. For a layout that uses md: and lg:, check these widths:
- 375px: a typical phone, which shows the unprefixed styles.
- 767px and 768px: either side of
md. - 1023px and 1024px: either side of
lg. - 1440px or wider: a large desktop.
In Chrome, Edge, or Firefox, open developer tools and turn on the device toolbar (Ctrl+Shift+M, or Cmd+Shift+M on macOS). Type the exact width into the dimensions box rather than dragging the handle. Dragging rarely lands on the pixel you want.
Browser emulation is not a real phone. Test on at least one physical device before release. Real devices show differences in touch targets, the address bar changing the viewport height, and font rendering.
Show the active breakpoint
A small badge that displays the current breakpoint removes guesswork while you develop. It uses the range variants from the earlier chapter on max-* variants.
<div class="fixed bottom-2 right-2 z-50 rounded bg-black px-2 py-1 font-mono text-xs text-white">
<span class="sm:hidden">base</span>
<span class="hidden sm:max-md:inline">sm</span>
<span class="hidden md:max-lg:inline">md</span>
<span class="hidden lg:max-xl:inline">lg</span>
<span class="hidden xl:max-2xl:inline">xl</span>
<span class="hidden 2xl:inline">2xl</span>
</div>Put it in your root layout while developing and remove it before you ship. Exactly one label should be visible at any width. If you see none or two, your breakpoint setup is not what you think it is, which is itself a useful finding.
Follow a debugging path
When a layout looks wrong, work through the causes in order. The cheapest checks come first.
Common symptoms and causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Phone shows the desktop layout, zoomed out | Missing `<meta name="viewport" ...>` | Add the viewport meta tag to the document head |
| `md:` class has no effect at any width | Class was never generated | Check for dynamic class names and your content or source configuration |
| Style appears on large screens but not small ones | Expected `sm:` to mean "small screens" | Put the small-screen style in the unprefixed class |
| Style works at 800px but breaks at 1100px | Missing override at the next breakpoint | Add a `lg:` class that sets the desired value |
| Hover color shows on touch devices | Hover variant not scoped | Stack the breakpoint with the hover variant, such as `md:hover:` |
| Component ignores its parent's width | Using viewport breakpoints for a component | Use `@container` and `@md:` variants |
Check that the class was generated
Tailwind builds only the classes it finds as complete strings in your source files. This code never produces the class you want:
const cols = 3;
// Tailwind cannot see "md:grid-cols-3" in this string.
const className = `grid grid-cols-1 md:grid-cols-${cols}`;Use full class names that exist in the source instead:
const colClasses = {
2: "md:grid-cols-2",
3: "md:grid-cols-3",
};
const className = `grid grid-cols-1 ${colClasses[3]}`;To confirm, inspect the element in the Elements panel. If the class is on the element but no matching rule appears under Styles, the class was not generated. If the rule appears but is struck through, another rule is overriding it.
Debug container queries
Container query variants respond to the nearest ancestor marked @container, not to the viewport. Resizing the browser can therefore appear to do nothing. Check two things:
- An ancestor has the
@containerclass. - That ancestor's own width crosses the threshold. A card in a narrow sidebar can stay in its small layout on a wide screen.
Exercise
A teammate writes <div class="sm:text-sm text-lg"> and says the text should be small on phones. On a 400px phone the text is large. Why, and what is the fix?
Show answer
Unprefixed utilities apply at all widths, and sm: applies only from 640px upward. So on a 400px phone the unprefixed text-lg wins, and sm:text-sm has not switched on. The fix is to put the phone style in the base class and use the prefix for larger widths: text-sm sm:text-lg.