I've been trying to create elements which will change position slightly when hovered - in this example we have a primitive attempt at a 3D button which is slightly depressed when hovered over. All works as intended, unless the top and left 3 pixels are hovered over, in which case it rapidly flickers between the two states.
Is it possible, without creating a duplicate transparent element, to make the hover area not shift with the element and eliminate this behaviour? JavaScript and JQuery solutions are okay but pure HTML/CSS would be best.
#button {
width: 100px;
height: 100px;
background-color: #00f;
box-shadow: 10px 10px 3px #aaa;
}
#button:hover {
transform: translate(3px, 3px);
box-shadow: 7px 7px 2.1px #888;
}
<div id="button">
</div>
JSFiddle link
You can't permanently stay as hover state using CSS as it merely defines the rules on styling the tags, classes, IDs, pseudo-classes, and states. So unfortunately, you need Javascript to solve the problem.
You can use Jquery to set a class when the mouse is hovered. Then the class will remain set even after the mouse moves away.
The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.
If you have two elements in your HTML and you want to :hover over one and target a style change in the other the two elements must be directly related--parents, children or siblings. This means that the two elements either must be one inside the other or must both be contained within the same larger element.
You could create a pseudo element :after
and style this instead:
#button {
width: 100px;
height: 100px;
}
#button:after {
display: block;
width: 100%;
height: 100%;
background-color: #00f;
box-shadow: 10px 10px 3px #aaa;
content: "";
}
#button:hover:after {
transform: translate(3px, 3px);
box-shadow: 7px 7px 2.1px #888;
}
<div id="button">
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With