2

i have a code like below:

<nav id="mainNav" class="">
   <div id="navContainer">
       <div id="active">
           [ some list ]
       </div><!-- #active -->
   </div><!-- #navContainer -->
</nav><!-- #mainNav -->

i want to change the background color of #mainNav when user hover on #active, but i dont know what to do. i did this and no results:

#active:hover #mainNav{
   background-color: #333;
}

any idea?

Geeky Guy
  • 8,658
  • 4
  • 38
  • 60
bobsilon
  • 1,568
  • 4
  • 15
  • 30

5 Answers5

3

Use JS or try a different approach, CSS ONLY:

http://jsfiddle.net/coma/zxybD/5/

HTML

<div id="main">
    <a href="#" class="red">red</a>
    <a href="#" class="green">green</a>
    <a href="#" class="blue">blue</a>
    <div></div>
</div>

CSS

#main {
    position: relative;
}

#main > a {
    display: block;
    padding: .5em;
}

#main > div {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: #eee;
    z-index: -1;
}

#main > a.red:hover ~ div {
    background-color: red;
}

#main > a.green:hover ~ div {
    background-color: green;
}

#main > a.blue:hover ~ div {
    background-color: blue;
}
coma
  • 15,247
  • 4
  • 46
  • 72
1

As One Trick Pony commented, there is no parent selector in pure CSS (at least yet). If you want to select the parent, you'd have to do so either through a different kind of selector (which requires knowledge of the DOM structure), or through something like Javascript.

In Javascript you can use .parentNode on the element to get its parent. If you have jQuery, you can call .parent().

Igor
  • 30,885
  • 14
  • 70
  • 107
1
$("#active").mouseenter(function () {
    $("#mainNav").css("background-color", "#333");
});

Don't forget to change it on mouse leaving as well.

$("#active").mouseleave(function () {
    $("#mainNav").css("background-color", "#ABC" /*some other color*/);
});
Geeky Guy
  • 8,658
  • 4
  • 38
  • 60
1

Since there is no way to have a CSS parent selector i would suggest you rethink your design.

add the class name active to the parent div when there is the div with id="active" as a child and you style the .active class to your needs:

<nav id="mainNav" class="{if child active print 'active'}">
   <div id="navContainer">
       <div id="active">
           [ some list ]
       </div><!-- #active -->
   </div><!-- #navContainer -->
</nav><!-- #mainNav -->

Depending on the language you are using you can accomplish this.

#mainNav.active:hover #active{
   ... your hover properties ...
}
Ibu
  • 39,552
  • 10
  • 71
  • 99
1

CSS4 defines a "subject" operator, which would allow you to do this:

!#mainNav #active:hover { background-color:#333 }

However, as far as I am aware no browsers support this yet. Instead, you can use this JavaScript:

(function() {
    var nav = document.getElementById('mainNav'), active = document.getElementById('active'),
        f = function(e) {
            e = e || window.event;
            nav.style.backgroundColor = e.type.match(/mouseover/) ? "#333" : "";
        };
    if( window.addEventListener) {
        active.addEventListener("mouseover",f,false);
        active.addEventListener("mouseout",f,false);
    }
    else {
        active.attachEvent("onmouseover",f);
        active.attachEvent("onmouseout",f);
    }
})();
Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
  • I thought the `!` was intended to *follow* the subject (`#mainNav!`, though there's been quite some discussion about preceding, following and/or wrapping in that regard)? And, incidentally, things seem to be changing (so far as CSS is concerned, at least) again: http://www.w3.org/TR/2013/WD-selectors4-20130502/#profiles – David says reinstate Monica May 10 '13 at 19:31
  • @DavidThomas The example showed it prefixed, and the note seems to mention something about it changing and being undecided XD – Niet the Dark Absol May 10 '13 at 19:33
  • The link was only to illustrate the fact that the subject selector isn't implemented in the 'fast' profile and "CSS implementations conformant to Selectors Level 4 must use the 'fast' profile for CSS selection." As to the syntax, yeah: honestly, I probably shouldn't have mentioned that, given the state of flux. – David says reinstate Monica May 10 '13 at 19:35