Performance: Global vs scoped style strategies
This affects page load speed and rendering performance by controlling how CSS is applied and how much style recalculation the browser must do.
<style>
button {
background: blue;
color: white;
}
</style>
<!-- Scoped styles apply only to this component's elements --><style>
:global(button) { background: blue; color: white; }
:global(h1) { font-size: 2rem; }
/* Global styles applied to all matching elements */
</style>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global styles | High (many elements matched) | Multiple reflows if styles change | High due to broad repaint | [X] Bad |
| Scoped styles | Low (limited to component DOM) | Single reflow per component | Lower paint cost | [OK] Good |
<style> block?:global() pseudo selector inside the style block to mark styles as global.:global().<h1> text be?<script>
let active = true;
</script>
<style>
h1 { color: blue; }
:global(h1) { color: red; }
</style>
<h1>Hello</h1>h1 { color: blue; } compiles to something like h1.svelte-abc { color: blue; } which has higher specificity. The :global(h1) { color: red; } compiles to h1 { color: red; }.h1, but the scoped rule has higher specificity (element + class vs element only), so the text will be blue.<style>
global(.title) { font-weight: bold; }
</style>:global() pseudo selector inside the style block to mark styles as global, not just global().global(.title) with :global(.title) to apply the style globally.:global(). Button styles remain scoped by default.global attribute. incorrectly makes button styles global.Pass the test and this pattern moves unknown