Sub topic 12
Testing and debugging responsive layouts
This chapter assumes you know the default breakpoints, the mobile-first model, and how sm:, md: and max-* variants combine. It covers how to check that a layout works at every width and how to find the cause when it doesn't.
Why responsive bugs are hard to see
A responsive layout is several layouts that share one HTML file. Each breakpoint switches a different set of utilities on. A bug often shows at only one width range, so you can miss it if you test at your own laptop width and one phone size.
Most responsive bugs fall into four groups:
- The breakpoint never fires, for example because the viewport meta tag is missing.
- The breakpoint fires, but the utility isn't in the generated CSS.
- Two utilities conflict, and the wrong one wins.
- The layout is correct at the tested widths and broken between them, usually through horizontal overflow.
Test at the boundaries, not the middle
Tailwind's default breakpoints are min-width media queries. A md: utility applies at 768px and above. It does not apply at 767px. Widths just below and at each breakpoint are where the layout changes, so test those.
| Breakpoint | Starts at | Test just below | Test at |
|---|---|---|---|
| base | 0 | 320 | 375 |
| `sm` | 640px | 639 | 640 |
| `md` | 768px | 767 | 768 |
| `lg` | 1024px | 1023 | 1024 |
| `xl` | 1280px | 1279 | 1280 |
| `2xl` | 1536px | 1535 | 1536 |
Add 320px as the narrowest realistic phone width. Add one very wide width such as 1920px to check that content doesn't stretch without limit.
If you changed the breakpoints (see the customizing chapter), use your own values in this table.
Use the browser's responsive mode
Every major browser has a responsive design mode in its developer tools. In Chrome and Edge, open DevTools and toggle the device toolbar. Firefox calls it Responsive Design Mode.
Use it in three ways:
- Type exact widths into the width field and step through the table above.
- Drag the edge of the viewport slowly from 320px to 1920px. Watch for jumps, overlaps and horizontal scrollbars between breakpoints.
- Open the Elements or Inspector panel and check the Styles pane. It shows which media query a rule belongs to. Rules inside a media query that doesn't match are not applied, and the pane marks them.
Device presets are useful for touch behavior and pixel density. For layout work, exact widths are more reliable than device names, because one named device can report different widths in portrait and landscape.
Add a breakpoint indicator
While developing, it helps to see which breakpoint is active. This small badge uses the same variants you are testing:
<div class="fixed bottom-2 left-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>Each label is hidden by default. It shows only inside its own range, built by stacking a minimum variant with a max-* variant. Because it uses real breakpoints, it also confirms that the viewport meta tag and your build are working. If the badge says base on a wide window, the problem is in your setup, not in your layout classes.
Remove the badge, or render it only in development, before you ship.
A debugging workflow
Work through the causes in order, from cheapest to check to most involved.
1. Check the viewport meta tag
The head of the document must contain this tag:
<meta name="viewport" content="width=device-width, initial-scale=1">Desktop DevTools device mode may emulate a viewport even when the tag is missing, so a page can look right there and fail on a real phone. Test on a real device at least once.
2. Check that the class exists in the CSS
Tailwind generates CSS by scanning your source files for complete class names. It cannot evaluate string building. This breaks:
<div class="md:grid-cols-${columns}">Broken</div>The scanner sees md:grid-cols-${columns} and finds no valid class. Write out every full class name instead, or map values to complete strings:
const columnClasses = {
2: 'md:grid-cols-2',
3: 'md:grid-cols-3',
4: 'md:grid-cols-4',
};
const className = `grid grid-cols-1 ${columnClasses[columns]}`;Tailwind v4 detects source files automatically and ignores files in .gitignore. In Tailwind v3, the content array in tailwind.config.js must include every file that contains classes. If a utility works in one file and not in another, a missing path is the likely cause.
To confirm, search the compiled CSS for the escaped class name, for example md\:grid-cols-3. In DevTools, you can also select the element and check whether the class appears in the Styles pane at all.
3. Check that the media query matches
In the Styles pane, find your rule and read its media query. For an md: utility, expect min-width: 48rem in v4 or min-width: 768px in v3. Common causes of a mismatch:
- You used
max-md:when you meantmd:. They select opposite sides of 768px. - You overrode the breakpoints in the theme and the value differs from what you assumed.
- You expected
sm:to mean "small screens". It means 640px and wider, which includes desktops.
4. Check for conflicting utilities
Two utilities that set the same property at the same breakpoint are resolved by the order of rules in the generated stylesheet. The order of class names in your class attribute is irrelevant. Suppose an element has p-4 p-8. You cannot predict which one wins by reading the attribute, so avoid the conflict.
Across breakpoints, Tailwind emits variants in a fixed order, so larger breakpoints come later and win when they match. This is what makes the mobile-first model work. A conflict is more likely between a breakpoint utility and a variant of a different kind, such as dark: or a max-* variant. Look in the Styles pane for the rule that is struck through. It shows what is being overridden and by what.
5. Check for overflow and flex or grid sizing
If the classes are right and the layout is still wrong, content is probably wider than its container. Paste this in the DevTools console to list elements that extend past the viewport:
const viewportWidth = document.documentElement.clientWidth;
document.querySelectorAll('*').forEach((el) => {
const rect = el.getBoundingClientRect();
if (rect.right > viewportWidth + 1) {
console.log(Math.round(rect.right), el);
}
});Common culprits:
- A long unbroken string, such as a URL or token. Add
break-wordsto its container. - A table or code block wider than the screen. Wrap it in
overflow-x-auto. - A flex or grid child that won't shrink. Grid and flex items have an automatic minimum size, so
min-w-0on the child often fixes it. w-screenor100vwwidths. These include the vertical scrollbar width on some platforms, which creates a small horizontal scroll.- Fixed widths such as
w-96with no smaller-screen alternative. Preferw-full max-w-smstyle combinations.
Automate the checks
Manual testing doesn't scale. A browser-based test can set the viewport to each boundary width and assert on the computed layout. This Playwright test checks the three-column grid from earlier chapters. It assumes the page is served at http://localhost:3000/ and the grid element has data-testid="grid":
import { test, expect } from '@playwright/test';
const cases = [
{ width: 375, columns: 1 },
{ width: 639, columns: 1 },
{ width: 640, columns: 2 },
{ width: 1023, columns: 2 },
{ width: 1024, columns: 3 },
];
for (const { width, columns } of cases) {
test(`grid has ${columns} column(s) at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 800 });
await page.goto('http://localhost:3000/');
const count = await page
.locator('[data-testid="grid"]')
.evaluate((el) => getComputedStyle(el).gridTemplateColumns.split(' ').length);
expect(count).toBe(columns);
});
}The test reads the computed grid-template-columns, which the browser resolves to a list of pixel sizes. Counting those values gives the column count. Testing the computed result, and not the class names, catches problems anywhere in the chain from source to rendered page.
Add an overflow check to the same suite:
import { test, expect } from '@playwright/test';
for (const width of [320, 375, 768, 1024, 1440]) {
test(`no horizontal scroll at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 800 });
await page.goto('http://localhost:3000/');
const overflow = await page.evaluate(
() => document.documentElement.scrollWidth > document.documentElement.clientWidth
);
expect(overflow).toBe(false);
});
}Screenshot comparison tests also work for catching visual regressions, but they need a stable baseline and fixed data. Start with the structural assertions above, since they fail with clearer messages.
Container queries need their own testing
A component that uses @md: responds to its container's width, not the viewport's. Resizing the browser window changes the result only indirectly. When you debug one:
- Confirm that an ancestor has the
@containerclass. Without it, the@md:utility has no container to measure and does nothing. - Test the component in its real placements, such as a narrow sidebar and a wide main column, not only at different window sizes.
- Remember the container is the nearest ancestor marked
@container. A closer wrapper added later can change which width is measured.
Edge cases worth knowing
- Zoom changes widths. Browser zoom at 200% halves the CSS viewport width, so a 1280px window behaves like 640px. This is correct, and it's a useful accessibility test.
- Orientation changes widths. A phone rotated to landscape can cross
sm:ormd:. Test both orientations. - Rem-based breakpoints respond to font size. In Tailwind v4, the default breakpoints are in
rem. A user who raises their browser's default font size reaches breakpoints at wider pixel widths. Treat that as a feature when you test. - Dev servers cache. If a new class doesn't appear after you add it, check that the build process is running and restart it if the config changed.
Summary
Test at the pixel just below and at each breakpoint. Use a breakpoint badge to confirm which range is active. When a utility fails, move through the same sequence each time: viewport tag, generated CSS, matching media query, conflicts, overflow. Encode the boundary checks in browser tests so they run on every change.
Exercise
A teammate writes <div class="md:grid-cols-${n}"> inside a template string. The grid never changes at 768px, and DevTools shows no grid-cols rule at that width. What is the cause, and how do you fix it?
Show answer
Tailwind scans source text for complete class names, and md:grid-cols-${n} is not a complete class name, so no CSS is generated for it. Fix it by writing each full class name in the source, for example a lookup object that maps 2, 3 and 4 to md:grid-cols-2, md:grid-cols-3 and md:grid-cols-4.
Exercise
Which two viewport widths should a test use to confirm that an lg: utility starts applying at the right place, and what should differ between them?
Show answer
Use 1023px and 1024px. The lg: utility should not apply at 1023px and should apply at 1024px, because lg is a min-width: 1024px breakpoint (64rem at default font size).
Exercise
On a real phone your md:flex-row layout shows as a row, but the phone is only about 390px wide. DevTools shows a stacked layout at 390px. What do you check first?
Show answer
Check that the page has <meta name="viewport" content="width=device-width, initial-scale=1">. Without it, mobile browsers use a virtual layout width of around 980px, so md: rules apply on the phone.
Exercise
Write the class list for a badge element that is visible only from md up to, but not including, lg, and hidden everywhere else.
Show answer
hidden md:max-lg:inline (or md:max-lg:block). The element is hidden by default, and the stacked md:max-lg: variant sets it visible only between 768px and 1023px.