Here's the code I have:
<div class="hoverDiv">Hover</div>
<div>
<div class="hoverDivShow"></div>
</div>
.hoverDivShow {
display: none;
}
.hoverDiv:hover ~ .hoverDivShow {
display: block;
}
What I've been trying to do, is make hoverDivShow appear when hoverDiv is hovered over.
The HTML won't be able to change.
I've looked elsewhere, and have been unable to find any solutions. Not really sure what I'm doing right/wrong. Help would be nice.
Thanks!
To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.
The adjacent sibling combinator ( + ) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element .
Set visibility: hidden and opacity: 0 for not displaying image first time. Using transition property for adding animation. When hovering on parent <div> then change the child element visibility: visible and opacity: 0.7; . You can change the position of text container using the top bottom left right CSS properties.
Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.
You need to use .hoverDiv:hover ~ div .hoverDivShow
as the "niece" selector. The idea is to select a <div>
that is the sibling of .hoverDiv
, then select a child of that sibling that has the class hoverDivShow
.
Here's a working demo:
.hoverDivShow {
display: none;
}
.hoverDiv:hover ~ div .hoverDivShow {
display: block;
}
<div class="hoverDiv">Hover</div>
<div>
<div class="hoverDivShow">I'm hidden until you hover!</div>
</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