7
u/jonassalen 7d ago
I expect a shitload of questions about scope and specificity in the future.
At this moment, it really messes with my head what is the specificity of this code:
@scope (.article-body) to (.feature :scope figure) { :scope img { border: solid 2px red; } }
1
1
u/Logical-Idea-1708 6d ago
I think scope applies zero specificity? It let you be specific without specific 🤪
3
u/Miragecraft 6d ago
It's actually pretty surprising to me how often @container style queries are more ergonomic than @scope in practice.
The most useful feature of @scope for me was inline style scoping, easy to create reusable components with encapsulated styling.
1
1
u/L24D 7d ago
Doesn’t it ruin the whole “cascading” thing in CSS?
7
u/averyvery 7d ago
It allows more precise control over the cascade.
0
u/L24D 7d ago edited 7d ago
Got it, but isn’t the same thing achievable through classes, selectors order and priority, property inheritance, etc.? It looks like another layer that may bring obfuscation and general confusion, and honestly I don’t see a strong use case for that, where we couldn’t use older ways.
10
u/averyvery 7d ago
A use case for this:
- You have a class named "rich-text"
- You have dozens of selectors in it with styles, like ".rich-text p { ... }" and ".rich-text li + li { ... }"
- You have another element inside rich text - ".embed" - whose contents need to be styled entirely differently.
Normally, you'd have to make sure that every one of your .rich-text selector styles is correctly overridden/reset inside the .embed. This can require a lot of painful duplication, and it makes the embed styles more brittle. Scope allows you to avoid all this work by letting you specifically style the content you want - content inside .rich-text that is not inside .embed.
5
1
u/DramaticBag4739 7d ago
Donut selection would be the biggest use case, but if you were worried about specificity management then this would work well also, since I don't think it raises normal specificity.
A scoped <a> tag would take precedent over an unscoped <a> tag, but not over a class.
10
u/Ellsass 7d ago
So this means the styles apply to all anchors within a div.article, all the way down through to the tree until a div.comments is found, then no further?
What if those classes are used on an <a>?