0
0
Svelteframework~20 mins

Scoped styles by default in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scoped Styles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How do scoped styles behave in Svelte components?

Consider two Svelte components, Parent.svelte and Child.svelte. Parent.svelte has a <style> block defining p { color: red; }. If Parent renders Child, what color will the paragraph text inside Child be?

Svelte
<!-- Parent.svelte -->
<p>This is parent text.</p>
<Child />
<style>
  p { color: red; }
</style>

<!-- Child.svelte -->
<p>This is child text.</p>
ABoth paragraphs will be red because styles cascade globally.
BOnly the parent paragraph will be red; the child paragraph uses default color because styles are scoped.
COnly the child paragraph will be red; the parent paragraph uses default color because styles are scoped.
DNeither paragraph will be red because styles are ignored in child components.
Attempts:
2 left
💡 Hint

Think about how Svelte applies styles only to the component they are declared in.

📝 Syntax
intermediate
1:30remaining
Which style block syntax scopes styles in Svelte?

Which of the following <style> blocks in a Svelte component will scope styles only to that component?

A
&lt;style&gt;
p { color: blue; }
&lt;/style&gt;
B
&lt;style global&gt;
p { color: blue; }
&lt;/style&gt;
C
&lt;style scoped&gt;
p { color: blue; }
&lt;/style&gt;
D
&lt;style module&gt;
p { color: blue; }
&lt;/style&gt;
Attempts:
2 left
💡 Hint

In Svelte, styles are scoped by default without extra attributes.

🔧 Debug
advanced
2:00remaining
Why does this style leak outside the component?

Given this Svelte component, why does the h1 style affect elements outside this component?

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

<h1>Title</h1>
ABecause the <code>h1</code> tag is outside the component's root element.
BBecause Svelte does not support scoped styles for <code>h1</code> elements.
CBecause the style block is missing the <code>scoped</code> attribute.
DBecause the <code>global</code> attribute makes styles apply globally, ignoring scoping.
Attempts:
2 left
💡 Hint

Check the effect of the global attribute on styles.

state_output
advanced
2:00remaining
What color will the paragraph be after toggling the class?

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

Svelte
<script>
  let active = false;
  function toggle() {
    active = !active;
  }
</script>

<p class:highlight={active}>Hello!</p>
<button on:click={toggle}>Toggle</button>

<style>
  p { color: black; }
  .highlight { color: orange; }
</style>
AThe paragraph text will be orange.
BThe paragraph text color will not change.
CThe paragraph text will be invisible.
DThe paragraph text will be black.
Attempts:
2 left
💡 Hint

Consider how the class directive class:highlight works with the active variable.

🧠 Conceptual
expert
2:30remaining
How does Svelte ensure styles are scoped to components?

Which mechanism does Svelte use internally to scope styles to components by default?

AIt uses JavaScript to dynamically inject inline styles at runtime.
BIt wraps each component in an iframe to isolate styles.
CIt adds unique attribute selectors to HTML elements and styles during compilation.
DIt requires developers to manually prefix all CSS selectors.
Attempts:
2 left
💡 Hint

Think about how Svelte compiles components and applies styles.