Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Tailwind md:, lg: and sm: classes do not override as expected

Errorsm:text-center has no effect on a phone

Short answer

Tailwind is mobile-first: a plain class applies at every width, and sm:, md: or lg: applies at that width and above. So sm: does not mean phones. Put the mobile style first with no prefix, add the larger style with a prefix, use max-* to cap a range, and write class names out in full.

  • Applies to Tailwind CSS 4, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Resize the browser and open the element in dev tools. Check which media query each class sits in, then compare it with the width where you expected the change.

    grep -rn "md:" resources/views | head
    npm run build
  2. 2Match what you see, then jump to its fix

    1. 1The style appears only from 640px upward and never on a phonesm: was used to target small screensFix
    2. 2md:flex works, but the same layout is wanted only between two widthsA style from a smaller breakpoint is still applied at larger sizesFix
    3. 3The class is in the HTML, but no CSS rule exists for it in the built stylesheetThe class name is built at runtime, so Tailwind never generates itFix

What does the error look like?

  • sm:text-center has no effect on a phone
  • md:flex still applies on large screens
  • A breakpoint class works only when written by hand, not when built from a variable

Cause 1 of 3

sm: was used to target small screens

1Check

Unprefixed utilities take effect at every size, while prefixed ones apply at that breakpoint and above. The docs call out sm: for mobile as a common mistake.

grep -rn "class=.*sm:" resources/views | head

2Fix

Write the mobile style with no prefix and the larger-screen style with the prefix.

<!-- wrong: centers only from 640px up -->
<div class="sm:text-center"></div>

<!-- right: centered on mobile, left aligned from 640px up -->
<div class="text-center sm:text-left"></div>

3Verify

Rebuild and check the page at 375px and at 800px.

npm run build

Confirmed by tailwindcss.com

Did this fix it?

Cause 2 of 3

A style from a smaller breakpoint is still applied at larger sizes

1Check

Styles such as md:flex stay applied at larger breakpoints, so a rule meant for one range leaks into the next.

grep -rn "md:" resources/views | head

2Fix

Stack the breakpoint with a max-* variant to limit the range, or set the opposite style at the next breakpoint.

<!-- flex only from 768px up to below 1280px -->
<div class="md:max-xl:flex"></div>

<!-- or undo it at the next breakpoint -->
<div class="md:flex xl:block"></div>

3Verify

Check the element at widths just below and above each breakpoint.

npm run build

Confirmed by tailwindcss.com

Did this fix it?

Cause 3 of 3

The class name is built at runtime, so Tailwind never generates it

1Check

Tailwind reads source files as plain text and cannot see names assembled from pieces such as md:grid-cols-{{ $n }}.

grep -rn "md:grid-cols-\$\|md:grid-cols-{{" resources/views | head

2Fix

Write each complete class name somewhere in the source, for example in a map that picks one of them.

@php
  $cols = [2 => 'md:grid-cols-2', 3 => 'md:grid-cols-3'];
@endphp
<div class="grid {{ $cols[$n] }}"></div>

3Verify

Rebuild and search the output CSS for the class.

npm run build

Confirmed by tailwindcss.com

Did this fix it?

How do I stop it coming back?

  • Design the smallest screen first and add one prefix per larger layout step.
  • Never assemble class names from variables; map values to complete class strings.
  • Test at widths on both sides of every breakpoint you use, not only at the device presets.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Laravel cannot find a Sass or JS entry in the Vite manifest

Unable to locate file in Vite manifest: resources/sass/app.scss

The @vite directive asks the built manifest for a file, and that file is listed only if it is an entry in the laravel() input option and the assets were built after adding it.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Web apps

Next.js fails with Cannot find module 'tailwindcss'

Error: Cannot find module 'tailwindcss'

Next.js loads Tailwind through PostCSS, so the error means the package named in postcss.config is not installed where the build runs.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.