Bird
Raised Fist0
Svelteframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main advantage of using CSS-in-JS patterns in Svelte components?
easy
A. It forces all styles to be global across the app.
B. It requires importing external CSS files for every component.
C. It disables scoped styles by default.
D. Styles stay close to the component's markup and logic, improving maintainability.

Solution

  1. Step 1: Understand CSS-in-JS purpose in Svelte

    CSS-in-JS means writing styles inside the component file, close to markup and logic.
  2. Step 2: Identify the benefit of this approach

    This closeness improves maintainability by keeping related code together and avoiding global style conflicts.
  3. Final Answer:

    Styles stay close to the component's markup and logic, improving maintainability. -> Option D
  4. Quick Check:

    CSS-in-JS = Styles close to component [OK]
Hint: Remember CSS-in-JS keeps styles inside components [OK]
Common Mistakes:
  • Thinking CSS-in-JS requires external CSS files
  • Assuming styles become global automatically
  • Confusing scoped styles with global styles
2. Which of the following is the correct way to write scoped CSS inside a Svelte component?
easy
A.
B. h1 { color: red; }
C.
D.

Solution

  1. Step 1: Identify correct tag for scoped styles in Svelte

    Svelte uses the <style> tag inside components for scoped CSS by default.
  2. Step 2: Eliminate incorrect tags

    <style global> makes styles global, <css> and <script> are invalid for CSS.
  3. Final Answer:

    <style> h1 { color: red; } </style> -> Option C
  4. Quick Check:

    Scoped CSS uses <style> tag [OK]
Hint: Use <style> for scoped CSS, not
Common Mistakes:
  • Using
  • Writing CSS inside <script> tags
  • Using non-existent <css> tags
3. Consider this Svelte component snippet:
<script>
  let color = 'blue';
</script>

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

What color will the text "Hello" appear as when rendered?
medium
A. Blue
B. Green
C. Black
D. Red

Solution

  1. Step 1: Check the variable value

    The variable 'color' is set to 'blue' in the script.
  2. Step 2: Understand inline style binding

    The div's style attribute uses {color}, so it applies 'color: blue;'.
  3. Final Answer:

    Blue -> Option A
  4. Quick Check:

    Inline style uses variable 'color' = blue [OK]
Hint: Inline style uses JS variable value directly [OK]
Common Mistakes:
  • Assuming default black color instead of variable
  • Confusing CSS property with variable name
  • Forgetting to use curly braces for JS in style
4. Identify the error in this Svelte component CSS-in-JS snippet:
<script>
  let bg = 'yellow';
</script>

<div style="background-color: bg;">Box</div>
medium
A. Variable 'bg' should be declared with const, not let.
B. Missing curly braces around 'bg' in style attribute.
C. CSS property 'background-color' is invalid in Svelte.
D. Style attribute must be written inside <style> tag.

Solution

  1. Step 1: Check how JS variables are used in inline styles

    JS variables must be wrapped in curly braces inside style attributes to interpolate values.
  2. Step 2: Identify the missing curly braces

    The style uses "background-color: bg;" but should be "background-color: {bg};" to apply the variable.
  3. Final Answer:

    Missing curly braces around 'bg' in style attribute. -> Option B
  4. Quick Check:

    Use {variable} inside style attribute [OK]
Hint: Wrap JS variables in {} inside style attributes [OK]
Common Mistakes:
  • Forgetting curly braces for JS in style
  • Thinking CSS properties are invalid in Svelte
  • Believing style must be only in <style> tag
5. You want to create a Svelte component that changes text color dynamically based on a prop isError. Which approach correctly applies CSS-in-JS to achieve this?
hard
A.
<script>
  export let isError = false;
</script>

<p style="color: {isError ? 'red' : 'black'}">Message</p>
B.
<script>
  export let isError = false;
</script>

<p style="color: isError ? 'red' : 'black';">Message</p>
C.
<script>
  export let isError = false;
</script>

<style>
  .error { color: red; }
  .normal { color: black; }
</style>

<p class="normal">Message</p>
D.
<script>
  export let isError = false;
</script>

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

<p>Message</p>

Solution

  1. Step 1: Understand dynamic inline style syntax in Svelte

    To use JS expressions inside style attributes, wrap them in curly braces.
  2. Step 2: Check each option's style usage

    <script>
      export let isError = false;
    </script>
    
    <style>
      .error { color: red; }
      .normal { color: black; }
    </style>
    
    <p class="normal">Message</p>
    uses CSS classes but statically applies class='normal', ignoring the isError prop.
    <script>
      export let isError = false;
    </script>
    
    <p style="color: isError ? 'red' : 'black';">Message</p>
    misses curly braces around JS expression.
    <script>
      export let isError = false;
    </script>
    
    <p style="color: {isError ? 'red' : 'black'}">Message</p>
    correctly uses curly braces with a JS ternary expression inside style attribute.
    <script>
      export let isError = false;
    </script>
    
    <style>
      p { color: red; }
    </style>
    
    <p>Message</p>
    applies static red color always.
  3. Final Answer:

    <p style="color: {isError ? 'red' : 'black'}">Message</p> -> Option A
  4. Quick Check:

    Dynamic style needs {JS expression} inside style attribute [OK]
Hint: Use {condition ? 'red' : 'black'} inside style attribute [OK]
Common Mistakes:
  • Forgetting curly braces for JS in style attribute
  • Using static classes instead of dynamic styles
  • Applying styles without condition logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard