-
Notifications
You must be signed in to change notification settings - Fork 715
[css-specificity] Add specificity property modifier (replace !important) #3890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think the overall idea is intriguing. But using CSS variables inside an importance modifier wouldn't work — you need to sort out specificity & the cascade earlier in the process than variable substitution. |
It's worth noting that we already have a specificity adjustment mechanism, and there was a proposal to extend it with an optional parameter for explicit specificity setting. However, the specificity itself is only the 3rd styles priority criterion in the cascade, following origin/importance and scope, so even the hypothetical |
That's a very good idea, though it's independent of your proposal for allowing to modify the specificity. For reference, there exists already a request for this for the Firefox DevTools. Sebastian |
The proposal looks good to me. Especially if the argument is a constant number and not a var. However, the issue with this proposal is that is not polyfillable and thus it'd take quite a long time before it can be used freely across the Web. Currently the main tool available to us is "keep CSS selectors as simple as possible and order CSS rules correctly". In a naive case, if all CSS selectors were a single class ( For instance, the following constraint could help: the |
I think polyfillability of this feature is important. It should be defined in a way that toolchains Maybe we could support specificity on a per-stylesheet basis rather than per-selector? |
@chrishtr I actually think if the |
@dvoytenko Just to clarify, specificity of properties is a 5-dimensional value: If I have an inline rule of My proposal—and of course it could be altered—is to give a way to freely manipulate the first member of the specificity set. Right now it has binary functionality, with only I used the word As for backward-compatibility, people could potentially be encouraged to use
|
Not exactly. It's often taught to the beginners that way for simplicity, but the CSS Cascade spec defines the "Origin and Importance" and "Specificity" as two separate criteria of the declaration precedence. Adding "!important" effectively changes the first one, but even So adding only extra importance levels without adding new "origins" would not allow, e.g., making a declaration's priority higher than any non-important author style (to prevent accidental overriding) but lower than animation declarations (to keep them animatable). |
For general adjustment of a selector's specificity, that's the planned additional arguments to !important isn't technically part of specificity; it controls a declaration's cascade origin. Custom Cascade Origins are the current plan to allow adjusting that aspect; the WG accepted work on it at the last f2f, and we should have a good spec out of it soon, once we resolve a few issues. I'll go ahead and close this issue, then, since #4470 now covers the topic. |
Currently there is the ability to hoist a property's specificity beyond that of its selector via
!important
. This often leads to people writing labyrinthine selectors, then slapping!important
on the properties to override a previous developer's styles.What if where you put
!important
, you could put!specificity(10)
? What if in developer tools, every rule/property showed its specificity in number form? This would effectively change the esoteric nature of specificity to something much easier to determine and comprehend. It would be very clear to see, "Oh, my rule isn't being applied because it's of specificity 2 and there's another one of specificity 5."Let's say that you want to ensure your
.hidden
class always hides the element it's applied to. In your stylesheet you could have something like:Or imagine switching between light and dark modes. All you would have to do is change the specificity variable of the dark to be greater or less than that of the light.
Or say you want to set the specificity for an entire selector:
It would make specificity akin to how z-index is now. If you can't see your element, it's pretty straightforward to compare z-indices. One potential issue is that it opens specificity up to abuse (e.g.
!specificity(100000000)
. That being said, I think it's mild compared to the abuse it currently suffers. I personally prefer a straight number over trying to parse my way through selector chains and!important
s.EDIT I know that there are 5 levels of specificity. This proposal is specifically being able to assign a number to
!important
.The text was updated successfully, but these errors were encountered: