0
0
Svelteframework~10 mins

Global vs scoped style strategies in Svelte - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Global vs scoped style strategies
Write styles in <style> tag
Is style scoped?
NoApply styles globally to all matching elements
Yes
Svelte adds unique attribute to elements
Styles apply only to elements with that attribute
Component renders with scoped styles
Other components unaffected by scoped styles
Styles in Svelte can be global or scoped. Scoped styles get a unique attribute to apply only inside the component, while global styles affect all matching elements.
Execution Sample
Svelte
<script>
  // No script needed
</script>

<style>
  p { color: red; }
</style>

<p>Hello</p>
This Svelte component styles the paragraph text red. By default, styles are scoped to this component only.
Execution Table
StepActionStyle TargetEffectNotes
1Parse <style> tagp selectorPrepare style rulesStyles read from component
2Check if styles are scopedYes (default)Add unique attribute to p elementsSvelte adds e.g. svelte-xyz attribute
3Render <p>Hello</p>p[svelte-xyz]Text color set to redOnly this component's p affected
4Other components render <p>p without attributeNo red color appliedScoped styles do not leak
5If style marked :global(p)p selector globallyAll p elements redOverrides scoped behavior
6Component updatesp[svelte-xyz]Style remains scopedScoped styles persist
7Component unmountsRemove p[svelte-xyz]Scoped styles removedNo leftover styles
8End--Component lifecycle ends
💡 Component unmounts, scoped styles removed, global styles remain if used
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 7
styleScopeundefinedunique attribute addedapplied to p elementremoved on unmount
pElementnot renderedrendered with attributestyled redremoved
Key Moments - 3 Insights
Why does the paragraph text turn red only inside this component?
Because Svelte adds a unique attribute to elements and styles, so styles apply only to elements with that attribute (see execution_table step 2 and 3).
What happens if I use :global in my style?
The style applies to all matching elements in the app, ignoring the unique attribute, making it global (see execution_table step 5).
Do scoped styles affect other components?
No, scoped styles only apply inside the component because of the unique attribute, so other components remain unaffected (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what attribute does Svelte add to scope styles?
AA class named 'scoped-style'
BA unique class like svelte-xyz
CAn id attribute 'style-scope'
DNo attribute is added
💡 Hint
Check step 2 in the execution_table where the unique attribute is added
At which step do styles apply only to this component's paragraph?
AStep 3
BStep 5
CStep 1
DStep 7
💡 Hint
Look at step 3 where the paragraph is rendered with the unique attribute and styled
If you change the style to :global(p), what changes in the execution?
AStyles apply only inside the component
BStyles are removed on unmount
CStyles apply globally to all paragraphs
DNo styles are applied
💡 Hint
See step 5 where :global makes styles apply to all matching elements
Concept Snapshot
Svelte styles are scoped by default.
Svelte adds a unique attribute to elements and styles.
Scoped styles apply only inside the component.
Use :global() to write global styles.
Scoped styles do not affect other components.
Scoped styles are removed when component unmounts.
Full Transcript
In Svelte, styles inside a component are scoped by default. This means Svelte adds a unique attribute to the HTML elements and the CSS selectors so that styles apply only inside that component. For example, a paragraph styled red inside one component will not be red in others. If you want styles to be global, you use the :global() modifier in your CSS. Scoped styles are removed when the component is removed from the page, so they do not leak. This keeps styles clean and prevents conflicts between components.