I want to hover any of the div that have the id
of symbol and change the color of #bg
, how do i do it?
<div id="container">
<div id="symbol1"></div>
<div id="symbol2"></div>
<div id="symbol3"></div>
<div id="symbol4"></div>
</div>
<div id="bg"></div>
I tried to use this but it didn’t work. I also used +
and >
and just a space too.
#symbol3:hover ~ #bg{
background-color:red;
}
but it didn’t work
2
Answers
The issue with the code is that which disinfor has stated.
If you move the
#bg
div to be outside but prior to the#container
div then you can use the below.The
:has()
selector will select a#bg
element that is followed by a#container
element of which has adiv#symbol3
being hovered.If you can not change the structure of the HTML, I believe you would to use some sort of JS.
A quick way to do this is to combine the
:has()
selector and the target attribute selector[attribute*="value"]
which selects any element that has a substringvalue
in itsattribute
.#container:has([id*="symbol"]:hover) + #bg
means to check for#container
that has anid
with a value containingsymbol
while it is being hovered on, then to select the#bg
placed immediately after it.Read more on CSS selectors: w3schools CSS Selectors Reference