Styles control how your app looks. Global styles affect everything, while scoped styles only change specific parts. This helps keep your design neat and avoids surprises.
Global vs scoped style strategies in Svelte
<style global> /* Global styles */ h1 { color: blue; } </style> <style> /* Scoped styles */ p { color: red; } </style>
In Svelte, styles inside a component are scoped by default, meaning they only apply to that component.
To make styles global, use the :global() selector or place styles in a global CSS file.
<h1> inside this component.<style>
h1 {
color: green;
}
</style>body, not just this component.<style>
:global(body) {
margin: 0;
font-family: sans-serif;
}
</style>global attribute on <style> makes all styles inside global.<style global>
a {
text-decoration: none;
}
</style>This Svelte component shows a button with scoped styles that only affect it. The paragraph below uses a global style, so all paragraphs on the page get the green bold text.
<script> let count = 0; </script> <style> button { background-color: lightblue; border: none; padding: 0.5rem 1rem; cursor: pointer; } /* Scoped style: only this button */ button:hover { background-color: deepskyblue; } /* Global style: applies to all paragraphs */ :global(p) { color: darkgreen; font-weight: bold; } </style> <button on:click={() => count++} aria-label="Increment count"> Clicked {count} times </button> <p>This paragraph is styled globally.</p>
Scoped styles prevent your component styles from leaking out and affecting other parts of your app.
Use global styles carefully to avoid unexpected changes across your app.
Use :global() inside scoped styles to target global elements without losing scoping benefits.
Scoped styles apply only inside the component, keeping styles isolated.
Global styles affect the whole app or page, useful for common design rules.
Svelte makes styles scoped by default, but you can use :global() or global attribute for global styles.
Practice
Solution
Step 1: Understand Svelte's default style behavior
Svelte automatically scopes styles to the component they are written in, preventing them from affecting other components.Step 2: Compare with global styles
Global styles affect the entire app, but Svelte requires explicit marking for global styles, so default is scoped.Final Answer:
Styles apply only to the current component, not affecting others. -> Option BQuick Check:
Scoped styles = component-only styles [OK]
- Thinking styles apply globally without marking
- Confusing scoped with global styles
- Assuming styles must be external
<style> block?Solution
Step 1: Identify how to mark global styles in Svelte
Svelte uses the:global()pseudo selector inside the style block to mark styles as global.Step 2: Check each option
is scoped by default. uses an invalid attribute. mixes scoped attribute (not valid) with :global(). Only correctly uses:global().Final Answer:
<style> :global(.btn) { color: red; } </style> -> Option AQuick Check:
Use :global() for global styles inside <style> [OK]
- Using invalid <style> attributes like global or scoped
- Forgetting to wrap selectors in :global()
- Assuming all styles are global by default
<h1> text be?<script>
let active = true;
</script>
<style>
h1 { color: blue; }
:global(h1) { color: red; }
</style>
<h1>Hello</h1>Solution
Step 1: Understand style precedence in Svelte
The scoped styleh1 { color: blue; }compiles to something likeh1.svelte-abc { color: blue; }which has higher specificity. The:global(h1) { color: red; }compiles toh1 { color: red; }.Step 2: Determine final color
Both rules apply to the component'sh1, but the scoped rule has higher specificity (element + class vs element only), so the text will be blue.Final Answer:
Blue -> Option CQuick Check:
Scoped styles have higher specificity than plain global styles [OK]
- Thinking global styles override scoped styles
- Assuming global styles have higher specificity
- Ignoring the unique class added by Svelte scoping
<style>
global(.title) { font-weight: bold; }
</style>What is the error and how to fix it?
Solution
Step 1: Identify the syntax for global styles in Svelte
Svelte requires the:global()pseudo selector inside the style block to mark styles as global, not justglobal().Step 2: Correct the syntax
Replaceglobal(.title)with:global(.title)to apply the style globally.Final Answer:
Use :global(.title) instead of global(.title) to apply global styles. -> Option DQuick Check:
Global styles need :global() syntax [OK]
- Using global() instead of :global()
- Trying to add global attribute to style tag incorrectly
- Assuming styles are global by default
Solution
Step 1: Identify how to apply global styles selectively
To make paragraph styles global, wrap the selector in:global(). Button styles remain scoped by default.Step 2: Evaluate each option
applies both styles scoped (not global). correctly applies global to paragraphs only. uses invalidglobalattribute. incorrectly makes button styles global.Final Answer:
<style> :global(p) { font-family: Arial; } button { color: blue; } </style> -> Option AQuick Check:
Use :global() only on selectors needing global scope [OK]
- Making all styles global unintentionally
- Using invalid style tag attributes
- Forgetting :global() for global styles
Ready to prove it?
Pass the test and this pattern moves unknown
