Consider a Svelte component with styles defined inside a <style> tag without the global attribute. What happens to these styles?
<script> let color = 'red'; </script> <style> p { color: red; } </style> <p>This text is styled.</p>
Think about how Svelte isolates styles by default to avoid conflicts.
By default, Svelte scopes styles to the component they are defined in. This means styles inside a <style> tag without global only affect that component's elements.
In Svelte, how do you write CSS inside a component so that it applies globally to the whole app?
Remember Svelte uses a special :global() selector inside styles for global CSS.
In Svelte, to apply global styles inside a component's style block, you wrap selectors with :global().
Given this Svelte component code, why does the h1 outside the component not turn green?
<style>
h1 { color: green; }
</style>
<h1>This is inside component</h1>Think about how Svelte isolates styles by default.
Svelte scopes styles by default. Without :global(), styles only apply inside the component. So h1 outside remains unaffected.
In this Svelte component, what color will the paragraph text show after clicking the button once?
<script> let isGlobal = false; function toggle() { isGlobal = !isGlobal; } </script> <style> p { color: red; } :global(.global-style) { color: blue; } </style> <p class={isGlobal ? 'global-style' : ''}>Hello!</p> <button on:click={toggle}>Toggle Style</button>
Check how the class changes and how global styles apply.
When the button is clicked, isGlobal becomes true, adding the global-style class. The global CSS sets color: blue for that class, so the paragraph text turns blue.
Which reason best explains why scoped styles are generally preferred over global styles in large Svelte applications?
Think about how CSS conflicts happen in big projects.
Scoped styles isolate CSS to components, avoiding conflicts and making it easier to maintain and reason about styles in large apps. Global styles can cause unintended overrides and bugs.