Skip to content

[selectors] Functional pseudo-class like :matches() with 0 specificity #1170

Closed

Description

@LeaVerou

The problem

Sometimes, specificity is a blessing. However, more often than not, it gets in the way, and authors have to resort to hacks in order to artificially adjust specificity, such as repeating classes (.foo.foo.foo), turning id selectors to attribute selectors ([id=foo] instead of #foo), or adding pointless :not()s (e.g. .foo:not(.enough):not(.specificity)).

A common example where specificity goes against author intent is :not(). The default specificity rules on :not() are almost never helpful. One often includes lots of filters, one after the other, but that increases specificity and makes these rules hard to override. E.g. good luck overriding div:not(#foo):not(#bar):not(#baz) in a sane way, even though this selector is essentially as general as div minus three specific divs.

In the last few years, it has even become common to avoid selector logic altogether, just to avoid specificity woes. This is basically what conventions like BEM are all about: turning combinators and pseudo-classes into a single class selector so that every selector has the same specificity and the cascade only goes top to bottom. For example, authors would write .form__input--disabled instead of form input[disabled].

Specificity becomes an even bigger problem when authoring UI libraries, where you want your styles to be easy to override by the end author without them having to keep up with your stylesheet, but you still want to provide sensible defaults. The default Mavo stylesheet has many good examples of this. It's a stylesheet that's meant to be easy to override, and in practice, authors have to artificially increase specificity of their rules to override it because I had to be very conservative in what is styled so the selectors have to be very specific. However, being specific doesn't always mean they are high priority.

All in all, specificity is a heuristic. It's assuming that it can deduce rule priority from the selection logic, but the selector is not really expressing priority, it's expressing a query. Heuristics are very helpful when they are correct, but a PITA when they fail, so there always needs to be a way to override them. Especially in this case, specificity is a heuristic that fails so often that many authors have decided the benefit they get from selectors is not worth the specificity trouble and prefer to ditch selectors altogether and just use classes + JS for the querying logic!

Proposed solution

A new pseudo-class (name TBB, perhaps :filter()?) that works exactly like :matches() but with zero specificity. That way, authors have control over the specificity of their rules, and can choose what is best for their use case.

Or, even :matches() itself, since it has not yet been implemented, and changing its specificity would solve #1027 at once.

Edited on Sep 15 to add :filter() and some more thoughts

Activity

  1. changed the title [-][selectors-4] Functional pseudo-class like :matches() with 0 specificity[/-] [+][selectors] Functional pseudo-class like :matches() with 0 specificity[/+] on Apr 14, 2017
  2. LeaVerou commented on Apr 14, 2017

    @LeaVerou
    MemberAuthor
  3. FremyCompany commented on Apr 16, 2017

    @FremyCompany
    Contributor

    cc @gregwhitworth who is a long-proponent of a similar solution.

    I was initially skeptical but then found some cases where this would be useful; all in all I would be in favor of such a change to :matches.

    FWIW one such example is that sometime you need to apply some form of css reset for an element to work properly (because it comes from another framework and doesn't like the fact you override flex-direction to column by default for all elements) so you want to unset flex-direction again inside that element but doing so means that the reset will override any selector targeting the content of the element if its selector is less specific than the selector you use to target the reset root.

  4. SelenIT commented on Apr 17, 2017

    @SelenIT
    Collaborator

    Or, even :matches() itself, since it has not yet been implemented

    What's wrong with WebKit's implementation of :matches()?

  5. bkardell commented on Sep 15, 2017

    @bkardell
    Contributor

    I think about this nearly every single day. Agree this would be worth doing!

  6. LeaVerou commented on Sep 15, 2017

    @LeaVerou
    MemberAuthor

    (For those wondering where my comment went, I integrated it into the first post and then deleted it)

  7. studiosciences commented on Sep 15, 2017

    @studiosciences

    I have a component with a lot of states, but I want to have a single class. I do this with data attributes now, but it increases specificity. Currently it's something like this (Suggest naming this :is)...

    .button {
        ...styles 
    
        &:is([data-size=small]) {
            ...styles
        } 
        &:is(:focus) {
            ...styles
        }
        &:is([disabled]) {
            ...styles
        }
        &:is([data-selected]) {
            ...styles
        } 
    }
  8. tabatkins commented on Sep 15, 2017

    @tabatkins
    Member

    I'm convinced by this reasoning; in particular, that "div, but minus these three specific ones" should be capable of being overridden reasonably, instead of blowing up to a 3-ID specificity.

    Do we want to stick with this "something that makes its contents specificity-0", or try and generalize this to solve specificity hacks more generally? I'm not 100% sure how we'd do the latter in a future-friendly way (we've gone back and forth with precisely how many categories are in "specificity"), but I guess just sticking with the Big Three would be fine. (When we've tried to insert something else, it's for significant semantic differences, like scoped vs not, or inline vs not.)

    Hmm. If we simplify and say that it has to be a compound selector only, then the grammar's pretty easy; if you want more complexity, you just nest a :matches() in. I think I'm in favor of the simple, short :is() naming, as this is more-or-less a no-op wrapper; every selector is an "is" filter already. Then you could control specificity like :is(.foo, 1 2 3) to give it the specificity of 1 ID, two attrs, and 3 tagnames. Leaving the specificity off defaults it to 0 0 0.

    (I suppose we could allow a full complex selector, as commas don't interfere with that, and just require :matches() if you want a selector list. But maybe good to keep it simple - it's just a modifier to a single selector. We let you do a full compound selector, rather than limiting to a single simple, to avoid verbosity in a common case - .foo:is(.bar):is(.baz) is identical in meaning to .foo:is(.bar.baz), no reason to require more parens than necessary there.)

  9. philipwalton commented on Sep 15, 2017

    @philipwalton
    Member

    Do we want to stick with this "something that makes its contents specificity-0", or try and generalize this to solve specificity hacks more generally?

    I'm in favor of a generalized approach.

    What about something that just turns off specificity at the stylesheet level, and for those stylesheets everything is order based? This would allow authors to easily control when they wanted specificity to work as an override and when they wanted to ignored it and treat it purely as a query.

    This would also help with resets and third-party stylesheets, and make it much easier for them to never clash with first-party styles.

    Maybe something like:

    <link nospecificity rel="stylesheet" hef="path/to/reset.css">
  10. LeaVerou commented on Sep 15, 2017

    @LeaVerou
    MemberAuthor

    @tabatkins
    Interesting idea, certainly more readable than current specificity hacks!
    However, I wonder if it would be better to add single-argument :is() to level 4 and extend to allow for a second argument in Level 5? I'm a bit concerned that the extra functionality might slow down implementations, and since selectors don't fall back as gracefully as other unsupported CSS features, adoption will be slow anyway.

  11. LeaVerou commented on Sep 15, 2017

    @LeaVerou
    MemberAuthor

    What about something that just turns off specificity at the stylesheet level, and for those stylesheets everything is order based? This would allow authors to easily control when they wanted specificity to work as an override and when they wanted to ignored it and treat it purely as a query.

    While something like this could be useful in some cases (as you mention, resets are a prime candidate), I'm not sure it should be the only way to override the specificity heuristic. Specificity can be a useful concept, it's just that the inferred specificity is often wrong.

    Also, you don't want any author rule to be able to override any rule in the third-party stylesheet. E.g. your library may have a .button component, you don't want their div or even * rule to override that, otherwise it becomes impossible to style anything.

  12. SelenIT commented on Sep 15, 2017

    @SelenIT
    Collaborator

    Speaking about current specificity hacks, in CSS Selectors 4 there is already a way to build complex selectors with the specificity of a single class using nested :not() pseudo-classes (proposed by @kizu and proven to work in WebKit). Maybe it could be useful as a reference?

  13. kizu commented on Sep 15, 2017

    @kizu
    Member

    Having something like that :is() instead of the nested :not() hack would be so much better of course.

    Though, I'm still thinking about (maybe alongside this :is()) having some way of creating your own cascade level, which you could then place above or below the default author one. This would allow either to import any external library on a level below, or write your styles on a level above, or even write code in multiple levels, like according to @csswizardry's ITCSS.

    I understand that that goes beyond the scope of this exact issue, but in my opinion this would cover some of the cases much better and with much less code from the authors (and I think it should be also really easily implementable, the main problem would be in coming up with syntax and handling all the nuances). Below I'd briefly write what I managed to come up with, if that sounds good and there is interest for something like that or at least for discussion, I could create a new issue for it maybe (for [css-cascade] probably?).

    1. By default all the author CSS goes to the default “Normal author” level (or “origin”, but I like the term “layer” for this thing more).

    2. Now, it is possible to create a new “layer”. There are now two possibilities: either this layer would be nameless, or with a set name:

    • Nameless layers: @layer {…} — by default everything that would go into those nameless layers would be put into one layer/origin that is above the one they were declared at. There should be also a way to put those not above, but below the current level, either with some keyword or stuff like @layer (position: below) {…}.
    • Named layers: @layer foo {…} — whenever you declare a new layer this way, it would be placed above the previous declared one (or below if used with a keyword). Then, all the next @layers with the same name would be merged into this place, so the first occurrence matters.
    1. Layers can be nested one into each other, and there should be a way to handle things similar to how we can now handle stacking context & z-index (we should be able both to put the layer somewhere in the global context, or only manage it inside the current layer).

    Basically, that's it. There are a lot of nuances and ways to implement some of this stuff, but: the main idea is to have a simple way of separating big chunks of code into independent layers similar to how the cascade of origins works now, basically allowing authors to create multiple author origins (or — split the current author's origin into multiple layers, as all those styles should still be at its place in the cascade).

    And, yes, the idea is still raw, and if there is interest we can discuss it in a new issue I guess? And, anyway, I think there regardless should be a way to handle the specificity of selectors like with is(), and I like the proposals for it above.

  14. LeaVerou commented on Sep 15, 2017

    @LeaVerou
    MemberAuthor

    @kizu yeah, @fantasai had a similar idea, you should post about it and link to this issue as well!

  15. 30 remaining items

  16. kizu commented on Oct 3, 2017

    @kizu
    Member

    I'm more for :matches() for having its specificity in a place, and for :is() for having zero specificity. Having a zero-specificity complex selectors would surely make it so it would be possible to write complex stuff without relying on third-party tools, though they could still be helpful even in that case by making some things easier.

  17. mattbrundage commented on Oct 13, 2017

    @mattbrundage
    Contributor

    This is a good proposition because it does what !important is sensibly used for, only in the opposite direction.

    @Stolzenhain , For the longest time, I've half-jokingly suggested to my coworkers that we need a complementary !unimportant keyword that we could apply to a selector. It's good to see that this basic concept is getting some traction.

  18. bradkemper commented on Oct 28, 2017

    @bradkemper
    Contributor

    Something like this would be nice:

    @specificity lower {
        @specificity lower {
             ...
             @specificity none {
                     ...
              }
         }
    }
    
  19. css-meeting-bot commented on Nov 7, 2017

    @css-meeting-bot
    Member

    The Working Group just discussed Functional pseudo-class like :matches() with 0 specificity, and agreed to the following resolutions:

    • RESOLVED: Add to selectors level 4 with a TBD name.
    The full IRC log of that discussion <leaverou> topic: Functional pseudo-class like :matches() with 0 specificity
    <eae> leaverou: As we all know specificity tries to infer importance from selectors. Resulting specificity might not always be logical.
    <astearns> github: https://github.com//issues/1170
    <Chris_> github, delete all comments
    <eae> leaverou: My favorite example of specificity not being a good heuristic is using the not pseudo class.
    <leaverou> div:not(#foo):not(#bar):not(#baz)
    <eae> leaverou: ^^^
    <eae> leaverou: In this example we want to target all dives except three, yet gets very high specificity.
    <eae> leaverou: Especially for libraries, when trying to be specific, it is hard to override.
    <eae> leaverou: uses something called BAM(?) encodes selector in class name and uses js to apply it.
    <bradk_> BEM
    <TabAtkins> s/BAM/BEM/
    <Chris_> .how__many__people__have__hear__of__bem
    <eae> leaverou: Willing to not use selectors at all, to bypass specificity.
    <bradk_> http://getbem.com/introduction/
    <eae> leaverou: One way to fix this would be to define selectors with a known specificity of one, would allow last selector win and make it easier to override.
    <fantasai> q+
    <eae> leaverou: I.e. put all nots in a single pseduo class. Easy solution vs other ones like per origin.
    <eae> leaverou: Lets one be very specific with regardess to specificty, can be selected per selector and solves most of the known problems.
    <eae> leaverou: Something that could be done as a pre-processor.
    <eae> leaverou: Many names have been proposed, we can bikeshed that.
    <eae> fantasai: Seems like a reasonable idea to me. The fact that authors can chose which parts of the selector applies to specificity sounds good to me.
    <bkardell_> ?+
    <eae> fantasai: Lower priority for an entire style sheet, library author might want to allow the entire stylesheet to be allowed to be overriden by author rules.
    <Rossen> q?
    <eae> leaverou: Example: in ?? there is a toolbar that has a class of mv-bar, also class of mv-ui that means applpy default style. Want author to be able to overrule using just mv-bar but I don't want their "div" rules to override.
    <bradk_> q+
    <astearns> s/??/mavo/
    <Rossen> ack fantasai
    <Rossen> ack bkardell_
    <eae> bkardell_: I don't think they are mutually exclusive. May be that you can do an awful lot for a whole bunch of things and we know we can do that easily.
    <eae> bkardell_: For a number of polyfills that I have worked on this would have been really useful.
    <eae> bkardell_: There are things like headings that need to take aria roles into account that make them have very high specificity which makes it hard to override.
    <TabAtkins> q+
    <Rossen> ack bradk_
    <eae> bradk_: Seems to me like we could solve this without taking specificity all the way down to zero.
    <eae> bradk_: Specificty only matters when there is a tie.
    <eae> bradk_: If there is a tie when it comes to ID or class, if the ID selector inside the funciton was more specific then outside the function that would solve the same problem while still allowoing specificity.
    <Rossen> q?
    <fantasai> Dael, here's the diagrams for Nat's presentation on line grids; please insert them in the correct section of the minutes. :) https://lists.w3.org/Archives/Public/www-archive/2017Nov/att-0008/CSSWG_2017.11.6_nmccully.pdf
    <eae> leaverou: As dicussed in issue, if we have fractions of specificity that introduces new levels of specificity and no one is suggestion that the entire thing doesn't have specicifity. All the classes and IDs outside of it still applies. It allows the specificity to be controlled. To have it count as a class you would just add it to the end, a bit hacky.
    <eae> bradk_: No use case for specifying it for the entire selector?
    <eae> leaverou: Yes but usaully not. You would only want to [put some criteria in the pseduo class. Like all the not for example.
    <eae> leaverou: Makes it less predictable. Now we know that we can count the number of IDs, number of classes, tags etc. If it is always zero it is very obiovus, if not it makes it much more complciated to compute it.
    <Rossen> q?
    <Rossen> ack TabAtkins
    <eae> TabAtkins: I agree in general with what leaverou is saying. Authors can often do just fine when specificity is in order. Opting out or in-order seems ot match what users want.
    <eae> TabAtkins: AS for chrome, we're happy with this, and would like to implement the stornger version of matches. This is easier, we might do this firts.
    <eae> TabAtkins: We're supportive.
    <Rossen> q?
    <eae> florian: Let's put this in level 4 for now, we don't have a five.
    <eae> fantasai:
    <eae> Rossen: Let's add it to four.
    <eae> fantasai: Very simple feature, as long as we're happy with the name.
    <eae> ericwillgers: What about web platform tests?
    <eae> fantasai: Not in CR yet, not needed.
    <eae> <laughter>
    <dbaron> q+
    <eae> dbaron: One comment: I think it is worth nothing that it is not clear how easy it would be to implement matches. This has most of the same risk.
    <Rossen> ack dbaron
    <eae> dbaron: One of the things with selectors, they're heavily optimized. Implementing without optimizaions not very useful. With optimizations is quite a bit of work and not clear how quickly that can happen.
    <eae> fantasai: Start with implemented subset.
    <eae> TabAtkins: WebKit goes beyond spec, also has support for pseduo elements.
    <eae> dbaron: Caution in that it's being tied to a feature which has an uncertain future.
    <eae> fantasai: De-risk by having certain things in level 4 vs 5.
    <eae> dbaron: Other alternatives that solves leaverou use cases.
    <eae> leaverou: I don't want to reduce *all* of it to zero but only a part.
    <bkardell_> ?+
    <Chris_> q?
    <eae> dbaron: I don't have a syntax proposal at this time but would avoid tie to branching combinators.
    <eae> TabAtkins: Earliest version didn't support that, does not.
    <eae> does now.
    <bkardell_> ?-
    <eae> fantasai: Making up a completely new unrelated syntax seems silly and doesn't make sense. Cut down on the syntax instead.
    <Rossen> q?
    <eae> dbaron: What doesn't make sense to me is spec moving so far ahead of what implementors are willing to do. Implementor by-in was not a factor.
    <eae> fantasai: Disagree, you're arguing against it based on implementation priority.
    <Chris_> s/by-in/buy-in
    <eae> dbaron: you're acting like we can only do everything or nothing.
    <fantasai> eae, that's not what I said
    <eae> fantasai: Not what I said.
    <fantasai> at all
    <eae> dbaron: I'm fine with the proposal but I do not think it's the only one.
    <eae> leaverou: If implementors aren't willing to do that at the moement shouldn't we discuss that?
    <eae> dbaron: I think I bought this up when we went to CR
    <eae> fantasai: Selectors are not in CR yet.
    <eae> dbaron: But people are goimg ahead to implement it.
    <leaverou> s/aren't willing to do that/aren't willing to implement a part of :matches()/
    <eae> dbaron: This is the problem with sticking things in editors draft.
    <fantasai> There shouldn't be anything in Selectors that didn't have a WG resolution to add.
    <eae> Rossen: Would you prefer to see this go in in another way? If not can we try to settle on a resoluiton and go on.
    <fantasai> If there is, the editors made a mistake.
    <fantasai> If there was a resolution and you didn't like it you should have objected.
    <eae> dbaron: Fine with matches without combinators under a different name.
    <fantasai> Being angry about it now is neither helpful nor fair.
    <bkardell_> q+
    <eae> leaverou: I'm fine with that (adding combinators later)
    <eae> TabAtkins: The no combinators version is the one that desugars effecitvely. Start with simple version.
    <eae> leaverou: Still supports commas, right?
    <Rossen> q?
    <eae> TabAtkins: Yes
    <eae> bkardell_: Is there a suggestion to take combinators out of matches level 4 as well?
    <eae> TabAtkins: Yes
    <eae> Rossen: Are you OK with that?
    <eae> leaverou: Yes, can we have a resolution?
    <dbaron> fwiw, we did discuss moving selectors4 to cr at least in https://lists.w3.org/Archives/Public/www-style/2015Jul/0349.html
    <eae> Proposed resolution: Add to selectors level 4 with a TBD name
    <eae> RESOLVED: Add to selectors level 4 with a TBD name.
    <eae> fantasai: Do we have a list of candidate names?
    <eae> leaverou: is, when, and filter
    <eae> Rossen: Any particular favorite we can resolve on right now?
    <eae> ericwilingers: I like filter
    <eae> Rossen: filter has the most plusses on github?
    <eae> leaverou: No, I think it was either is or when.
    <dbaron> filter is also the name of a property
    <eae> Rossen: You all work it out.
    <eae> Rossen: let's move on.
  20. dbaron commented on Nov 8, 2017

    @dbaron
    Member

    For what it's worth (responding to my own comments in the minutes above), I might be wrong about which aspects of :matches() are hard -- maybe the branching doesn't add that much complexity -- and difficulty with specificity wouldn't be present here, although there may still be issues with implementing the various class/tag/id and bloom filter optimizations to make :matches() equivalent in performance to the old way of saying the same thing. It does appear I had the opposite opinion before (about how bad the branching of combinators resulting from allowing combinators inside :matches() was). But I think it's still a relatively complex feature, and that tying the ability to drop specificity to :matches() does hurt the chances of getting the ability to drop specificity sooner.

  21. o-t-w commented on Nov 28, 2017

    @o-t-w

    :is is the perfect name. Keep it simple and implement as soon as possible 🎉

    The issue raised at the end of the IRC discussion is important. Having a property and a selector named the same thing isn't ideal so I think filter would be a bad choice.

  22. added a commit that references this issue on Dec 3, 2017
  23. fantasai commented on Dec 28, 2017

    @fantasai
    Collaborator

    I filed #2143 for further discussion on the name, as we haven't decided on it.

    I also marked this as an open issue in the draft and named the pseudo-class :something() for now so that it's really obvious that it needs a name.

  24. deleted a comment from mattalxndr on Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    selectors-4

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions