Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex CSS selector for parent of active child [duplicate]

Is there a way to select a parent element based on the class of a child element in the class? The example that is relevant to me relating to HTML output by a nice menu plugin for http://drupal.org. The output renders like this:

<ul class="menu">       <li>           <a class="active">Active Page</a>       </li>       <li>             <a>Some Other Page</a>       </li>   </ul>   

My question is whether or not it is possible to apply a style to the list item that contains the anchor with the active class on it. Obviously, I'd prefer that the list item be marked as active, but I don't have control of the code that gets produced. I could perform this sort of thing using javascript (JQuery springs to mind), but I was wondering if there is a way to do this using CSS selectors.

Just to be clear, I want to apply a style to the list item, not the anchor.

like image 592
Deeksy Avatar asked Sep 05 '08 00:09

Deeksy


People also ask

How do you select parent to child 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.

Can CSS be used to find child => parent page element?

There is currently no way to select the parent of an element in CSS, at least not a way that works across all browsers.

How do I select a parent sibling in CSS?

The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".


2 Answers

According to Wikipedia:

Selectors are unable to ascend

CSS offers no way to select a parent or ancestor of element that satisfies certain criteria. A more advanced selector scheme (such as XPath) would enable more sophisticated stylesheets. However, the major reasons for the CSS Working Group rejecting proposals for parent selectors are related to browser performance and incremental rendering issues.

And for anyone searching SO in future, this might also be referred to as an ancestor selector.

Update:

The Selectors Level 4 Spec allows you to select which part of the select is the subject:

The subject of the selector can be explicitly identified by prepending a dollar sign ($) to one of the compound selectors in a selector. Although the element structure that the selector represents is the same with or without the dollar sign, indicating the subject in this way can change which compound selector represents the subject in that structure.

Example 1:

For example, the following selector represents a list item LI unique child of an ordered list OL:

OL > LI:only-child 

However the following one represents an ordered list OL having a unique child, that child being a LI:

$OL > LI:only-child 

The structures represented by these two selectors are the same, but the subjects of the selectors are not.

Although this isn't available (currently, November 2011) in any browser or as a selector in jQuery.

like image 72
Sam Hasler Avatar answered Oct 07 '22 23:10

Sam Hasler


Unfortunately, there's no way to do that with CSS.

It's not very difficult with JavaScript though:

// JavaScript code: document.getElementsByClassName("active")[0].parentNode;  // jQuery code: $('.active').parent().get(0); // This would be the <a>'s parent <li>. 
like image 31
Dave Ward Avatar answered Oct 07 '22 22:10

Dave Ward