Why CSS :hover Doesn’t Work and How to Fix It
If a button or link doesn't change color when you hover over it, there are two possible causes. Either the element never enters the :hover state, or it does enter that state but a rule you wrote loses to another rule. A navigation dropdown that closes while you move the mouse downward is one example of the first case.
The test environment was Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. I opened a page that used only HTML and CSS in Playwright, moved the mouse to the center of each element, and read the values from getComputedStyle() and the result of matches(':hover'). For smartphones, I only tried Playwright's Pixel 7 emulation; I did not use a real device. I did not test Firefox or Safari.
- 1. Telling the two causes apart in the browser
- 2. The mouse isn't reaching the element
- 3. The element is in :hover, but the color doesn't change
- 4. Changing an element that comes before the hovered one
- 5. A dropdown menu closes while the mouse is moving toward it
- 6. On smartphones, :hover remains after a tap
Telling the two causes apart in the browser
To check whether an element is in the :hover state, keep the mouse over it and run el.matches(':hover') in the browser console.
const el = document.querySelector('.btn');
el.matches(':hover'); // true if the mouse is over it
getComputedStyle(el).backgroundColor; // the color actually in use
If the result is false, the mouse isn't reaching the element, for example because another element is covering it. If it is true but the color still doesn't change, the problem is in how the CSS is written. The causes I tested fall into these two groups as follows.
| Cause | matches(':hover') |
Color |
|---|---|---|
| A transparent element is on top | false |
Doesn't change |
The element has pointer-events: none |
false |
Doesn't change |
| An ID selector wins on specificity | true |
Doesn't change |
A rule with the same specificity comes after :hover |
true |
Doesn't change |
.btn :hover has a space before :hover |
true |
Doesn't change |
| A selector list contains an unknown pseudo-class | true |
Doesn't change |
In Chrome DevTools, you can force an element into the :hover state without moving the mouse, and then see in the Styles pane which rules are being overridden. I covered the steps in a separate article, "Using Chrome DevTools to Force :hover and Inspect CSS."
The mouse isn't reaching the element
A transparent element is on top
I placed a transparent <div> with position: absolute over a button. Nothing looked different, but when I called document.elementFromPoint() at the center of the button, it returned the overlaying <div>. The button's matches(':hover') was false, and its background stayed gray.
When I added pointer-events: none to the overlaying element, the mouse reached the button underneath, and the background changed to pink.
.cover {
position: absolute;
inset: 0;
pointer-events: none; /* let the mouse pass through to the element below */
}
As described in MDN's explanation of pointer-events, an element with none is not a target for pointer events.
The same thing happened when the overlapping element was a pseudo-element. I stretched a ::after on a parent element that wraps the button with inset: 0. elementFromPoint() returned the parent, and the button did not enter :hover. When I added pointer-events: none to the ::after, the button turned pink. I tested the technique of letting clicks pass through an overlapping layer in more detail in a separate article, "How to Pass Clicks Through to Elements Below with CSS pointer-events."
The element itself has pointer-events: none
Conversely, when I added pointer-events: none to the button itself, it never entered :hover. elementFromPoint() at its center returned the <body> behind it, not the button. If you added pointer-events: none to a link or button to make it temporarily unclickable, check that you haven't forgotten to remove it.
Also, in Chromium, a <button> with disabled did enter :hover, and its background turned pink. If you don't want a disabled button's color to change, writing .btn:hover:not(:disabled) kept the background gray.
The element is in :hover, but the color doesn't change
An ID selector wins
With the CSS below, the link's color did not change when hovered. matches(':hover') returned true.
#menu a { color: #1f2a30; }
.link:hover { color: #d85878; } /* doesn't apply */
#menu a contains an ID selector. As described in MDN's explanation of specificity, a selector with more IDs wins regardless of the rest of the selector. .link:hover has one class and one pseudo-class, so it loses to #menu a, which has one ID.
I tried two fixes, and both turned the link pink.
/* 1. Add the same ID to the :hover rule */
#menu a:hover { color: #d85878; }
/* 2. Lower the specificity of the base rule with :where() */
:where(#menu) a { color: #1f2a30; }
.link:hover { color: #d85878; }
As described in MDN's explanation of :where(), the specificity inside :where() is always 0. :where(#menu) a counts only the element name a, so .link:hover wins.
A rule with the same strength comes later
.btn:hover and .btn.primary both have a class or pseudo-class count of two, so their specificity is the same. With them written in this order, the background stayed light blue even when hovered.
.btn:hover { background: #d85878; }
.btn.primary { background: #00c8e8; } /* the later rule wins */
MDN's specificity explanation says that when specificity is equal, the style declared last takes precedence. When I moved .btn:hover after .btn.primary, the background turned pink. The same reasoning applies to link states: :link, :visited, :hover, and :active. MDN's :hover explanation recommends writing them in this order. I tested link colors in more detail in a separate article, "Changing the Color of Visited Links with CSS :visited."
A space before :hover
Putting a space between selectors, as in .btn :hover, means "an element under .btn that the mouse is over." For a button with no child elements, the background stayed gray.
When the button had a <span> inside, it was the <span>'s background that changed, not the button's. Only the text turned pink, and the padding around it stayed gray. To change the whole button, write .btn:hover without a space.
A selector list contains an unknown pseudo-class
If any selector in a comma-separated list is unknown to the browser, the entire rule is discarded. When I wrote the unknown pseudo-class :-moz-focusring alongside :hover, the background did not change in Chromium.
/* In Chromium, this whole rule is not loaded */
.btn:hover, .btn:-moz-focusring { background: #d85878; }
When I checked the loaded rules in document.styleSheets, this rule was missing. MDN's explanation of selector lists also notes that if a selector is invalid, the whole style block is ignored.
Splitting the rule into two, or wrapping the list in :is(), turned the button pink. Inside :is(), only the unknown part is dropped. In Chromium, the rule that was loaded became .btn:is(:hover).
.btn:is(:hover, :-moz-focusring) { background: #d85878; }
Changing an element that comes before the hovered one
I tried to change the color of a description text placed before a button when the button is hovered, using .btn:hover ~ .hint, but the color didn't change. As described in MDN's explanation of the subsequent-sibling combinator, ~ only reaches siblings that come after the matched element.
<div class="row">
<span class="hint">説明</span>
<button class="btn">?</button>
</div>
Adding :has() to the parent and selecting .hint inside the parent that contains the hovered button turned the description text pink. MDN's explanation of :has() also presents :has() as a way to select a parent element or a preceding sibling.
.row:has(.btn:hover) .hint { color: #d85878; }
A dropdown menu closes while the mouse is moving toward it
In a design where the parent .nav:hover sets the menu to display: block, if there is a gap between the parent link and the menu, the menu closes as soon as the mouse enters the gap.

I placed the menu 12px away with top: calc(100% + 12px), and moved the mouse downward from the center of the parent link in 4px steps. The bottom edge of the parent link was 80px from the top of the page. The menu stayed open up to 76px, closed at 80px, and stayed closed even when the mouse reached the menu item at 112px. With the menu still open, I called elementFromPoint() at 84px, in the gap, and it returned <body>. The gap belongs to neither .nav nor the menu, so when the mouse is there, .nav:hover is lost.
When I placed the menu directly below the parent link with top: 100% and made the visual gap with padding-top: 12px on the menu, the mouse reached the item at 104px without the menu ever closing. elementFromPoint() at 84px returned the menu's <ul>, which shows that the space is inside the menu's box.
.nav { position: relative; }
.menu {
display: none;
position: absolute;
top: 100%; /* place it directly below the parent */
padding-top: 12px; /* make the visual gap with padding */
}
.nav:hover .menu { display: block; }
When I kept top: 100% and shifted the menu with margin-top: 12px, it closed at 80px and didn't reach the item, the same as with top set to 12px away. elementFromPoint() at 84px returned <body>, because the margin area is not part of the menu's box.
On smartphones, :hover remains after a tap
In Pixel 7 emulation, after tapping a button, matches(':hover') was still true 0.5 seconds later, and the background stayed pink. Tapping somewhere else turned it gray again. MDN's :hover explanation also notes that on touchscreens, depending on the browser, :hover may never match, or it may keep matching until another element is touched.
In this state, the media query (hover: hover) was false and (hover: none) was true. On a desktop setup with a mouse, the results are reversed. The hover media feature describes the primary input mechanism (MDN calls it the "primary input mechanism") and tells you whether the user can easily hover over elements (MDN's explanation of hover).
@media (hover: hover) {
.btn:hover { background: #d85878; }
}
When I put the :hover rule inside this query, the background stayed gray after tapping in the Pixel 7 emulation. matches(':hover') was true, but the rule itself was not applied. When I tried the same CSS with a desktop setup that uses a mouse, the color changed to pink on hover. On touch devices, don't rely on :hover; make sure the element looks tappable on its own. I tested using :hover to lift a card in a separate article, "Adding a Hover Lift Effect to Cards with CSS."





