0
0
Svelteframework~20 mins

Passing styles to components (--style-props) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Style Prop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What style will the button have?

Given this Svelte component usage, what will be the background color of the button?

<Button style="background-color: red;" />

Assume the Button component uses --style-props to pass styles.

Svelte
/* Button.svelte */
<script>
  export let style = "";
</script>
<button style={style}>Click me</button>
AThe button background will be red.
BThe button background will be default (no red).
CThe button will have a syntax error and not render.
DThe button background will be blue.
Attempts:
2 left
💡 Hint

Think about how inline styles passed as props affect the element.

📝 Syntax
intermediate
2:00remaining
Which option correctly passes multiple CSS variables as style props?

In Svelte, you want to pass two CSS custom properties --main-color and --padding to a component using --style-props. Which syntax is correct?

A<Component style={"--main-color: blue; --padding: 1rem;"} />
B<Component style={{ '--main-color': 'blue', '--padding': '1rem' }} />
C<Component style='--main-color: blue; --padding: 1rem;' />
D<Component style="--main-color: blue; --padding: 1rem;" />
Attempts:
2 left
💡 Hint

Remember how HTML inline styles are written as strings.

state_output
advanced
2:00remaining
What is the final color of the text after clicking the button twice?

Consider this Svelte component that toggles a CSS variable passed as a style prop:

<script>
  let isRed = true;
</script>

<div
  style={isRed ? '--text-color: red;' : '--text-color: blue;'}
>
  <button
    on:click={() => isRed = !isRed}
  >Toggle Color</button>

  <p style="color: var(--text-color);">Colored Text</p>
</div>

What color will the "Colored Text" be after clicking the button twice?

ARed
BBlue
CNo color (default black)
DSyntax error prevents rendering
Attempts:
2 left
💡 Hint

Each click toggles the CSS variable between red and blue.

🔧 Debug
advanced
2:00remaining
Why does this style prop not apply the background color?

Given this Svelte component usage:

<Card style="background-color: green" />

And the Card.svelte component:

<script>
  export let style = "";
</script>

<div style="{style}">Content</div>

Why does the background color not appear green?

AThe background-color property is invalid CSS and ignored.
BThe style prop is not passed down because it is not declared as export.
CThe style attribute in the div uses quotes incorrectly, so the style string is not applied.
DThe component needs to use a class instead of style prop.
Attempts:
2 left
💡 Hint

Check how the style attribute is bound in the component.

🧠 Conceptual
expert
3:00remaining
How does using --style-props improve component styling flexibility in Svelte?

Choose the best explanation for why passing styles via --style-props is beneficial in Svelte components.

AIt automatically scopes styles globally, so no CSS conflicts occur anywhere in the app.
BIt allows parent components to customize CSS variables dynamically, enabling theme and style changes without modifying component internals.
CIt disables component reactivity to style changes, making rendering faster.
DIt forces all styles to be inline, which improves performance by avoiding CSS files.
Attempts:
2 left
💡 Hint

Think about how CSS variables and style props help with theming.