Performance: Scoped styles in components
Scoped styles affect rendering speed by limiting CSS application to specific components, reducing style recalculations and layout shifts.
<style> .button { color: red; } </style>
<component> <button class="button">Click</button> </component><style global> .button { color: red; } </style> <component> <button class="button">Click</button> </component>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global styles affecting many elements | High (many nodes matched) | Multiple reflows if styles change | High paint cost due to wide impact | [X] Bad |
| Scoped styles limited to component | Low (only component nodes) | Single reflow scoped to component | Lower paint cost due to limited scope | [OK] Good |
scoped styles in Astro components?scoped attribute inside the <style> tag.<style scoped>. Others misuse tags or attributes.<h1> text be when rendered?<style scoped>
h1 { color: blue; }
</style>
<h1>Hello World</h1>
<style>
h1 { color: red; }
</style>h1 and sets color blue.h1 color red, but scoped styles override global styles inside the component.<style> scoped
p { font-weight: bold; }
</style><style> tag: <style scoped>, not inside the CSS.<style scoped> for component-specific button styles.<style> without scoped attribute.Pass the test and this pattern moves unknown