Bird
Raised Fist0
Svelteframework~20 mins

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

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

Practice

(1/5)
1. What is the main benefit of passing styles as props to a Svelte component?
easy
A. It automatically adds animations to the component.
B. It makes the component run faster.
C. It allows customizing the component's appearance without changing its internal code.
D. It disables the component's default styles permanently.

Solution

  1. Step 1: Understand the purpose of style props

    Passing styles as props lets you change how a component looks without editing its code.
  2. Step 2: Compare options

    Only It allows customizing the component's appearance without changing its internal code. correctly describes this benefit; others mention speed, animations, or disabling styles, which are unrelated.
  3. Final Answer:

    It allows customizing the component's appearance without changing its internal code. -> Option C
  4. Quick Check:

    Style props = customize without code change [OK]
Hint: Style props let you change look without touching component code [OK]
Common Mistakes:
  • Thinking style props improve performance
  • Confusing style props with animations
  • Assuming style props disable default styles
2. Which of the following is the correct way to accept a style prop in a Svelte component?
easy
A. export let style;
B. let style = props.style;
C. const style = this.props.style;
D. function style() {}

Solution

  1. Step 1: Recall Svelte prop syntax

    Svelte uses export let to declare props that components accept.
  2. Step 2: Evaluate options

    export let style; uses correct Svelte syntax. Options B and C use React or other framework styles. function style() {} is a function, not a prop declaration.
  3. Final Answer:

    export let style; -> Option A
  4. Quick Check:

    Svelte props use export let [OK]
Hint: Use 'export let' to declare props in Svelte components [OK]
Common Mistakes:
  • Using React or Vue syntax inside Svelte
  • Declaring props without export
  • Confusing functions with props
3. Given this parent component code:
<ChildComponent style="color: red; font-weight: bold;" />

And this child component code:
  <script>
    export let style;
  </script>
  <div style={style}>Hello</div>

What will be the color and font weight of the text rendered by ChildComponent?
medium
A. Default color and font weight
B. No text will be rendered
C. Blue color and normal font weight
D. Red color and bold font weight

Solution

  1. Step 1: Understand style passing

    The parent passes a style string with color red and bold font weight to the child as a prop.
  2. Step 2: Check child usage

    The child applies the style prop directly to the div's style attribute, so the styles apply to the text.
  3. Final Answer:

    Red color and bold font weight -> Option D
  4. Quick Check:

    Passed style string applies directly [OK]
Hint: Style string passed as prop applies directly to element [OK]
Common Mistakes:
  • Assuming styles won't apply without CSS files
  • Thinking default styles override passed styles
  • Believing text won't render without explicit content
4. What is wrong with this child component code that tries to accept a style prop?
<script>
  let style;
</script>
<div style={style}>Content</div>
medium
A. The div tag is missing a closing tag.
B. The style prop is not exported, so it won't receive values from the parent.
C. The style variable should be a function, not a let variable.
D. The style attribute cannot be bound to variables in Svelte.

Solution

  1. Step 1: Check prop declaration

    In Svelte, props must be declared with export let to receive values from parents.
  2. Step 2: Identify the issue

    The code declares let style; without export, so it stays local and undefined.
  3. Final Answer:

    The style prop is not exported, so it won't receive values from the parent. -> Option B
  4. Quick Check:

    Props need export let [OK]
Hint: Always export props to receive parent values [OK]
Common Mistakes:
  • Forgetting to export props
  • Thinking style binding is invalid
  • Misreading HTML tag syntax
5. You want to create a reusable Button component that accepts custom styles from its parent. Which approach correctly passes and applies styles while keeping default styles intact?

Choose the best code snippet for the Button.svelte component:
hard
A.
<script>
  export let style = '';
</script>
<button style={`background: blue; color: white; ${style}`}>Click me</button>
B.
<script>
  let style;
</script>
<button style={style}>Click me</button>
C.
<script>
  export let style;
</script>
<button class={style}>Click me</button>
D.
<script>
  export let style;
</script>
<button>Click me</button>

Solution

  1. Step 1: Understand default and custom styles

    The component should have default styles but allow extra styles from the parent.
  2. Step 2: Analyze options

    <script>
      export let style = '';
    </script>
    <button style={`background: blue; color: white; ${style}`}>Click me</button>
    sets a default style string and appends the passed style, combining both.
    <script>
      let style;
    </script>
    <button style={style}>Click me</button>
    misses export and default styles.
    <script>
      export let style;
    </script>
    <button class={style}>Click me</button>
    uses class binding incorrectly for styles.
    <script>
      export let style;
    </script>
    <button>Click me</button>
    ignores the style prop.
  3. Final Answer:

    export let style = ''; button with combined style string -> Option A
  4. Quick Check:

    Combine default + passed styles with export let [OK]
Hint: Combine default and passed styles in template string [OK]
Common Mistakes:
  • Not exporting style prop
  • Overwriting default styles instead of combining
  • Using class instead of style attribute for inline styles
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