Parent Selectors in CSS

Album Cover: Into the Wild

"I knew all the rules, but the rules did not know me...guaranteed."
Eddie Vedder / Guaranteed

Posted on May 27, 2006 8:25 PM in Web Design
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

If you're like me and have ever run into a case where you'd like to select the parent of a given page element and apply a specific style to it, you've probably wondered if parent selectors are available in CSS. Well, the answer is no, they aren't. There's a good explanation as to why over at css-discuss:

...the reason beeing that implementers want to calculate the applicable rules in the moment they have read the start-tag of the element, which only allows selections based on previous siblings and ancestor elements. There is an exception in css3-selectors for e.g. :only-of-type which needs to know about following-siblings but then implementers argued that they do not want to make things worse...

Usually what I end up doing to work around the problem is assign a specific class to the parent element in advance so I can target it in my stylesheet(s). Parent selectors sure would make it a heck of a lot easier, though.


No one has added any comments.

Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.


Email Address:



Check this box if you hate spam.