You have the flexibility to create your own custom media queries by utilizing the raw
key within your tailwind.config.js
file. For instance:
module.exports = {
theme: {
extend: {
screens: {
'smh': { 'raw': '(min-height: 640px)' },
// => @media (min-height: 800px) { ... }
'mdh': { 'raw': '(min-height: 800px)' },
// => @media (min-height: 800px) { ... }
'lgh': { 'raw': '(min-height: 1240px)' },
// => @media (min-height: 1240px) { ... }
'xlh': { 'raw': '(min-height: 1600px)' },
// => @media (min-height: 1600px) { ... }
}
}
}
}
Any media queries defined using the raw key will be directly outputted, while any min and max keys specified will be disregarded.
Feel free to choose whatever names you prefer for your custom media queries - I used smh
, mdh
, lgh
, and
xlh</cdoe>, but alternatives like <code>random
,
something
, or
tallerscreen
would work just as well. The important thing is that the name makes sense to you.
These customized responsive utilities function similarly to width-based media queries. For example, after adding your custom responsive utilities in the tailwind.config.js
file, you can apply the following code to change the border color based on screen height:
<div class="border-4 border-blue-500 smh:border-red-500">test</div>
Check out this Tailwind Play link showcasing the above example: Link.
In a similar vein, you could use a setup like this to toggle the visibility of your overflow-y
utility:
<div class="h-[5rem] overflow-y-hidden border-4 smh:overflow-y-visible">
<div>Text.................</div>
<div>Text.................</div>
<div>Text.................</div>
<div>Text.................</div>
<div>Text.................</div>
</div>
When the window height exceeds 640px, anything overflowing from the div becomes visible. Conversely, if the window height is less than 640px, the overflow is hidden.
Explore this scenario on Tailwind Play