[Draft] Respected Authored CSS Specificity #22
jonathantneal
started this conversation in
Proposal
Replies: 1 comment 1 reply
-
This would be great. Maybe in the meantime we can update the Astro docs to make clear this gotcha (e.g. in the build a blog tutorial and the styling docs). In fact the docs seem to suggest this problem doesn't exist. I also thought of modifying the astro plugin to inline the scoped style so that it's easier to calculate the specificity. I don't know how difficult would this be, but I'm happy to try if there is interest for this |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Respected CSS Specificity Proposal
Astro should respect the authored specificity when generating scoped CSS.
Background
Astro component styles are scoped by default, meaning they only apply to items in the component.
These are exclusive styling boundaries that prevent styles from applying to items outside the component or within child components.
To accomplish this, Astro adds an additional class to all elements and selectors, increasing specificity from
O+1
toO(1)+1
.See an example of the current behavior.
This specificity increase is perceptually inconsistent to authors, without a deeper knowledge of the Astro internals and Type, ID, add Class selector specificity.
This specificity increase makes it hard to combine with other CSS files or other styling libraries (Tailwind, CSS Modules, Styled Components, Stitches), as those systems would not include our additional side-effects.
Goals
Non Goals
Browser support
Specificity increases on purpose
Using classes vs data attributes
Controlling the order of styles
Solution
Astro can prevent this specificity increase by wrapping the scoping mechanism in a
:where()
CSS pseudo-class.By using
:where()
, styles would now:See an example of the proposed behavior.
Detailed Design
Example of the Current Behavior
In this example, the HTML and CSS will generally transform into the following code:
p
p[data-astro-scope="OZV3B5RX"]
0.0.1
0.1.1
aside p
aside[data-astro-scope="OZV3B5RX"] p[data-astro-scope="OZV3B5RX"]
0.0.2
0.2.2
:global(aside) p
aside p[data-astro-scope="OZV3B5RX"]
0.0.2
0.1.2
Example of the Proposed Behavior
In this example, the HTML and CSS will generally transform into the following code:
p
p:where([data-astro-scope="OZV3B5RX"])
0.0.1
0.0.1
aside p
aside:where([data-astro-scope="OZV3B5RX"]) p:where([data-astro-scope="OZV3B5RX"])
0.0.2
0.0.2
:global(aside) p
aside p:where([data-astro-scope="OZV3B5RX"])
0.0.2
0.0.2
Open Questions
Is there another way to increase the specificity of component styles without the unexpected behaviors that were mentioned?
Beta Was this translation helpful? Give feedback.
All reactions