1

How can I change element CSS by hovering an another element in CSS?

We use this:

.example .example2 li:hover .element

If we have:

<div class='example'><div class='example2'><li></li></div></div>
<div class='element'></div>

But if we have:

<div class='example'><div class='example2'><li></li></div></div>
<div class='exemple2'><div class='element'></div></div>

we could not use .example .example2 li:hover .exemple2 .element

Is there any method to put the parent element .exemple2 to select his child .element in the hover?

Soufiane Douimia
  • 401
  • 3
  • 10

3 Answers3

0

You can use

.exemple:hover > .exemple2 .element
baao
  • 62,535
  • 14
  • 113
  • 168
0

I think what you want is the adjacent sibling combinator.

https://css-tricks.com/almanac/selectors/a/adjacent-sibling/

Craig Stroman
  • 165
  • 2
  • 5
0

You need to use the + selector, like:

.exemple:hover + .exemple2 .element{
    color: red;
}

DEMO

lmgonzalves
  • 6,331
  • 3
  • 18
  • 39
  • i was thinking that is the same .example . example1 li:hover + .example2 .element this doesn't work li:hover + .example2 .element this work – Soufiane Douimia Jul 13 '15 at 00:40
  • @Imgonzalves this is my demo https://jsfiddle.net/cz6L7ctu/1/ – Soufiane Douimia Jul 13 '15 at 00:56
  • Oh I see, then it's not possible with CSS3, see [this question](http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector). And please delete the comments to keep it clean. – lmgonzalves Jul 13 '15 at 01:29