Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css3 :not() selector to test parent's class

I have an unordered list, using bootstraps "tabs" plugin. The code looks like this:

<ul>   <li class="active span3"><a href="#ourAgency" data-toggle="tab"><i class="icon-building icon-3x"></i>Our Agency</a></li>   <li class="span3"><a href="#studentVisas" data-toggle="tab"><i class="icon-laptop icon-3x"></i>Student Visas</a></li>   <li class="span3"><a href="#workVisas" data-toggle="tab"><i class="icon-suitcase icon-3x"></i>Work Visas</a></li>   <li class="span3"><a href="#accreditation" data-toggle="tab"><i class="icon-legal icon-3x"></i>Accreditation</a></li> </ul> 

I'd like to use CSS3 to change the colour of all the <a> links whose parent <li> DOESN'T have the class .active.

I've tried something like this:

 a:not(li.active>a){     color:grey; } 

but to no avail. Is there any way to do this or am I barking up the wrong tree?

like image 392
JVG Avatar asked Jan 10 '13 05:01

JVG


People also ask

How do I select a parent class in CSS?

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.

How do I select a parent in CSS selector?

There is currently no way to select the parent of an element in CSS, at least not a way that works across all browsers. If there was a way to do it, it would be in either of the current CSS selectors specs: Selectors Level 3 Spec. CSS 2.1 Selectors Spec.

Which is not a selector in CSS3?

The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class. The :not() pseudo-class has a number of quirks, tricks, and unexpected results that you should be aware of before using it.

Does CSS have a parent selector?

According to the CSS spec, the :has selector checks if a parent contains at least one element, or one condition like if an input is focused.


1 Answers

Combinators such as >, + and space for descendant aren't allowed within :not() in CSS; they're only allowed as a jQuery selector. You can find out more in this other question.

That said, you may be able to use :not() on the li alone, and move out the > a part; however this will depend on the structure of your ul and li elements:

li:not(.active) > a {     color: grey; } 

For example, you can always chain other selectors, such as .span3 if you want to limit it to a elements with li parents of that class only:

li.span3:not(.active) > a {     color: grey; } 

Keep in mind, though, that you can only rely on using :not() in this manner if you have control over the markup or the structure is at least predictable (e.g. you know what kind of elements the parents are). In your case for example, you're only looking at li.span3 > a, and applying styles only when the li.span3 does not have the active class. With this information you can construct a selector like one of the above, which should work as expected.

like image 93
BoltClock Avatar answered Sep 30 '22 03:09

BoltClock