Replies: 11 comments 47 replies
-
Could you expand on this? Because
It could be the case when you have content that is hidden/revealed on hover, but then thats also a problem for eveybody that's not using a mouse.
It can be done with plugins |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The behavior of |
Beta Was this translation helpful? Give feedback.
-
Exactly and this inconsistency is another reason to want to disable them. |
Beta Was this translation helpful? Give feedback.
-
Best way is to use media query to detect if a device supports hover and only disable it if it does not. This can be done by adding this to your
Now you can use it like this : |
Beta Was this translation helpful? Give feedback.
-
The downside to @VaibhavAcharya's solution is that it is currently not possible to make it responsive. |
Beta Was this translation helpful? Give feedback.
-
The Tailwind classes: pointer-events-none Are everything you need |
Beta Was this translation helpful? Give feedback.
-
@VaibhavAcharya's solution still required me to write out |
Beta Was this translation helpful? Give feedback.
-
If you are using Tailwind with postcss maybe this plugin can help postcss-hover-media. |
Beta Was this translation helpful? Give feedback.
-
For anyone bumping into this thread using v3, there's now a tailwind flag you can use to disable hover on mobile devices without downloading anything. from this pull request Just add:
|
Beta Was this translation helpful? Give feedback.
-
Answer updated for v4 - see the v3 version at the bottom of this post. Here's my current solution which covers all use-cases, which adds a few custom variants:
Note: By PC I mean device supporting hover, and by mobile I mean device not supporting hover, for simplicity. To use, simply add this to your css file: @custom-variant has-hover (@media (hover: hover));
@custom-variant no-hover (@media not all and (hover: hover));
@custom-variant hover-always {
@media (hover: hover) {
&:hover {
@slot;
}
}
@media not all and (hover: hover) {
& {
@slot;
}
}
}
@custom-variant mobile-hover (&:hover); [ Playground | Cross-test on mobile from desktop ] Sample usage: <!-- Button is always visible on mobile, otherwise shown when the parent card is hovered -->
<div class="card group">
...
<button class="opacity-0 group-hover-always:opacity-100">...</button>
</div> Answer for v3In v3, the standard The config below (which should be added to
import type { Config } from "tailwindcss"
import plugin from "tailwindcss/plugin"
export default {
// ...
plugins: [
plugin(({ addVariant, addUtilities, matchVariant }) => {
// Hover media queries
addVariant("has-hover", "@media (hover: hover) and (pointer: fine)")
addVariant("no-hover", "@media not all and (hover: hover) and (pointer: fine)")
// Applied on hover if supported, never applied otherwise
addVariant("hover-never", "@media (hover: hover) and (pointer: fine) { &:hover }")
matchVariant(
"group-hover-never",
(_, { modifier }) => `@media (hover: hover) and (pointer: fine) { :merge(.group${modifier ? "\\/" + modifier : ""}):hover & }`,
{ values: { DEFAULT: "" } },
)
matchVariant(
"peer-hover-never",
(_, { modifier }) => `@media (hover: hover) and (pointer: fine) { :merge(.peer${modifier ? "\\/" + modifier : ""}):hover & }`,
{ values: { DEFAULT: "" } },
)
// Applied on hover if supported, always applied otherwise
addVariant("hover-always", [
"@media (hover: hover) and (pointer: fine) { &:hover }",
"@media not all and (hover: hover) and (pointer: fine)",
])
matchVariant(
"group-hover-always",
(_, { modifier }) => [
`@media (hover: hover) and (pointer: fine) { :merge(.group${modifier ? "\\/" + modifier : ""}):hover & }`,
"@media not all and (hover: hover) and (pointer: fine)",
],
{ values: { DEFAULT: "" } },
)
matchVariant(
"peer-hover-always",
(_, { modifier }) => [
`@media (hover: hover) and (pointer: fine) { :merge(.peer${modifier ? "\\/" + modifier : ""}):hover & }`,
"@media not all and (hover: hover) and (pointer: fine)",
],
{ values: { DEFAULT: "" } },
)
}),
],
// ...
} satisfies Config |
Beta Was this translation helpful? Give feedback.
-
If I create a custom media query that uses https://caniuse.com/#feat=css-media-interaction to target devices that are not mobile, I can do something like
notmobile:hover:font-bold
to make sure the bold style only takes effect not on mobile devices.The problem is that I'd have to remember to always use
notmobile:hover:
everywhere I'm usinghover:
, which clutters the html and could lead to forgetting to use it in certain places.A feature where we could set
:hover
to only apply for a certain media query that applies globally would be useful. Often the hover styles cause issues on mobile browsers where they're better off not being there.Alternatively is there a way to write a media query that automatically appends another style to each style? So that when I create the
notmobile
media query I can tell it to generate all its styles appending:hover
to it. Then I only need to usenotmobile:font-bold
instead ofnotmobile:hover:font-bold
. This feature could be a better way of doing it than what I suggested before because this is more generic and could allow for interesting possibilities.Beta Was this translation helpful? Give feedback.
All reactions