Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :hover on other element?

Tags:

css

hover

Short question: Why does the background-color of .b does not change when I hover? .a?

CSS

.a {
    color: red;
}

.b {
    color: orange;
}

.a:hover .b {
    background-color: blue;
}

HTML

<div id="wrap">
    <div class="a">AAAA</div>
    <div class ="b">BBBB</div>
</div>

http://jsfiddle.net/2NEgt/

like image 635
Sven Avatar asked Jul 16 '12 15:07

Sven


1 Answers

You need to have .a:hover + .b instead of .a:hover .b

.a:hover .b would work for a structure like

<div class="a">AAAA
  <div class ="b">BBBB</div>
</div>

If at some point you'll need to have some elements between .a and .b, then you'll need to use .a:hover ~ .b, which works for all siblings of .a coming after it, not just the next one.

Demo http://jsfiddle.net/thebabydino/EajKf/

like image 158
Ana Avatar answered Nov 14 '22 05:11

Ana