Remove ':hover' CSS behavior from element
To disable the :hover
effect, you need to reapply the default styles of the element during the :hover
state. In other words, you counteract the hover styles. Akin to a "Ctrl+Z" operation for styles!
The element remains Stoic - unchanged on hover.
Extending the removal of hover
Sometimes, just countering the hover effect may not sail your boat. You need something more rigid, unyielding, pretty much like your :hover
. Here are a few strategies:
-
Pointer Events: You can play 'statue' with the element by using
pointer-events: none;
. The element won't budge on hover or click: -
Disabled Class: Establish an 'Immunity Passport' for elements by defining a
.disabled class
. Use it wherever you want hover freedom: -
Selective Hover Removal: Want to play favorites with your elements? Salute the
:not()
pseudo-class! It helps you choose who gets the hover: -
Aesthetic Disabled Effect: Silence isn't always evident. For those scenarios, create a blend of
pointer-events: none;
for the back-end andopacity: 0.2;
for the front-end to create a 'disabled' visual impression. -
Targeted Overrides: If you need to rephrase a specific hover effect, narrow down your rules. They should be so specific they could find a needle in a haystack.
Taking the steer of hover effects
"Hover-cover" approach
Think of the :hover
as a blanket over an element. To remove it doesn't mean burning the blanket. Instead, just add another blanket (element) on top. Make sure your new blanket is as chilled out as summertime - pointer-events: none
, and for good measure, make it cozy and a bit transparent (opacity: 0.5
).
JavaScript - When style needs a bit of dynamism
For a more dynamic scenario, where CSS monologues seem insufficient, make it an interactive play with JavaScript. Add and remove classes controlling hover states on the go:
And the corresponding CSS class could be something like:
Nifty practical examples
To truly harness the power of these pointers, why just read, when you can DO and LEARN? Get your hands dirty with some practical examples, such as JSFiddle, in the references section.
Was this article helpful?