The most recent information I can find regarding this is the W3C Selectors Level 4 Editor’s Draft, but, as far as I can see, it doesn\'t mention the parent selector anymore.
The survey culminated in the subject selector (the proper name for the so-fabled "parent selector") being replaced with the far more versatile :has()
pseudo-class, which is documented here (with an interesting anchor name, #relational
, I wonder if that will stick).
Implementations will probably only arrive when the specification for this feature is more stable. Currently, with such disruptive changes as completely replacing the subject selector with a pseudo-class, I'm not banking on it happening anytime soon. That said, it is likely that the :has()
pseudo-class will stick, but whether it can be implemented in CSS remains to be seen due to its very nature. See this section of the same draft to learn about implementation profiles.
The reason :has()
is more versatile is because, with the subject selector, it was never made clear in any draft if a single complex selector could have more than one subject selector (since a single complex selector can only ever have one subject) and/or if functional pseudo-classes such as :matches()
accepted the subject selector. But because a pseudo-class is a simple selector, you know that :has()
can be accepted anywhere a pseudo-class is accepted.
As an example, this makes such selectors as the following quite theoretically possible:
/*
* Select any p
* that is a sibling of a ul
* that has more than one li child.
*/
ul:has(> li:nth-of-type(2)) ~ p, /* p follows ul */
p:has(~ ul:has(> li:nth-of-type(2))) /* p precedes ul */
Whereas, using the subject selector, this would only be possible if :matches()
accepted the subject selector, which was never stated directly in the spec:
ul:matches(! > li:nth-of-type(2)) ~ p, /* p follows ul */
!p ~ ul:matches(! > li:nth-of-type(2)) /* p precedes ul */
You can also see here why I dislike the name "parent selector" for either form of the selector — it can be used for so much more.
Well in css there is no parent selector. But due to a little trick I can use .childinternal :parent { background-color: yellow } in my local CSS file without going to deep into jquery or javascript.
The trick is a bit dirty because it alters the style of the parent element (which is not what CSS does) and is not a real pseudo class. But in your css stylesheet you can use it as of it were.
There are two ways of implementing (both shown) The first one is a pseudo class :parent this can only be done on a local stylesheet because of the 'dont allow bad psuedos' of some browsers.
The otherone is one that runs over all stylesheets checking for a 'GetParent class reference'.
For me it works. I normally take the first one, which is the fastest.
Solution:
$(function() {
//First possibility when using local css file (=faster)//
$.when($.get("your local filename.css")).done(function(response) {
var spl = response.split(":parent");
if (spl.length > 1) {
var clas = $.trim((spl[0].split("}")[spl[0].split("}").length - 1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var cs = $.trim((spl[1].split("}")[0].split("{")[1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var eClas = $(clas).parent().attr('style');
eClas = eClas == undefined ? '' : (eClas + '').toString;
$(clas).parent().attr('style', eClas + ';' + cs);
}
});
});
// second possibility looping all used css files
for (var s = document.styleSheets.length - 1; s >= 0; s--) {
var cssRules = document.styleSheets[s].cssRules || document.styleSheets[s].rules || []; // IE support
for (var c = 0; c < cssRules.length; c++) {
if (cssRules[c].selectorText) {
if (cssRules[c].selectorText.indexOf('.GetParent') > -1) {
var spl = cssRules[c].cssText.split(".GetParent");
if (spl.length > 1) {
var clas = $.trim((spl[0].split("}")[spl[0].split("}").length - 1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var cs = $.trim((spl[1].split("}")[0].split("{")[1]).replace(String.fromCharCode(10), '').replace(String.fromCharCode(13), ''));
var eClas = $(clas).parent().attr('style');
eClas = eClas == undefined ? '' : (eClas + '').toString;
$(clas).parent().attr('style', eClas + ';' + cs);
}
}
}
}
}
.childinternal :parent {
background-color: yellow
}
.childexternal .GetParent {
Background-color: green
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Not affected Main parent
<div class="childinternal">
<p>Not affected parent (no parent selector)</p>
</div>
<div class="childinternal:parent">
<p>local css file used (:parent selector)
<span style='color:grey;font-size:0.6em'>Only works on local files so not possible to show in this snippet
</span>
</p>
</div>
</div>
<div>
<div class="childexternal .GetParent">
<p>external css file used (.GetParent class selector)</p>
<div class="x"></div>
</div>
</div>
According to Wikipedia:
Selectors are unable to ascend
CSS currently offers no way to select a parent or ancestor of an element that satisfies certain criteria. CSS Selectors Level 4, which is still in Working Draft status, proposes such a selector, but only as part of the “complete” selector profile, not the “fast” profile used in dynamic CSS styling. A more advanced selector scheme (such as XPath) would enable more sophisticated style sheets. The major reasons for the CSS Working Group previously rejecting proposals for parent selectors are related to browser performance and incremental rendering issues.
As for when Selectors Level 4 will be introduced, though... well, it depends on when the major browsers implement support for it. And when enough users upgrade to those browser versions.
Edit: See this answer for more information.