443

When I hover over a div or class with an id of "a", can I get the background color of a div or class with the id of "b" to change?

1
  • 1
    Using flexbox, you can hover other elements even if they (appear to) be placed before the hovered one in the DOM. See my pure-CSS rating widget. Commented Sep 9, 2015 at 4:32

4 Answers 4

737

Yes, you can do that, but only if #b is after #a in the HTML.

If #b comes immediately after #a: http://jsfiddle.net/u7tYE/

#a:hover + #b {
    background: #ccc
}

<div id="a">Div A</div>
<div id="b">Div B</div>

That's using the adjacent sibling combinator (+).

If there are other elements between #a and #b, you can use this: http://jsfiddle.net/u7tYE/1/

#a:hover ~ #b {
    background: #ccc
}

<div id="a">Div A</div>
<div>random other elements</div>
<div>random other elements</div>
<div>random other elements</div>
<div id="b">Div B</div>

That's using the general sibling combinator (~).

Both + and ~ work in all modern browsers and IE7+

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

ALTERNATIVE: You can use pure CSS to do this by positioning the second element before the first. The first div is first in markup, but positioned to the right or below the second. It will work as if it were a previous sibling.

34
  • 5
    it should be noted that the sibling selector (+) doesn't work correctly in ie8 and below.
    – Ben Rowe
    Commented Aug 2, 2011 at 9:52
  • 6
    Interesting, thanks @thirtydot. Does either operator work on an immediately nested element? (such as <div id = "asdf"><div id = "affect_this_element?">...</div></div>) Commented Jun 27, 2013 at 19:15
  • 33
    @IanCampbell: Like #asdf:hover > #affect_this_element?
    – thirtydot
    Commented Jun 27, 2013 at 20:01
  • 7
    Cool, the child selector > works, thanks @thirtydot! Commented Jun 27, 2013 at 20:04
  • 3
    @thirtydot -- I'm wondering, when both #a and #b is within a separate wrapper div, the hover over effect does not work. Any CSS-based solution that can somehow get this to work? (An example might be something like: A**<div class="wrapper01"><div id="a">Div A</div></div> and **B <div class="wrapper02"><div id="b">Div B</div></div>) I have set up a jsfiddle here: jsfiddle.net/kenhimself/mu86yj6f
    – kenhimself
    Commented Nov 26, 2014 at 5:26
37

This can not be done purely with css. This is a behaviour, which affects the styling of the page.

With jquery you can quickly implement the behavior from your question:

$(function() {
  $('#a').hover(function() {
    $('#b').css('background-color', 'yellow');
  }, function() {
    // on mouseout, reset the background colour
    $('#b').css('background-color', '');
  });
});
4
  • 11
    Yes it can, albeit only under relatively limited situations. But this can certainly be done with CSS. Commented Aug 2, 2011 at 9:53
  • 5
    @David yes only in limited situations where #b is a child or sibling of #A, etc. The OP didn't specify the relationship between the two, so I assumed this wasn't the case.
    – Ben Rowe
    Commented Aug 2, 2011 at 9:56
  • 2
    Using flexbox, you can hover other elements even if they (appear to) be placed before the hovered one in the DOM. See my pure-CSS rating widget. Commented Sep 9, 2015 at 4:31
  • 1
    Bumped for including mouseout reset
    – moodboom
    Commented Apr 1, 2016 at 19:03
18

A pure solution without jQuery:

Javascript (Head)

function chbg(color) {
    document.getElementById('b').style.backgroundColor = color;
}   

HTML (Body)

<div id="a" onmouseover="chbg('red')" onmouseout="chbg('white')">This is element a</div>
<div id="b">This is element b</div>

JSFiddle: http://jsfiddle.net/YShs2/

4
  • 1
    You don't even need onmouseover. See my pure-CSS rating widget. Commented Sep 9, 2015 at 4:30
  • 2
    @DanDascalescu this is what I was looking for, thanks! Commented Oct 10, 2019 at 17:55
  • 1
    How to use that on SCSS?
    – Adam
    Commented Dec 16, 2019 at 9:28
  • This is the only solution here that worked for me. I was even able to tweak the function a bit to allow for the target element to be a parameter as well. Thank you so much @kongr45gpen!
    – Catlover
    Commented Nov 16, 2021 at 12:35
10

The following example is based on jQuery but it can be achieved using any JS tool kit or even plain old JS

$(document).ready(function(){
     $("#a").mouseover(function(){
         $("#b").css("background-color", "red");
     });
});
12
  • 31
    css not jquery (or javascript). Commented Aug 2, 2011 at 9:45
  • 1
    jquery is the most adequate solution here. Commented Aug 2, 2011 at 9:47
  • 7
    Then at the very least offer an explanation of why css is inadequate, and what the other, more suitable, options are. I don't think that just throwing a jQuery solution into the question is particularly useful. Commented Aug 2, 2011 at 9:50
  • jQuery is a extension of javascript. The user never specifically asked for no jQuery solution.
    – Curtis
    Commented Aug 2, 2011 at 9:50
  • 14
    He also didn't specifically ask for a 'no-python' solution, but given that the question was tagged with only 'css', it seems fair to assume that he wanted a css solution. Commented Aug 2, 2011 at 9:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.