The problem is that it is applying the same rule to two independent selectors, using the "," no relation is created between the selectors.
Using the brother selector (General Sibling Selector) "~" you can apply the rule of hover
on the brother whose selector is on the right. Thus the rule is:
#div1:hover ~ #div2 {
background-color: white;
}
Reading the rule would be:
When mouse over element with id div1
, select all brothers whose id is div2
and apply the rule below.
Take a look at this Jsfiddle to see the code working. In case I put the black background to have contrast.
There is a variation, the direct brother selector "+" that only picks up the subsequent siblings and antecedent to the elements of the left selector, can be useful as well.
In addition to these, there is also the direct child selector (Child Selector) ">", which can also be used, but not for this specific case. And the descending selector (Descendant Selector) " (space), which is more comprehensive than the Child selector.
For more details of a look at:
- https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors
- https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors
- https://developer.mozilla.org/en-US/docs/Web/CSS/Child_selectors
- https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_selectors
You want the #div2 background to change when you hover over #div1 (returning to normal on exit)?
– Wakim
@Wakim, exactly.
– ptkato
I deleted my answer, I didn’t mean the question, I think @Wakim already answered what you needed ^^
– Alan PS