When using iOS Safari 11–17, I encountered a strange behavior where a hover effect is applied to a link underneath a <div>
element that has been removed from the DOM. This happens when the <div>
is positioned over the link and is then clicked to disappear.
An animated GIF illustrating this issue can be seen below:
To demonstrate the problem, I added a transparent background to the button so that the link behind it becomes visible. When clicking on the button not directly above the link, the link (labeled as Some link
) remains blue instead of changing to its red hover state.
However, if I click on the <div>
directly above the link, causing it to be removed, the link does receive the hover effect after the action.
Interestingly, this issue does not occur on Android Chrome, as shown in the example below:
Additionally, I have included the sample HTML/CSS/JS code used in this setup, which is relatively straightforward.
I would prefer it if iOS behaved like Android Chrome in this scenario: clicking on an element that is immediately removed should not trigger a hover state change for an element positioned right behind it.
var button = document.querySelector(".button");
button.addEventListener("click", function() {
button.parentNode.removeChild(button);
});
var link = document.querySelector('a');
link.addEventListener("click", function() {
window.alert('clicked!');
});
a:link { color: blue }
a:visited { color: blue }
a:hover { color: red }
a:active { color: green }
.button {
background: rgba(100, 0, 0, .4);
position: absolute;
top: 2px;
left: 2px;
z-index: 1;
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
border-radius: 5px;
}
.button:hover {
background: rgba(0, 100, 0, .4);
cursor: pointer;
}
<a href="#">Some link</a>
<div class="button">Click me!</div>