-
Notifications
You must be signed in to change notification settings - Fork 708
[css-selectors] Improve the usefulness of specificity of Tags & IDs #4690
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
This is already the case, isn't it? |
According to the HTML living standard, IDs must be unique on a page:
I think there are very good reasons for that – IDs are used to select a single element on the page in various form/ARIA related uses – so I'm not sure that making IDs more flexible is an option here. and it does make sense to me that a unigue-ID direct selection would live at the highest level of specificity. The same is true for tag names living at the lowest level. The two use-cases that would be particularly helpful in development are:
While point 2 can be handled with multiple classes, it feels like we're missing out on the power that specificity layering would provide to ensure that specific components always override more general patterns, which always override broad defaults. I don't think we need a lot of layers – 3-5 seems about right – but our current 3 don't provide the flexibility required for the common use-cases. |
Right, but what Emilio referred to was that the Selectors spec. also handles the case of invalid HTML documents defining multiple IDs:
Sebastian |
Sure. I guess I'd question if that's really something to encourage? Unique IDs seem important for various semantics/accessibility issues, and I'd want to be careful about pushing a solution that might cause more accessibility issues. |
Of course it's rather something that should be discouraged. As you say, IDs should be unique. I just mentioned that in regard of Jen's point of IDs being used more than once on a page and Emilio's response to it. Sebastian |
@jensimmons Is this something we still want to look into more? I think we've basically covered the use-cases here with a combination of |
If you think it's covered, then it sounds good to me. |
Hi Jen (@jensimmons) and others! I know this thread is closed already, but I don’t find a place where to put my suggestion on how CSS specificity could be calculated to solve a lot of scope problems. We all know the problem when an precise selector (THIS camel is blue) loses against some selector somewhere else (animals on THIS planet are green). My suggestion: Instead of calculating a singular specificity for a CSS selector, the specificity should be calculated regarding the DOM element it is matching with. (How specific does the selector describe the target element.) Example:
A frequent case is that several selectors match for one element, take the li #my-foo for instance:
Now #my-foo gets painted blue. Although the first selector doesn’t mention this specific element, it wins over the second selector which actually is more precise.
The part values then get summated like in the current CSS calculation. That leads to:
Now the precise second selector wins in the b (ID) section of a-b-c-d like it should be, because it is more specific. Every “closer” (DOM-tree-wise) description would win now. Regards, |
@ThomasGutmann That's what https://drafts.csswg.org/css-cascade-6/#scope-combinator is trying to introduce. But you can already have something like that with custom properties: #my-module { --color: blue }
#my-module ul.foos li { color: var(--color); }
#my-foo { --color: red; } |
Thanks, @Loirooriol, for your answer. My point is that the CSS “specificity” is not really inutitiv, and it rewards over-determined selectors and leads to HTML code wich is cluttered with classes and IDs. My website has easy HTML and almost non design and still I stumbled over the problem where I couldn’t style a specific element because of other selectors. |
@ThomasGutmann everything we do here has to be backwards-compatible. We can introduce new features that are unknown in older browsers, but we can't change the behavior of existing CSS syntax. So to that extent, everything we do has to work around what already exists. There would be ways to work around compatibility problems, by introducing new selector syntax – but I would expect that to have the same issues we're facing with (More generally, I would recommend opening new issues for a proposal like this, and linking to prior discussions where they are relevant) |
In an effort to modernize the CSS Cascade, and make it more suitable for projects with multiple Authors writing code over many years, @mirisuzanne is thinking about Origins (and how we might extend their power), and about the Specificity of Selectors.
For context:
The Selectors Level 4 defines three levels of sectors / three levels of specificity:
Miriam wrote on Twitter:
Let's talk about this. What would it look like to extend selectors so that:
• IDs can be used more than once per page
• The 'tag layer' has more customizability
• And / or other ways to empower Authors to use all three layers of specificity, instead of flattening their work into one layer.
The text was updated successfully, but these errors were encountered: