Is there a CSS parent selector? (ok)
https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector
Đã ok
Ask QuestionAsked 11 years agoActive 2 days agoViewed 2.1m times3212467
How do I select the <li>
element that is a direct parent of the anchor element?
As an example, my CSS would be something like this:
Obviously there are ways of doing this with JavaScript, but I'm hoping that there is some sort of workaround that exists native to CSS Level 2.
The menu that I am trying to style is being spewed out by a CMS, so I can't move the active element to the <li>
element... (unless I theme the menu creation module which I'd rather not do).
This question is over a decade old, but if anyone is interested in a way to do this on individual clients (not on a server), I can provide an answer. Just let me know. Please remember that my answer will not work on servers, and thus will not satisfy the most common need. – RockPaperLizard Jun 10 at 10:17
33 Answers
There is currently no way to select the parent of an element in CSS.
If there was a way to do it, it would be in either of the current CSS selectors specs:
That said, the Selectors Level 4 Working Draft includes a :has()
pseudo-class that will provide this capability. It will be similar to the jQuery implementation.
However, as of May 2020, this is still not supported by any browser.
69It would seem that it has already been suggested and rejected: stackoverflow.com/questions/45004/… – RobM Oct 27 '10 at 12:22
14Looks like the subject selector has been revisited, except by using a
!
now:The subject of the selector can be explicitly identified by appending an exclamation mark (!) to one of the compound selectors in a selector.
– animuson♦ Jan 29 '12 at 21:3013The prepended
$
looked better for me... the appended!
can be overlooked more easily. – Christoph Feb 13 '12 at 11:2551Major plot twist! The Selectors 4 WD was just updated today to exclude the subject indicator from the fast profile, which is to be used by CSS implementations. If this change remains, it means you won't be able to use the subject indicator in stylesheets anymore (unless you use add some sort of polyfill like the one described in another answer) - you can only use it with the Selectors API and anywhere else that the complete profile may be implemented. – BoltClock♦ May 2 '13 at 15:19
56Another major update: it looks like they're considering doing away with the subject selector syntax altogether and replacing it with the
:has()
pseudo everybody has come to know and love from jQuery. The latest ED has removed all references to the subject indicator, and replaced it with the:has()
pseudo. I don't know the exact reasons, but the CSSWG held a poll some time ago and the results must have influenced this decision. It's most likely for compatibility with jQuery (so it can leverage qSA without modifications), and because the subject indicator syntax proved too confusing. – BoltClock♦ May 4 '14 at 14:28
I don’t think you can select the parent in CSS only.
4You can't shift the pseudo selector to the list item, as it is not a focusable element. He's using the :active selector, so when the anchor is active he wants the list item to be affected. List items will never be in the active state. As an aside, it's unfortunate that such a selector doesn't exist. Getting a pure CSS menu to be fully keyboard accessible seems to be impossible without it (using sibling selectors you can make submenus created using nested lists to appear, but once the list gains focus it becomes hidden again). If there are any CSS-only solutions to this particular conun – user914183 Aug 26 '11 at 13:46
13@Dominic Aquilina Take a look at the question, the OP is using a class, not a pseudo selector. – jeroen Aug 26 '11 at 14:34
add a comment1291
You can use this script:
This will select any parent of a text input. But wait, there's still much more. If you want, you can select a specified parent:
Or select it when it's active:
Check out this HTML:
You can select that span.help
when the input
is active and show it:
There are many more capabilities; just check out the documentation of the plugin.
5suppose using jquery
patent()
would be faster. This need testing, however – Dan Sep 22 '11 at 19:302@Idered It fails when you have CSS declaration of a Selector Subject with no child selector (
#a!
alone throws an error,#a! p
works), and so the others will not works either because ofUncaught TypeError: Cannot call method 'split' of undefined
: see jsfiddle.net/HerrSerker/VkVPs – yunzen Apr 16 '13 at 15:333@HerrSerker I think #a! is an invalid selector, what should it select? – Idered Apr 17 '13 at 13:41
2@Idered I don't know. The spec doesn't say it is illegal. #a! should select itself. At least their should be no error in the JavaScript – yunzen Apr 17 '13 at 15:31
5Per my comment on the accepted answer, it looks like the polyfill may be required even in the near future after all, because the subject indicator may never be implemented by browsers in CSS. – BoltClock♦ May 2 '13 at 15:35
As mentioned by a couple of others, there isn't a way to style an element's parent/s using just CSS but the following works with jQuery:
21The
<
selector does not exist (verified using jQuery 1.7.1). – Rob W Feb 23 '12 at 17:536Perhaps that
<
-syntax worked in 2009 but I've updated it (for 2013). – Alastair May 2 '13 at 5:105Even better, use jQuery's built-in
:has()
selector:$("li:has(a.active)").css("property", "value");
. It reads similarly to CSS 4's proposed!
selector. See also::parent
selector,.parents()
method,.parent()
method. – Rory O'Kane May 8 '13 at 22:127And rather than using
.css("property", "value")
to style the selected elements, you should usually.addClass("someClass")
and have in your CSS.someClass { property: value }
(via). That way, you can notate the style with the full power of CSS and any preprocessors you are using. – Rory O'Kane May 8 '13 at 22:20
add a comment1025
Yes: :has()
There is no parent selector; just the way there is no previous sibling selector. One good reason for not having these selectors is because the browser has to traverse through all children of an element to determine whether or not a class should be applied. For example, if you wrote:
Then the browser will have to wait until it has loaded and parsed everything until the </body>
to determine if the page should be red or not.
12so make the browser faster. the internet itself faster. this selector is definitely needed, and the reason for not implementing it is, sadly, because we live in a slow, primitive world. – vsync Feb 19 '14 at 21:33
11actually, the selector would make a very fast browser look slow. – Salman A Feb 20 '14 at 4:09
6I trust that browser developers would come up with an implementation which is (at least) as fast as the javascript version, which is the one people end up using anyway. – Marc.2377 Jun 9 '18 at 0:15
"we live in a slow, primitive world" cough cough new apple watch cough cough – Marvin May 5 '19 at 19:26
@Marc.2377 If you try the above example in JS on your website, I'm never going to visit it. On the other side, I'd say that most of the time, you only care about immediate children, so if it was only limited to the immediate children, it'd be a good addition without too much impact. – xryl669 Sep 7 '19 at 12:41
There isn't a way to do this in CSS 2. You could add the class to the li
and reference the a
:
3by making the a element display:block you can style it to fit the whole of the li area. if you can explain what style you are looking for perhaps I could help with a solution. – Josh Jun 18 '09 at 21:53
this is actually an simple and elegant solution and, in many cases, it makes sense to set the class on the parent. – Ricardo Mar 18 '16 at 23:17
Try to switch a
to block
display, and then use any style you want. The a
element will fill the li
element, and you will be able to modify its look as you want. Don't forget to set li
padding to 0.
The CSS selector “General Sibling Combinator” could maybe used for what you want:
25This is just sibling selector, it's not child, parent... not really answering the question mate – Alireza Jun 16 '17 at 18:08
I know the OP was looking for a CSS solution but it is simple to achieve using jQuery. In my case I needed to find the <ul>
parent tag for a <span>
tag contained in the child <li>
. jQuery has the :has
selector so it's possible to identify a parent by the children it contains:
will select the ul
element that has a child element with id someId. Or to answer the original question, something like the following should do the trick (untested):
3Or use
$yourSpan.closest("ul")
and you'll get the parent UL of your span element. Nevertheless your answer is completely offtopic imho. We can answer all of the CSS-taged questions with a jQuery solution. – Robin van Baalen Jul 18 '13 at 17:401As identified in other answers, there is no way to do this using CSS 2. Given that constraint, the answer I provided is one I know is effective and is the simplest way to answer the question using javascript imho. – David Clarke Dec 19 '15 at 19:27
Given your upvotes, some people agree with you. But the only answer remains the accepted one; plain and simple "it cant be done the way you want it". If the question is how to archieve 60mph on a bicycle and you answer it with "simple; get in a car and hit the gas.", it's still not an answer. Hence my comment. – Robin van Baalen Dec 19 '15 at 19:36
1That approach would make SO almost completely useless. I search SO for how to solve problems, not to find the "only answer" doesn't address the issue. That is why SO is so awesome, because there is always more than one way to skin a cat. – David Clarke Dec 20 '15 at 18:35
The pseudo element :focus-within
allows a parent to be selected if a descendent has focus.
An element can be focused if it has a tabindex
attribute.
Browser support for focus-within
Example
Run code snippetExpand snippet
2This works fine in my case, thanks! Just a not, the example would be more ilustrative if you change the color to the parent, not to the sibling, this is replace
.color:focus-within .change
with.color:focus-within
. In my case i'm using bootstrap nav-bar that add the class to the children when active and I want to add a class to the parent .nav-bar. I think this is a pretty common scenario where I own the markup but the component css+js is bootstrap (or other) so I cannot change the behavior. Although I can add tabindex and use this css. Thanks! – cancerbero Jun 29 '18 at 15:13
This is the most discussed aspect of the Selectors Level 4 specification. With this, a selector will be able to style an element according to its child by using an exclamation mark after the given selector (!).
For example:
will set a red background-color if the user hovers over any anchor.
You might try to use hyperlink as the parent, and then change the inner elements on hover. Like this:
1That is correct, but limits the markup code within the
a
tag to certain elements only, if you want to conform to XHTML standards. For instance, you cannot use adiv
withina
, without getting a warning of violating the schema. – Ivaylo Slavov Jul 24 '12 at 18:222Totaly right Ivaylo! "a" is a non-block element, so can't use block elements inside it. – riverstorm Dec 12 '12 at 22:34
1In HTML5 it is perfectly fine to put block elements inside links. – Matthew James Taylor Apr 6 '14 at 7:47
2... if it was semantically wrong, they wouldn't have allowed it in HTML5 where it wasn't before. – BoltClock♦ Dec 28 '15 at 15:54
Currently there is no parent selector & it is not even being discussed in any of the talks of W3C. You need to understand how CSS is evaluated by the browser to actually understand if we need it or not.
There is a lot of technical explanation here.
Jonathan Snook explains how CSS is evaluated.
Chris Coyier on the talks of Parent selector.
Harry Roberts again on writing efficient CSS selectors.
But Nicole Sullivan has some interesting facts on positive trends.
12The
need
is defined by web developers' requirements, whether to have it in the spec is decided by other factors. – nicodemus13 May 15 '14 at 9:04
Just an idea for horizontal menu...
Part of HTML
Part of CSS
Updated demo and the rest of code
3It's not at all clear to me how you mean to generalize this to some/many/most of the parent selector use cases, or even exactly which parts of this CSS are doing what. Can you add a thorough explanation? – Nathan Tuggy Oct 28 '15 at 7:57
2I did not try to apply this to real world scenarios, that is why I say "Not for production". But I think It can be applied to 2-level menu only with fixed item width. "which parts of this CSS are doing what" - .test-sibling here is actually background of parent item (the last line of CSS). – Ilya B. Oct 28 '15 at 8:08
2Added explanation (css section of jsfiddle, starting from "MAIN PART")... And I was mistaken - there may be any number of sublevels. – Ilya B. Oct 28 '15 at 12:59
Here's a hack using pointer-events
with hover
:
Run code snippetExpand snippet
There's a plugin that extends CSS to include some non-standard features that can really help when designing websites. It's called EQCSS.
One of the things EQCSS adds is a parent selector. It works in all browsers, Internet Explorer 8 and up. Here's the format:
So here we've opened an element query on every element a.active
, and for the styles inside that query, things like $parent
make sense, because there's a reference point. The browser can find the parent, because it's very similar to parentNode
in JavaScript.
Here's a demo of $parent
and another $parent
demo that works in Internet Explorer 8, as well as a screenshot in case you don't have Internet Explorer 8 around to test with.
It's now 2019, and the latest draft of the CSS Nesting Module actually has something like this. Introducing @nest
at-rules.
3.2. The Nesting At-Rule: @nest
While direct nesting looks nice, it is somewhat fragile. Some valid nesting selectors, like .foo &, are disallowed, and editing the selector in certain ways can make the rule invalid unexpectedly. As well, some people find the nesting challenging to distinguish visually from the surrounding declarations.
To aid in all these issues, this specification defines the @nest rule, which imposes fewer restrictions on how to validly nest style rules. Its syntax is:
@nest = @nest <selector> { <declaration-list> }
The @nest rule functions identically to a style rule: it starts with a selector, and contains declarations that apply to the elements the selector matches. The only difference is that the selector used in a @nest rule must be nest-containing, which means it contains a nesting selector in it somewhere. A list of selectors is nest-containing if all of its individual complex selectors are nest-containing.
(Copy and pasted from the URL above).
Example of valid selectors under this specification:
Technically there is no direct way to do this. However, you can sort that out with either jQuery or JavaScript.
However, you can do something like this as well.
jQuery
27false. because the DOM is a tree, they have to go to the parent before getting to the child, so the simply just go back one node. o.o – NullVoxPopuli Nov 10 '11 at 16:56
9CSS selectors are a queue so selector order is evaluated rather than the document XPath or DOM hierarchy. – Paul Sweatte Aug 18 '12 at 16:14
3@rgb At least that's what they told us. – yunzen Apr 16 '13 at 15:35
The short answer is NO; we don't have a parent selector
at this stage in CSS, but if you don't have to swap the elements or classes anyway, the second option is using JavaScript. Something like this:
Or a shorter way if you use jQuery in your application:
Although there is no parent selector in standard CSS at present, I am working on a (personal) project called axe (ie. Augmented CSS Selector Syntax / ACSSSS) which, among its 7 new selectors, includes both:
an immediate parent selector
<
(which enables the opposite selection to>
)an any ancestor selector
^
(which enables the opposite selection to[SPACE]
)
axe is presently in a relatively early BETA stage of development.
See a demo here:
http://rounin.co.uk/projects/axe/axe2.html
3The project is in an early Beta stage at present. You can (at least currently) activate axe selectors in your CSS styles by including
<script src="https://rouninmedia.github.io/axe/axe.js"></script>
at the very end of your html document, just before</body>
. – Rounin Jul 12 '17 at 16:40
At least up to and including CSS 3 you cannot select like that. But it can be done pretty easily nowadays in JavaScript, you just need to add a bit of vanilla JavaScript, notice that the code is pretty short.
Run code snippetExpand snippet
Any ideas?
CSS 4 will be fancy if it adds some hooks into walking backwards. Till then it is possible (though not advisable) to use checkbox
and/or radio
input
s to break the usual way that things are connected, and through that also allow CSS to operate outside of its normal scope...
Run code snippetExpand snippet
... pretty gross, but with just CSS and HTML it is possible to touch and re-touch anything but the body
and :root
from just about anywhere by linking the id
and for
properties of radio
/checkbox
input
s and label
triggers; likely someone'll show how to re-touch those at some point.
One additional caveat is that only one
input
of a specificid
maybe used, firstcheckbox
/radio
wins a toggled state in other words... But multiple labels can all point to the sameinput
, though that would make both the HTML and CSS look even grosser.... I'm hoping that there is some sort of workaround that exists native to CSS Level 2...
I am not sure about the other :
selectors, but I :checked
for pre-CSS 3. If I remember correctly, it was something like [checked]
which is why you may find it in the above code, for example,
... but for things like ::after
and :hover
, I'm not at all certain in which CSS version those first appeared.
No, you cannot select the parent in CSS only.
Changing parent element based on child element can currently only happen when we have an <input>
element inside the parent element. When an input gets focus, its corresponding parent element can get affected using CSS.
Following example will help you understand using :focus-within
in CSS.
Run code snippetExpand snippet
It's possible with ampersand in Sass:
CSS output:
2it's true, but only if you know the selector in advance. – Shahar Aug 9 '17 at 8:02
11This does not select
h3
's parent, which was the goal. This would selecth3
s which are descendants of.some-parent-selector
. – Jacob Ford Mar 25 '19 at 12:41
I'd hire some JavaScript code to do that. For example, in React when you iterate over an array, add another class to the parent component, which indicates it contains your children:
And then simply:
There no css (and therefore in css preprocessors) parent selector due to "The major reasons for the CSS Working Group previously rejecting proposals for parent selectors are related to browser performance and incremental rendering issues."share improve this answer follow
Last updated