3

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.

I know there was a Google survey about this, but that's over now.

What happened to the parent selector? Will it ever be introduced, or has it been removed?

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
U r s u s
  • 5,912
  • 10
  • 41
  • 84
  • I just use jQuery for the rare times that I need that selector. $(this).parent().addClass('parent'); I know that on css-tricks they said there are massive performance concerns around the css parent selector. – 2ne Jan 16 '15 at 11:28
  • 2
    How that is a duplicate I am not sure, but the "Does anyone know if and when" part of this question cannot be objectively answered by anyone other than browser vendors, which makes for either a question with potentially authoritative answers, or a very brittle one. That said, I posted an answer addressing the first part. – BoltClock Jan 16 '15 at 11:37
  • @chipChocolate.py I didn't ask about how to fake the effect, so I don't see how this is a duplicate. – U r s u s Jan 16 '15 at 11:41
  • @BoltClock Fair point, I was hoping for authoritative answers. – U r s u s Jan 16 '15 at 11:42
  • 1
    I tried to make your question a little more grounded and a better fit for the site. The development history of this particular feature is interesting to say the least and I'm pleasantly surprised someone actually posted a question relating to the survey. – BoltClock Jan 16 '15 at 12:30
  • @2ne the concerns are only massive if you are on a massive scale like Facebook or Google. – TylerH Feb 25 '15 at 05:20
  • @Ursus "doesn't mention the parent selector anymore" What? http://dev.w3.org/csswg/selectors-4/#relational http://dev.w3.org/csswg/selectors-4/#profiles – TylerH Feb 25 '15 at 05:23

3 Answers3

11

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, it fits right into the existing selector syntax, and you can reliably assume that :has() will 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.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
2

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.

Community
  • 1
  • 1
GoBusto
  • 4,013
  • 4
  • 21
  • 39
0

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>
Ton
  • 250
  • 2
  • 11