<p>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 <a href="http://drupal.org" rel="noreferrer">http://drupal.org</a>.  The output renders like this:  </p>

<pre><code>&lt;ul class="menu"&gt;  
    &lt;li&gt;  
        &lt;a class="active"&gt;Active Page&lt;/a&gt;  
    &lt;/li&gt;  
    &lt;li&gt;    
        &lt;a&gt;Some Other Page&lt;/a&gt;  
    &lt;/li&gt;  
&lt;/ul&gt;  
</code></pre>

<p>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.</p>

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

[{'answer_id': 45008, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 8, 'selected': True, 'text': '<p>Unfortunately, there\'s no way to do that with CSS.</p>\n\n<p>It\'s not very difficult with JavaScript though:</p>\n\n<pre><code>// JavaScript code:\ndocument.getElementsByClassName("active")[0].parentNode;\n\n// jQuery code:\n$(\'.active\').parent().get(0); // This would be the &lt;a&gt;\'s parent &lt;li&gt;.\n</code></pre>\n'}, {'answer_id': 45530, 'author': 'Sam Hasler', 'author_id': 2541, 'author_profile': 'https://Stackoverflow.com/users/2541', 'pm_score': 8, 'selected': False, 'text': '<p><a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets#Limitations" rel="noreferrer">According to Wikipedia</a>:</p>\n\n<blockquote>\n  <p><strong>Selectors are unable to ascend</strong> </p>\n  \n  <blockquote>\n    <p>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.</p>\n  </blockquote>\n</blockquote>\n\n<p>And for anyone searching SO in future, this might also be referred to as an ancestor selector.</p>\n\n<p><strong>Update:</strong> </p>\n\n<p>The <a href="http://www.w3.org/TR/2011/WD-selectors4-20110929/#subject" rel="noreferrer">Selectors Level 4 Spec</a> allows you to select which part of the select is the subject:</p>\n\n<blockquote>\n  <p>The subject of the selector can be explicitly identified by prepending\n  a dollar sign ($) to one of the compound selectors in a selector.\n  Although the element structure that the selector represents is the\n  same with or without the dollar sign, indicating the subject in this\n  way can change which compound selector represents the subject in that\n  structure.</p>\n  \n  <p><strong>Example 1:</strong></p>\n  \n  <p>For example, the following selector represents a list item LI unique child of\n  an ordered list OL:</p>\n\n<pre><code>OL &gt; LI:only-child\n</code></pre>\n  \n  <p>However the following one represents an ordered list OL having a unique child,\n  that child being a LI:</p>\n\n<pre><code>$OL &gt; LI:only-child\n</code></pre>\n  \n  <p>The structures represented by these two selectors are the same,\n  but the subjects of the selectors are not.</p>\n</blockquote>\n\n<p>Although this isn\'t available (currently, November 2011) in any browser or as a selector in jQuery.</p>\n'}, {'answer_id': 877438, 'author': 'Mark B', 'author_id': 108756, 'author_profile': 'https://Stackoverflow.com/users/108756', 'pm_score': 1, 'selected': False, 'text': '<p>I had the same problem with Drupal. Given the limitations of CSS, the way to get this working is to add the "active" class to the parent elements when the menu HTML is generated. There\'s a good discussion of this at <a href="http://drupal.org/node/219804" rel="nofollow noreferrer">http://drupal.org/node/219804</a>, the upshot of which is that this functionality has been rolled in to version 6.x-2.x of the nicemenus module. As this is still in development, I\'ve backported the patch to 6.x-1.3 at <a href="http://drupal.org/node/465738" rel="nofollow noreferrer">http://drupal.org/node/465738</a> so that I can continue to use the production-ready version of the module.</p>\n'}, {'answer_id': 3446316, 'author': 'David Clarke', 'author_id': 132599, 'author_profile': 'https://Stackoverflow.com/users/132599', 'pm_score': 5, 'selected': False, 'text': '<p>Late to the party again but for what it\'s worth it is possible using jQuery to be a little more succinct. In my case I needed to find the <code>&lt;ul&gt;</code> parent tag for a <code>&lt;span&gt;</code> tag contained in the child <code>&lt;li&gt;</code>. jQuery has the <code>:has</code> selector so it\'s possible to identify a parent by the children it contains (updated per @Afrowave\'s comment ref: <a href="https://api.jquery.com/has-selector/" rel="noreferrer">https://api.jquery.com/has-selector/</a>):</p>\n\n<pre><code>$("ul").has("#someId")\n</code></pre>\n\n<p>will select the <code>ul</code> element that has a child element with id <em>someId</em>. Or to answer the original question, something like the following should do the trick (untested):</p>\n\n<pre><code>$("li").has(".active")\n</code></pre>\n'}, {'answer_id': 7601165, 'author': 'Brian', 'author_id': 168645, 'author_profile': 'https://Stackoverflow.com/users/168645', 'pm_score': 4, 'selected': False, 'text': '<p>The first draft of <a href="http://www.w3.org/TR/2011/WD-selectors4-20110929" rel="noreferrer">Selectors Level 4</a> outlines a way to explicitly set the <em>subject</em> of a selector. This would allow the OP to style the list element with the selector <code>$li &gt; a.active</code></p>\n<p>From <a href="http://www.w3.org/TR/2011/WD-selectors4-20110929/#subject" rel="noreferrer">Determining the Subject of a Selector</a>:</p>\n<blockquote>\n<p>For example, the following selector represents a list item LI unique child of an ordered list OL:</p>\n<p><code>OL &gt; LI:only-child</code></p>\n<p>However the following one represents an ordered list OL having a unique child, that child being a LI:</p>\n<p><code>$OL &gt; LI:only-child</code></p>\n<p>The structures represented by these two selectors are the same, but the subjects of the selectors are not.</p>\n</blockquote>\n<p><em>Edit: Given how &quot;drafty&quot; a draft spec can be, it\'s best to keep tabs on this by checking the <a href="http://wiki.csswg.org/spec/selectors4" rel="noreferrer">CSSWG\'s page on selectors level 4</a>.</em></p>\n'}, {'answer_id': 7936364, 'author': 'John Drefahl', 'author_id': 624790, 'author_profile': 'https://Stackoverflow.com/users/624790', 'pm_score': 1, 'selected': False, 'text': '<p>I actually ran into the same issue as the original poster.  There is a simple solution of just using <code>.parent()</code> jQuery selector.  My problem was, I was using <code>.parent</code> instead of <code>.parent()</code>. Stupid mistake I know.</p>\n\n<p>Bind the events (in this case since my tabs are in Modal I needed to bind them with <code>.live</code> instead of a basic <code>.click</code>.</p>\n\n<pre><code>$(\'#testTab1 .tabLink\').live(\'click\', function() {\n    $(\'#modal ul.tabs li\').removeClass("current"); //Remove any "current" class\n    $(this).parent().addClass("current"); //Add "current" class to selected tab\n    $(\'#modal div#testTab1 .tabContent\').hide();\n    $(this).next(\'.tabContent\').fadeIn();   \n    return false;\n})\n$(\'#testTab2 .tabLink\').live(\'click\', function() {\n    $(\'#modal ul.tabs li\').removeClass("current"); //Remove any "current" class\n    $(this).parent().addClass("current"); //Add "current" class to selected tab\n    $(\'#modal div#testTab2 .tabContent\').hide();\n    $(this).next(\'.tabContent\').fadeIn();   \n    return false;\n})\n</code></pre>\n\n<p>Here is the HTML..</p>\n\n<pre><code>&lt;div id="tabView1" style="display:none;"&gt;\n  &lt;!-- start: the code for tabView 1 --&gt;\n  &lt;div id="testTab1" style="width:1080px; height:640px; position:relative;"&gt;\n    &lt;h1 class="Bold_Gray_45px"&gt;Modal Header&lt;/h1&gt;\n    &lt;div class="tabBleed"&gt;&lt;/div&gt;\n    &lt;ul class="tabs"&gt;\n      &lt;li class="current"&gt; &lt;a href="#" class="tabLink" id="link1"&gt;Tab Title Link&lt;/a&gt;\n        &lt;div class="tabContent" id="tabContent1-1"&gt;\n          &lt;div class="modalCol"&gt;\n            &lt;p&gt;Your Tab Content&lt;/p&gt;\n            &lt;p&gt;&lt;a href="#" class="tabShopLink"&gt;tabBased Anchor Link&lt;/a&gt; &lt;/p&gt;\n          &lt;/div&gt;\n          &lt;div class="tabsImg"&gt; &lt;/div&gt;\n        &lt;/div&gt;\n      &lt;/li&gt;\n      &lt;li&gt; &lt;a href="#" class="tabLink" id="link2"&gt;Tab Title Link&lt;/a&gt;\n        &lt;div class="tabContent" id="tabContent1-2"&gt;\n          &lt;div class="modalCol"&gt;\n            &lt;p&gt;Your Tab Content&lt;/p&gt;\n            &lt;p&gt;&lt;a href="#" class="tabShopLink"&gt;tabBased Anchor Link&lt;/a&gt; &lt;/p&gt;\n          &lt;/div&gt;\n          &lt;div class="tabsImg"&gt; &lt;/div&gt;\n        &lt;/div&gt;\n      &lt;/li&gt;\n    &lt;/ul&gt;\n  &lt;/div&gt;\n&lt;/div&gt;\n</code></pre>\n\n<p>Of course you can repeat that pattern..with more LI\'s</p>\n'}, {'answer_id': 9111040, 'author': 'Jared', 'author_id': 1184883, 'author_profile': 'https://Stackoverflow.com/users/1184883', 'pm_score': 0, 'selected': False, 'text': '<p>Another thought occurred to me just now that could be a pure CSS solution. Display your active class as an absolutely positioned block and set its style to cover up the parent li.</p>\n\n<pre><code>a.active {\n   position:absolute;\n   display:block;\n   width:100%;\n   height:100%;\n   top:0em;\n   left:0em;\n   background-color: whatever;\n   border: whatever;\n}\n/* will also need to make sure the parent li is a positioned element so... */\nul.menu li {\n    position:relative;\n}    \n</code></pre>\n\n<p>For those of you who want to use javascript without jquery...</p>\n\n<p>Selecting the parent is trivial. You need a <code>getElementsByClass</code> function of some sort, unless you can get your drupal plugin to assign the active item an ID instead of Class. The function I provided I grabbed from some other genius on SO. It works well, just keep in mind when you\'re debugging that the function will always return an array of nodes, not just a single node.</p>\n\n<pre><code>active_li = getElementsByClass("active","a");\nactive_li[0].parentNode.style.whatever="whatever";\n\nfunction getElementsByClass(node,searchClass,tag) {\n    var classElements = new Array();\n    var els = node.getElementsByTagName(tag); // use "*" for all elements\n    var elsLen = els.length;\n    var pattern = new RegExp("\\\\b"+searchClass+"\\\\b");\n    for (i = 0, j = 0; i &lt; elsLen; i++) {\n       if ( pattern.test(els[i].className) ) {\n       classElements[j] = els[i];\n       j++;\n   }\n}\nreturn classElements;\n}\n</code></pre>\n'}, {'answer_id': 11020342, 'author': 'Matt Wagner', 'author_id': 1445647, 'author_profile': 'https://Stackoverflow.com/users/1445647', 'pm_score': 4, 'selected': False, 'text': '<h2>Future answer with CSS4 selectors</h2>\n\n<p>New CSS Specs contain an experimental <code>:has</code> pseudo selector that might be able to do this thing.</p>\n\n<pre><code>li:has(a:active) {\n  /* ... */\n}\n</code></pre>\n\n<p>The <a href="https://caniuse.com/#search=%3Ahas" rel="noreferrer">browser support</a> on this is basically non-existent at this time, but it is in consideration on the <a href="https://www.w3.org/TR/selectors-4/#relational" rel="noreferrer">official specs</a>.</p>\n\n<hr>\n\n<h2>Answer in 2012 that was wrong in 2012 and is even more wrong in 2018</h2>\n\n<p>While it is true that CSS cannot ASCEND, it is incorrect that you cannot grab the parent element of another element. Let me reiterate:</p>\n\n<p>Using your HTML example code, you are able to grab the li without specifying li</p>\n\n<pre><code>ul * a {\n    property:value;\n}\n</code></pre>\n\n<p>In this example, the ul is the parent of some element and that element is the parent of anchor. The downside of using this method is that if there is a ul with any child element that contains an anchor, it inherits the styles specified.</p>\n\n<p>You may also use the child selector as well since you\'ll have to specify the parent element anyway.</p>\n\n<pre><code>ul&gt;li a {\n    property:value;\n}\n</code></pre>\n\n<p>In this example, the anchor must be a descendant of an li that MUST be a child of ul, meaning it must be within the tree following the ul declaration. This is going to be a bit more specific and will only grab a list item that contains an anchor AND is a child of ul.</p>\n\n<p>SO, to answer your question by code.</p>\n\n<pre><code>ul.menu &gt; li a.active {\n    property:value;\n}\n</code></pre>\n\n<p>This should grab the ul with the class of menu, and the child list item that contains only an anchor with the class of active.</p>\n'}, {'answer_id': 13506287, 'author': 'Praveen Kumar Purushothaman', 'author_id': 462627, 'author_profile': 'https://Stackoverflow.com/users/462627', 'pm_score': 5, 'selected': False, 'text': '<h3>THE “PARENT” SELECTOR</h3>\n<p>Right now, there is no option to select the parent of an element in CSS (not even CSS3). But with <strong>CSS4</strong>, the most important news in the current W3C draft is the support for the parent selector.</p>\n<pre><code>$ul li:hover{\n    background: #fff;\n}\n</code></pre>\n<p>Using the above, when hovering an list element, the whole unordered list will be highlighted by adding a white background to it.</p>\n<p>Official documentation: <a href="https://www.w3.org/TR/2011/WD-selectors4-20110929/#overview" rel="noreferrer">https://www.w3.org/TR/2011/WD-selectors4-20110929/#overview</a> (last row).</p>\n'}, {'answer_id': 14556845, 'author': 'ggedde', 'author_id': 2017192, 'author_profile': 'https://Stackoverflow.com/users/2017192', 'pm_score': 1, 'selected': False, 'text': '<p>Many people answered with <code>jQuery</code> parent, but just to add on to that I wanted to share a quick snippet of code that I use for adding classes to my navs so I can add styling to <code>li</code>\'s that only have sub-menus and not <code>li</code>\'s that don\'t.</p>\n\n<pre><code>$("li ul").parent().addClass(\'has-sub\');\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/45004', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4640/']