2

Can I use ":hover" effect to affect html tag which is higher in the nesting hierarchy? I am trying to affect element's CSS property by hovering over element lover in the hierarchy. As in example below. HTML example

<div>
  <ul>
    <li>
      <a>link</a>
    </li>
  </ul>
</div>

CSS example /* Usualy this is how we use it */

ul>li{
   color:red;
}

/* How can I use it other way round? */

li>div{
   color:blue;
}

/* or ? */

li>ul{
   color:yellow;
}

/* or ? */

a>div{
   color:black;
}

Examples most welcome. Thank you very much guys.

DevWL
  • 17,345
  • 6
  • 90
  • 86

2 Answers2

3

I believe that the best (and possibly only) way to do this is with the use of jQuery.

jQuery("a").parent("div').addClass('class_name');

then you can define the style using the class instead. To remove the class:

jQuery("a").parent("div').removeClass('class_name');
  • instead of adding a class can you also add CSS property ? – DevWL Oct 03 '13 at 12:41
  • 1
    I would think it would be easier to maintain the code if you use a class. But then again, I suppose that's personal preference more than anything. Just seems that semantically, it would make more sense to assign a class of "hover" and then it would be clear in the CSS along with the rest of the styling. – Nick Bernhard Oct 03 '13 at 18:34
0

It is possible in CSS4. But it is not supported in any of the major browsers.

You may find your answer here

Community
  • 1
  • 1
Beniamin Szabo
  • 1,909
  • 4
  • 17
  • 26