0
0
Svelteframework~20 mins

Global vs scoped style strategies in Svelte - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Style Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does scoped styling affect component styles in Svelte?

Consider a Svelte component with styles defined inside a <style> tag without the global attribute. What happens to these styles?

Svelte
<script>
  let color = 'red';
</script>

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

<p>This text is styled.</p>
AThe styles apply globally to all <code>&lt;p&gt;</code> elements in the app.
BThe styles apply only to elements inside this component, not affecting other components.
CThe styles are ignored and do not apply anywhere.
DThe styles apply only to elements outside this component.
Attempts:
2 left
💡 Hint

Think about how Svelte isolates styles by default to avoid conflicts.

📝 Syntax
intermediate
2:00remaining
Which syntax applies global styles in a Svelte component?

In Svelte, how do you write CSS inside a component so that it applies globally to the whole app?

A<style> :global(p) { color: blue; } </style>
B<style scoped> p { color: blue; } </style>
C<style> p { color: blue; } </style>
D<style global> p { color: blue; } </style>
Attempts:
2 left
💡 Hint

Remember Svelte uses a special :global() selector inside styles for global CSS.

🔧 Debug
advanced
2:00remaining
Why does this global style not apply outside the component?

Given this Svelte component code, why does the h1 outside the component not turn green?

Svelte
<style>
  h1 { color: green; }
</style>

<h1>This is inside component</h1>
ABecause styles without <code>:global()</code> are scoped and do not affect elements outside the component.
BBecause the <code>h1</code> tag is invalid inside <code>&lt;style&gt;</code>.
CBecause Svelte requires styles to be in a separate CSS file to apply globally.
DBecause the component is missing a <code>global</code> attribute on the <code>&lt;style&gt;</code> tag.
Attempts:
2 left
💡 Hint

Think about how Svelte isolates styles by default.

state_output
advanced
2:00remaining
What color will the paragraph text be after clicking the button?

In this Svelte component, what color will the paragraph text show after clicking the button once?

Svelte
<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>
ABlack, because no style applies after toggle.
BRed, because scoped styles override global styles.
CNo color change, the text remains red.
DBlue, because the class 'global-style' applies a global blue color.
Attempts:
2 left
💡 Hint

Check how the class changes and how global styles apply.

🧠 Conceptual
expert
2:00remaining
Why prefer scoped styles over global styles in Svelte for large apps?

Which reason best explains why scoped styles are generally preferred over global styles in large Svelte applications?

AScoped styles require less CSS code overall, reducing file size significantly.
BGlobal styles load faster because they are cached once and reused everywhere.
CScoped styles prevent style conflicts by isolating CSS to components, making maintenance easier.
DGlobal styles automatically update component state, improving reactivity.
Attempts:
2 left
💡 Hint

Think about how CSS conflicts happen in big projects.