-1

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

CSS

.a {
    color: red;
}

.b {
    color: orange;
}

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

HTML

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

http://jsfiddle.net/2NEgt/323/

A.B
  • 17,478
  • 3
  • 28
  • 54
user3546854
  • 349
  • 1
  • 3
  • 12
  • 1
    possible duplicate of [Is there a CSS parent selector?](http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – Paulie_D Dec 04 '14 at 12:52

4 Answers4

5

Because .a is not descendent or comes after/inside of .b which is condition to work for it

for example if you inverse it, since .b is descendent of .a, it will work

.a:hover .b {
    background-color: blue;
}
A.B
  • 17,478
  • 3
  • 28
  • 54
1

You are trying to select .a when it's a child of .b in a hover state. This could never happen .a is the parent of .b.

Andrei Nemes
  • 2,913
  • 1
  • 13
  • 21
0

Actually you are trying to change background of parent on child hover this would not possible as it's parent of b if you change b's background color on hover of a then it will work

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

it's here http://jsfiddle.net/u7tYE/

nikita
  • 315
  • 3
  • 14
-2

There is an error in your HTML also .a is not descendent

HTML:

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

CSS:

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

}

RandomUser
  • 1,741
  • 7
  • 30
  • 60
  • There is no html error. It may be not the nicest way to write the html, but its ok to do it this way. – Der Vampyr Dec 04 '14 at 12:47
  • @DerVampyr Because of the structure the CSS didn't work correctly in the fiddle link that he mentioned in the question. I updated my answer. – RandomUser Dec 04 '14 at 12:49
  • 2
    He don´t want both single elements blue. He want to hover a child element of ``.a`` (in this case `.b`) and therefore make ``.a`` blue – Der Vampyr Dec 04 '14 at 12:56