0
0
Svelteframework~20 mins

Global styles in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Global Styles Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does global style affect nested components in Svelte?

Consider a Svelte app with a global style defined in app.css and imported in App.svelte. How will the global style affect nested components?

Svelte
/* app.css */
body {
  background-color: lightblue;
}

/* App.svelte */
<script>
  import './app.css';
  import Child from './Child.svelte';
</script>
<main>
  <Child />
</main>
AThe global style applies to all nested components, changing the background color of the entire app.
BThe global style only applies to <code>App.svelte</code> and does not affect nested components.
CThe global style applies only if nested components explicitly import the CSS file again.
DGlobal styles are ignored in Svelte; styles must be scoped inside each component.
Attempts:
2 left
💡 Hint

Think about how CSS works in the browser and how Svelte handles style imports.

📝 Syntax
intermediate
2:00remaining
Which syntax correctly defines a global style in a Svelte component?

In Svelte, how do you write a style block that applies styles globally instead of scoping them to the component?

Svelte
<script>
  // No script needed
</script>
<style>
  /* Your style here */
</style>
A<style scoped>h1 { color: red; }</style>
B<style lang="global">h1 { color: red; }</style>
C<style global>h1 { color: red; }</style>
D<style :global>h1 { color: red; }</style>
Attempts:
2 left
💡 Hint

Remember the special attribute Svelte uses to mark global styles inside a component.

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

Given the following Svelte component, why does the button not get the red background as expected?

Svelte
<style>
  button {
    background-color: red;
  }
</style>
<button>Click me</button>
AThe style is scoped and does not apply globally, but since the button is inside the component, it should apply.
BThe button is a native HTML element and cannot be styled inside Svelte components.
CThe style block is missing the <code>global</code> attribute, so the style is scoped and applies only to this component's button.
DThe style block must be placed inside a <code>&lt;style global&gt;</code> tag to apply to the button.
Attempts:
2 left
💡 Hint

Think about how Svelte scopes styles and how that affects native elements inside the component.

state_output
advanced
2:00remaining
What is the background color after toggling the global style in Svelte?

In a Svelte app, a global style toggles a class on the body element. What background color will the page have after toggling?

Svelte
<script>
  let darkMode = false;
  function toggle() {
    darkMode = !darkMode;
    if (darkMode) {
      document.body.classList.add('dark');
    } else {
      document.body.classList.remove('dark');
    }
  }
</script>
<button on:click={toggle}>Toggle Dark Mode</button>
<style global>
  body { background-color: white; }
  body.dark { background-color: black; }
</style>
AThe background color does not change because the <code>dark</code> class is not defined globally.
BThe background color switches between white and black each time the button is clicked.
CThe background color changes to black once and never switches back.
DThe background color stays white because Svelte does not allow modifying <code>body</code> classes.
Attempts:
2 left
💡 Hint

Consider how global styles and DOM class manipulation work together.

🧠 Conceptual
expert
3:00remaining
Why use :global() selector inside scoped styles in Svelte?

What is the purpose of using the :global() CSS selector inside a scoped style block in Svelte?

Svelte
<style>
  :global(h1) {
    color: green;
  }
  p {
    color: blue;
  }
</style>
AIt applies the style only to <code>h1</code> elements inside this component, but globally to all <code>p</code> elements.
BIt scopes the <code>h1</code> style only to this component, preventing global leakage.
CIt causes a syntax error because <code>:global()</code> cannot be used inside scoped styles.
DIt applies the style to all <code>h1</code> elements globally, ignoring Svelte's style scoping, while <code>p</code> remains scoped.
Attempts:
2 left
💡 Hint

Think about how :global() overrides Svelte's default style scoping.