0
0
Svelteframework~20 mins

CSS-in-JS patterns with Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSS-in-JS Svelte Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered style output of this Svelte component using CSS-in-JS?

Consider this Svelte component that uses a <style> tag with a dynamic CSS variable:

<script>
  export let color = 'blue';
</script>

<style>
  div {
    color: var(--text-color);
  }
</style>

<div style="--text-color: {color};">Hello</div>

What color will the text inside the <div> be when rendered?

Svelte
<script>
  export let color = 'blue';
</script>

<style>
  div {
    color: var(--text-color);
  }
</style>

<div style="--text-color: {color};">Hello</div>
AThe text color will be transparent.
BThe text color will be black (default).
CThe text color will be red.
DThe text color will be blue.
Attempts:
2 left
💡 Hint

Look at how the CSS variable --text-color is set inline on the div.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies scoped dynamic styles in Svelte using CSS-in-JS?

You want to apply a dynamic background color to a <button> using CSS-in-JS in Svelte. Which code snippet correctly does this?

A<button style="background-color: {bgColor};">Click me</button>
B<button style="background-color: bgColor;">Click me</button>
C<button style="background-color: ${bgColor};">Click me</button>
D<button style="background-color: var(bgColor);">Click me</button>
Attempts:
2 left
💡 Hint

Remember how to insert JavaScript variables inside Svelte attribute bindings.

🔧 Debug
advanced
2:00remaining
Why does this CSS-in-JS style not apply in Svelte?

Given this Svelte component:

<script>
  let color = 'green';
</script>

<style>
  .text {
    color: color;
  }
</style>

<p class="text">Hello</p>

Why is the text color not green?

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

<style>
  .text {
    color: color;
  }
</style>

<p class="text">Hello</p>
AThe <style> tag must be inside the <script> tag for variables to work.
BThe class name 'text' is invalid in Svelte styles.
CCSS cannot use JavaScript variables directly; 'color' is treated as a literal string, not the variable.
DThe color variable must be declared as export for CSS to access it.
Attempts:
2 left
💡 Hint

Think about how CSS and JavaScript variables interact in Svelte.

state_output
advanced
2:00remaining
What is the background color after clicking the button twice in this Svelte CSS-in-JS example?

Consider this Svelte component:

<script>
  import { writable } from 'svelte/store';
  const bg = writable('white');

  function toggle() {
    bg.update(c => c === 'white' ? 'black' : 'white');
  }
</script>

<button on:click={toggle} style="background-color: {$bg};">Toggle</button>

What is the background color of the button after clicking it twice?

Svelte
<script>
  import { writable } from 'svelte/store';
  const bg = writable('white');

  function toggle() {
    bg.update(c => c === 'white' ? 'black' : 'white');
  }
</script>

<button on:click={toggle} style="background-color: {$bg};">Toggle</button>
Ablack
Bwhite
Cgray
Dtransparent
Attempts:
2 left
💡 Hint

Each click toggles the color between white and black.

🧠 Conceptual
expert
3:00remaining
Which statement best describes CSS-in-JS pattern limitations in Svelte compared to React?

Choose the statement that correctly explains a limitation or difference of CSS-in-JS in Svelte compared to React frameworks.

ASvelte does not support scoped CSS-in-JS libraries like styled-components because styles are compiled separately, requiring inline styles or CSS variables for dynamic styling.
BSvelte fully supports all React CSS-in-JS libraries without any changes because both use virtual DOM.
CSvelte requires all CSS-in-JS to be written inside the <script> tag as JavaScript objects.
DSvelte automatically converts all CSS-in-JS styles into global styles, so scoping is impossible.
Attempts:
2 left
💡 Hint

Think about how Svelte compiles components and handles styles differently from React.