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?
-
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](http://stackoverflow.com/questions/4502633/how-to-affect-other-elements-when-a-div-is-hovered/32470900#32470900). – Dan Dascalescu Sep 09 '15 at 04:32
4 Answers
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.
-
5it should be noted that the sibling selector (+) doesn't work correctly in ie8 and below. – Ben Rowe Aug 02 '11 at 09:52
-
-
@Brock Adams: Someone else inexplicably added the `javascript` tag: http://stackoverflow.com/posts/6910049/revisions – thirtydot Aug 02 '11 at 09:53
-
-
1Doesn't work in Chrome on Linux/Windows, works in FF{linux/winxp}, Opera{linux} and IE7/8{win xp}, I would have accepted your answer – Kumar Aug 02 '11 at 09:56
-
Thanks to all who answered. This was the closest to a no jquery solution. Thanks – Wesley Skeen Aug 05 '11 at 08:07
-
Great, its working for me as i had other div's between my target div's. Thank you very much. – N Khan Feb 25 '13 at 10:56
-
is there a way of affecting #a when #b is hovered over? I tried http://jsfiddle.net/u7tYE/1829/ but that does not work – Sam007 Jun 20 '13 at 17:31
-
6Interesting, thanks @thirtydot. Does either operator work on an immediately nested element? (such as ``) – Ian Campbell Jun 27 '13 at 19:15...
-
32
-
7
-
i have a doubt what ever style they include in the hover state i think it should apply for parent plus child.How come here it is applying for child alone. Can anybody help me? – sun Sep 16 '13 at 13:26
-
@sun: I'm not entirely sure what you're asking, but maybe this will help: http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector – thirtydot Sep 16 '13 at 13:29
-
if i understand correctly on hover of `a` tag you are selecting its preceding element so that styles are applied to the preceding elements. Am i right? @thirtydot – sun Sep 16 '13 at 13:41
-
-
-
@sun: What is the actual CSS selector you're asking about (for example `#a:hover + #b`)? There is no way in CSS to *select* the preceding element (or the parent element). `a + b` selects `b` if it *immediately follows* `a`. There is no way to do the reverse, select `a` if there is `b` *immediately following* it. – thirtydot Sep 16 '13 at 14:02
-
sorry its my english fault i shouldn't have stated precede there. ok is that possible to give style for body tag on hover of other element ofcourse it is parent of other div and a. But is there any bypass way there? @thirtydot – sun Sep 16 '13 at 14:13
-
-
i am trying to do the same thing but on target(to simulate on click event) and not on hover but it doesn't work. do you know why? – Dchris Apr 23 '14 at 17:21
-
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**`` and **B** `Div A`) I have set up a jsfiddle here: http://jsfiddle.net/kenhimself/mu86yj6f/ – kenhimself Nov 26 '14 at 05:26Div B
-
1@kenhimself: Unfortunately, there's no CSS based solution with HTML like that, the elements must be siblings. Time to use JavaScript. – thirtydot Nov 29 '14 at 10:12
-
Is it possible to effect the element before the hovered one? For example : `Div A` `Div B` Hovering on #b change #a. – Hanzallah Afgan Jun 08 '15 at 10:16
-
1just to note, it works with classes as well `.level:hover + .levelEnd { border-left: thin solid #808080; }` – Tcll Aug 08 '15 at 20:46
-
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](http://stackoverflow.com/questions/4502633/how-to-affect-other-elements-when-a-div-is-hovered/32470900#32470900). – Dan Dascalescu Sep 09 '15 at 04:31
-
I am trying to do this backwards too, so horvering #a affects #b, and hovering #b affects #a, although this seem to not work the same way... What should I do here? – Albert MN. Sep 24 '15 at 17:41
-
This solution is great but sucks at the same time! I want B to affect A without repositioning everything in CSS. It seems "adjacent" siblings are not really "adjacent" if you can only affect the second element with the first. Seems like B is really a sibling of A no matter what based on the DOM. maybe JS is the best solution for me. – MilkyTech Jul 06 '18 at 00:05
-
Awesome, Didn't know this cool stuff before. Was overloading my page with JQuery alternatives till now. Thanks again. – Ananda Prasad Bandaru May 24 '19 at 14:37
-
1How do I do the opposite of `#a:hover #b`? So when I hover over the child, the parent gets styled. – mekb Aug 31 '19 at 01:11
-
1@facepalm42: In general, that's [not possible](https://stackoverflow.com/q/1014861/405015). – thirtydot Aug 31 '19 at 14:20
-
it seems like it doesn't work if the hoverable element is inside a
inside a table, so weird – Barbz_YHOOL Apr 18 '20 at 18:23 -
I've been here a million times already, and if I could upvote this every time I came here, I would. – May 30 '21 at 07:08
-
Salute to this answer. Worked like a charm!!! I'll give you million upvotes!! – Exis Jan 13 '23 at 11:49
-
This is a cheat sheet I always use when it comes to very precise css selection https://www.w3schools.com/cssref/trysel.php? Works liek acharm and you can see what operators to use to target a specific element you may have – oluwatyson Jan 27 '23 at 09:11
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', '');
});
});

- 28,406
- 6
- 55
- 75
-
11Yes it can, albeit only under relatively limited situations. But this can certainly be done with CSS. – David Thomas Aug 02 '11 at 09: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 Aug 02 '11 at 09:56
-
1Using 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](http://stackoverflow.com/questions/4502633/how-to-affect-other-elements-when-a-div-is-hovered/32470900#32470900). – Dan Dascalescu Sep 09 '15 at 04:31
-
1
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/

- 443
- 3
- 15
-
1You don't even need onmouseover. See my [pure-CSS rating widget](http://stackoverflow.com/questions/4502633/how-to-affect-other-elements-when-a-div-is-hovered/32470900#32470900). – Dan Dascalescu Sep 09 '15 at 04:30
-
2
-
1
-
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 Nov 16 '21 at 12:35
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");
});
});
-
30
-
1
-
7Then 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. – David Thomas Aug 02 '11 at 09:50
-
jQuery is a extension of javascript. The user never specifically asked for no jQuery solution. – Curtis Aug 02 '11 at 09:50
-
jQuery is most adequate as most people know it, though you can use some other toolkit as well – Kumar Aug 02 '11 at 09:50
-
14He also didn't specifically ask for a 'no-[tag:python]' solution, but given that the question was tagged with *only* '[tag:css]', it seems fair to assume that he wanted a css solution. – David Thomas Aug 02 '11 at 09:55
-
Fair point @David Thomas, at the time of posting this answer, javascript was a tag, so i assumed a javascript solution would be acceptable – Curtis Aug 02 '11 at 09:59
-
Ah, yeah. I saw that tag was added (though I hadn't seen it until after I posted my first comment). I was about to retract my initial comment when I realised that it'd been removed in another edit (since it wasn't the OP that added the [tag:javascript] tag). Sorry if I seem unduly...uh... *contentious*, it's not intended as hostility. Also, it's worth pointing out that the down-vote wasn't mine, I accept your answer is useful, it's just not what was asked-for, I think. – David Thomas Aug 02 '11 at 10:02
-
If I had come to this question with only a CSS tag I wouldn't have posted a javascript/jquery solution, as the user may be after a solution which works with or without javascript. No problem, you don't seem hostile, its good to see people are re-enforcing that jquery shouldn't just be used as a quick solution when its not even asked for! – Curtis Aug 02 '11 at 10:05
-
-
while a JS/JQuery solution IS helpful, I too agree that `CSS:hover` and `JS.mouseover()` are 2 completely different things. – Tcll Aug 08 '15 at 20:51
-
Javascript is less performant than CSS, therefore css is a more desirable solution in a professional project. – Shawn J. Molloy Jul 05 '18 at 18:38