Sven
Sven

Reputation: 13296

CSS :hover on other element?

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/

Upvotes: 10

Views: 50966

Answers (9)

Ana
Ana

Reputation: 37178

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/

Upvotes: 45

Ivan Carrasco Quiroz
Ivan Carrasco Quiroz

Reputation: 615

Jquery is a good and easy solution:

html:

<div class="a">AAA</div>
<div class="b">BBB</div>

script: Put this script into your html if you want. That's all.

<script>
      $(".a").mouseover(function(){
        $(".b").css("color", "blue"); 
      });
      $(".a").mouseleave(function(){
        $(".b").css("color", "red");
      });
</script>

Upvotes: 0

zdifahk
zdifahk

Reputation: 11

try to understanding this example:
html code

<p>Hover over 1 and 3 gets styled.</p>
<div id="one" class="box">1</div>
<div id="two" class="box">2</div>
<div id="three" class="box">3</div>


<!--css-->
#one:hover ~ #three{
background-color: black;
color: white;
}
.box {
cursor: pointer;
display: inline-block;
height: 30px;
line-height: 30px;
margin: 5px;
outline: 1px solid black;
text-align: center;
width: 30px;
}

when you hover on the box 1 than the box 3 will get black color

Upvotes: 0

Jared
Jared

Reputation: 12524

There are two things you can do.

Either change your HTML to make .b a child of .a

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

OR

Change your css to use the adjacent selector

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

Upvotes: 2

Nicholas King
Nicholas King

Reputation: 938

no js needed http://jsfiddle.net/2NEgt/3/

Upvotes: 1

Stano
Stano

Reputation: 8949

You can use + selector

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

to apply the css for sibling element, or

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

for nested class.

Upvotes: 6

Josh
Josh

Reputation: 3475

Can you not do something like a:hover + b? see http://meyerweb.com/eric/articles/webrev/200007a.html

Upvotes: 6

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 175098

You shouldn't change a sibling's style when an event occurs on a different element. It's out of the context of CSS.

Use JavaScript to achieve this, for example:

var wrap = document.getElementById("wrap");
var aDiv = wrap.getElementsByClassName("a")[0];
var bDiv = wrap.getElementsByClassName("b")[0];
aDiv.onmouseover = function() {
    bDiv.style.backgroundColor = "red";
};
aDiv.onmouseout = function() {
    bDiv.style.backgroundColor = "white";
};

Upvotes: 0

Tallboy
Tallboy

Reputation: 13467

because .b isn't a child of .a, so that selector isn't finding anything. Use javascript to do what you want to do there.

Upvotes: 3

Related Questions