tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

Adding non-pixel media queries to screens config prevents auto-generated max-* classes for the rest of the breakpoints

Open curtisburns opened this issue 1 year ago • 6 comments

What version of Tailwind CSS are you using?

v3.3.3

What build tool (or framework if it abstracts the build tool) are you using?

Next.js 13.5.4

What version of Node.js are you using?

v18.16.0

What browser are you using?

All

What operating system are you using?

macOS

Reproduction URL

https://play.tailwindcss.com/hzEFzrG4NO

Describe your issue

module.exports = {
  theme: {
    extend: {
      screens: {
        // => @media (min-width: 400px) { ... }
        sm: '400px',
        // => @media (min-width: 612px) { ... }
        md: '612px',
        // => @media (min-width: 768px) { ... }
        lg: '768px',
        // => @media (min-width: 1024px) { ... }
        xl: '1024px',
        // Comment out this line to see max-* class work again.
       'has-hover': { raw: '(hover: hover)' },
      },
    },
  },
  variants: {},
  plugins: [],
}

As title says, any additional media queries such as hover:hover or min-aspect-ratio: 1 will prevent the auto-generation of the max-* classes, which makes sense as you can't have a max hover, but I feel they should still generate for those where it applies, or we should at least get a warning when generated unsuccessfully. This leads to styling not being applied for max breakpoints, and no way for the dev to know unless it's visually noticeable in the browser.

As I had trouble figuring out where the max-* classes were defined while trying to identify the bug, once I remembered that they were auto-generated (tucked away in the docs here), I just explicitly defined these in the config as well. It's easier, then, to trace for any other devs unfamiliar with TW.

{
  sm: '400px',
  md: '612px',
  lg: '768px',
  xl: '1024px',

  'max-sm': { max: '399px'},
  'max-md':  { max: '611px'},
  'max-lg':  { max: '767px'},
  'max-xl':  { max: '1023px'},

  // This now has no impact on the max-* classes as we define them above
  'has-hover': { raw: '(hover: hover)' },
}

Is this the expected behaviour? Should there be more notice when the max-* classes aren't successfully generated? Should docs surrounding the max-* classes be easier to find, or added to the screens section? Quick search is no help, unfortunately.

Thanks!

curtisburns avatar Feb 25 '24 14:02 curtisburns

Have just seen this - https://github.com/tailwindlabs/tailwindcss/pull/9558#user-content-restrictions

This should be added to the docs, it would save a lot of people a good few hours to eventually stumble across this caveat.

curtisburns avatar Feb 25 '24 14:02 curtisburns

Yeah I really thought this was documented but will make a note to update the docs 👍

For what it's worth I'd use a totally custom variant for your example and keep screens focused to just breakpoints:

https://play.tailwindcss.com/m29hlgZJSc?file=config

adamwathan avatar Feb 27 '24 02:02 adamwathan

This restriction effectively means it's impossible to have a height-based breakpoint. Since heights require the use of the 'mybreakpoint': {'raw': '(min-height: 123px)'}, they will always conflict with max-* breakpoints.

mathrick avatar Mar 01 '24 17:03 mathrick

Here is a workaround:

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  theme: {
    extend: {
      screens: {
        'max-sm': { raw: `not all and (min-width: ${defaultTheme.screens.sm})` },
        'max-md': { raw: `not all and (min-width: ${defaultTheme.screens.md})` },
        'max-lg': { raw: `not all and (min-width: ${defaultTheme.screens.lg})` },
        'max-xl': { raw: `not all and (min-width: ${defaultTheme.screens.xl})` },
        'max-2xl': { raw: `not all and (min-width: ${defaultTheme.screens['2xl']})` },

        // You can now have your custom media queries and still use max-* ranges.
        tall: { raw: '(min-height: 800px)' },
      },
    },
  },
}

The not all and is what Tailwind normally generates for those classes.

verekia avatar Mar 19 '24 10:03 verekia

+ 1 to improve documentation or show some kind of warning or error on compile if this is not supported. Otherwise, it silently breaks many styles

illiaChaban avatar May 28 '24 02:05 illiaChaban

Like @mathrick I wound up here after seeing the example in the docs that used raw in combination with min-height:

https://play.tailwindcss.com/3n98EXEVKG?size=464x720&file=config

I appreciate @verekia's workaround, though!

searls avatar Jun 21 '24 12:06 searls