Reputation: 135
I have a Svelte project that uses Tailwind CSS for styling.
I added the below media query code in the Tailwind CSS file inside @layer components { ... }
:
@media only screen and (max-width: 1100) {
.toggle {
display: block;
}
header nav {
display: none;
}
}
But it does not work when I change the width of the browser.
I even checked in inspect element, but it appears as if the media query doesn't even exist in the browser.
Why does the CSS media query not work and how can I fix it?
Upvotes: 1
Views: 4727
Reputation: 603
If you want your current code to work you need to add "px" to your max-width in media query, like this:
media only screen and (max-width: 1100px)
However, you don't need to do that in tailwind, preferable way would be adding screens to your tailwind.config.js, like this:
module.exports = {
theme: {
screens: {
'sm': '640px',
// => @media (min-width: 640px) { ... }
'md': '768px',
// => @media (min-width: 768px) { ... }
'lg': '1024px',
// => @media (min-width: 1100px) { ... }
'xl': '1280px',
// => @media (min-width: 1280px) { ... }
'2xl': '1536px',
// => @media (min-width: 1536px) { ... }
}
}
}
In your code you need to do something like this:
className='md:block hidden'
Read this page for further info: https://tailwindcss.com/docs/screens
update
When working with media queries it is better go with mobile-first approach and use min-width, rather than max-width
Upvotes: 1