How to disable a CSS :hover effect?
Last Updated :
16 Sep, 2024
Disabling a CSS :hover effect means preventing the visual changes applied when an element is hovered over by the cursor. This can be done by overriding the :hover styles with none or applying the effect conditionally using JavaScript or specific classes to control when the hover state is active.
Here are some common approaches :
Approach 1: Using JQuery by .removeClass() method
Using the jQuery .removeClass() method disables a CSS :hover effect by removing the class that applies the hover styling to an element. This approach provides dynamic control, allowing hover effects to be enabled or disabled based on specific interactions or conditions.
Example 1: In this example, we are using jQuery to disable a CSS :hover effect on a div. When the button is clicked, removeClass('hover') is called, removing the hover effect and updating the text.
html
<!DOCTYPE html>
<html>
<head>
<title>Disable Hover Effect Using jQuery</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
.element {
height: 100px;
width: 200px;
background: green;
color: white;
text-align: center;
line-height: 100px;
}
.hover:hover {
background: blue;
}
</style>
</head>
<body>
<h1 id="h1" style="color:green;">GeeksforGeeks</h1>
<p id="GFG_UP"></p>
<div id="div" class="element hover">
Hover It
</div>
<br>
<button onclick="gfg_Run()">Click here</button>
<p id="GFG_DOWN"></p>
<script>
let el_up = document.getElementById("GFG_UP");
let el_down = document.getElementById("GFG_DOWN");
let heading = document.getElementById("h1");
let div = document.getElementById("div");
el_up.innerHTML =
"Click on the button to remove the CSS :hover effect.";
function gfg_Run() {
$('#div').removeClass('hover');
el_down.innerHTML = "Hover effect Removed";
}
</script>
</body>
</html>
Output:
How to disable a CSS :hover effect?Approach 2: Using JavaScript .classList.remove() method
The JavaScrit .classList.remove() method in JavaScript disables a CSS :hover effect by removing the class that applies the hover styles. This approach allows dynamic control over styling, letting you toggle hover effects on or off by manipulating the class list directly.
Example : In this example we use JavaScript's .classList.remove() method to disable a CSS :hover effect on a <div>. Clicking the button removes the hover class, preventing the background color change.
html
<!DOCTYPE html>
<html>
<head>
<title>Disable Hover Effect Using JavaScript</title>
<style>
.element {
height: 100px;
width: 200px;
background: green;
color: white;
text-align: center;
line-height: 100px;
}
.hover:hover {
background: blue;
}
</style>
</head>
<body>
<h1 id="h1" style="color:green;">GeeksforGeeks</h1>
<p id="GFG_UP"></p>
<div id="div" class="element hover">
Hover It
</div>
<br>
<button onclick="gfg_Run()">Click here</button>
<p id="GFG_DOWN"></p>
<script>
let el_up = document.getElementById("GFG_UP");
let el_down = document.getElementById("GFG_DOWN");
let heading = document.getElementById("h1");
let div = document.getElementById("div");
el_up.innerHTML =
"Click on the button to remove the CSS :hover effect.";
function gfg_Run() {
document.getElementById('div').classList.remove("hover");
el_down.innerHTML = "Hover effect Removed";
}
</script>
</body>
</html>
Output:
How to disable a CSS :hover effect?
Similar Reads
How to apply Hover Effect in CSS? The :hover pseudo-class in CSS is used for styling when an element is hovered over by a cursor. Syntaxa:hover { // CSS Styles...}HTML<!DOCTYPE html> <html> <head> <style> .hover-button { background-color: #4CAF50; padding: 15px 30px; font-size: 16px; cursor: pointer; } .hover
1 min read
How to Disable Everything on Hover in ChartJS? Disabling all hover interactions in Chart.js involves configuring the chart to ignore hover events and disabling tooltips and legends. This makes sure that no visual or interactive elements respond when a user hovers over the chart. By adjusting various options and event handlers, we can turn off ho
3 min read
How to create icon hover effect using CSS ? To style an icon's color, size, and shadow using CSS, use the color property to set the icon's color, font size to adjust its size, and text-shadow or box-shadow for adding shadow effects, creating a visually appealing and dynamic look.Using: hover Pseudo-ClassUsing the: hover pseudo-class, you can
2 min read
How to Display Element on Hover using CSS ? In web development, displaying an element on hover is a common interaction pattern used to reveal additional content or actions. Below are the various CSS approaches to achieve this effect: Table of Content Using the display propertyUsing the visibility propertyUsing the display propertyThis approac
2 min read
How to disable a link using only CSS? To disable a link using CSS, pointer-events can be used, which sets whether the element in the page has to respond or not while clicking on elements. The pointer-events property is used to specify whether the element should respond to pointer events or not. The following example illustrates this app
3 min read