1

I want to make a <div b on hover to affect <div> a. I have seen code for the reverse using (+)

#a:hover + #b {
    background: #ccc
}
<div id="a">Div A</div>
<div id="b">Div B</div>

But is there away to affect the opposite? Div B affecting Div A rather then A affecting B?

Hashem Qolami
  • 88,138
  • 25
  • 132
  • 151
Michael
  • 123
  • 2
  • 8
  • 1
    No...not yet. Perhaps in CSS Module 4 – Paulie_D Mar 19 '14 at 16:45
  • Sorry it got messed up. To clarify I want to make a div b on hover to affect div a. I have seen code for the reverse using (+) – Michael Mar 19 '14 at 16:45
  • 1
    In pure CSS, you can't. You could do some sort of hack using `:before` but that's rarely much of a consolation. JS is the only sure-fire way of achieving this. – Mitya Mar 19 '14 at 16:47
  • No just two div's next to each other. I have link and span with an arrow. I want the arrow to move when you hover over the link (which it does) but I would also like when you hover over the arrow to have the link color change as it does when you hover. – Michael Mar 19 '14 at 16:47
  • Utkanos - Ok I will look into it. – Michael Mar 19 '14 at 16:48
  • @HashemQolami sorry, we must have been editing at the same time. It happens. – Mitya Mar 19 '14 at 16:48
  • With pure CSS? No. With JavaScript? Yes. – j08691 Mar 19 '14 at 16:48
  • Similar [issue here](http://stackoverflow.com/questions/22510960/cant-change-background-color-of-another-element-on-hover/) which needs a *granduncle* selector :) – Hashem Qolami Mar 19 '14 at 16:57
  • 1
    CSS tricks can fake it , http://codepen.io/anon/pen/vtsnC/ not to use really :) – G-Cyrillus Mar 19 '14 at 17:01

2 Answers2

2

As has been mentioned, this isn't feasible as a CSS solution at this point in technology. You'd need to rely on Javascript.

Here is a jQuery solution for you:

$('#b').hover(
    function(){ $('#a').addClass('yellow') },
    function(){ $('#a').removeClass('yellow') }
)

JSFiddle

EnigmaRM
  • 7,207
  • 9
  • 41
  • 72
  • Thanks for this, but for some reason I cannot get it to work. Maybe because I am using a div class? This was close when I added a border element, but color did not work $(document).ready(function () { $(".research_arrow").hover(function () { $(".research_link").css("color:#000000"); }); $(".research_arrow").mouseleave(function () { $(".research_link").css("color:#ffffff"); }); }); – Michael Mar 19 '14 at 17:57
1

Selectors Level 4 draft defines the subject of a selector, which allows you to use

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

However, AFAIK browsers don't support it yet.

And also note it's a feature of the complete Selectors profile, so it isn't extremely performance sensitive.

Oriol
  • 225,583
  • 46
  • 371
  • 457