CSS hover on one element to affect another (sibling or child)

This is possible to trigger css rules for an hover on one element that will affect another.
One example for this is hovering over a parent Div and changing background color for one of its inner elements.

This is possible only if #b is after #a in the HTML:
If #b comes immediately after #a:

adjacent sibling combinator (+).

If there are other elements between #a and #b :

general sibling combinator (~).

Both + and ~ work in all modern browsers.

If #b is a descendant of #a, you can simply use #a:hover #b.

For elements which have no sibling / child relations a JavaScript based solution is required.

Leave a Reply

Your email address will not be published. Required fields are marked *