What is the purpose of passing styles to Svelte components using --style-props?
It allows you to customize the look of a component by sending CSS style values as properties, making components flexible and reusable without changing their internal code.
Click to reveal answer
beginner
How do you define a style property in a Svelte component to accept a passed style value?
You declare a export let variable in the component script and then use it in the style attribute or CSS variables inside the component.
Click to reveal answer
intermediate
Example: How would you pass a background color to a Svelte button component using style props?
In the parent: <code><Button bgColor="lightblue" /></code>. In the Button component: <code>export let bgColor = 'white';</code> and use <code>style="background-color: {bgColor};"</code> in the button element.
Click to reveal answer
beginner
Why is passing styles as props better than hardcoding styles inside components?
It keeps components flexible and reusable. You can change their appearance without editing their code, which helps maintain clean and scalable projects.
Click to reveal answer
intermediate
What is a common mistake when passing styles to components and how to avoid it?
A common mistake is passing raw CSS strings that break syntax or forgetting to set default values for style props. Avoid this by validating inputs and providing defaults.
Click to reveal answer
How do you declare a style property in a Svelte component to accept a passed style value?
Aconst styleProp = 'value';
Blet styleProp = export;
Cexport let styleProp;
DstyleProp: string;
✗ Incorrect
In Svelte, you use export let to declare a property that can receive values from outside.
What is the benefit of passing styles as props to components?
AIt makes components reusable with different looks.
BIt makes components slower.
CIt hides the component code.
DIt forces all components to have the same style.
✗ Incorrect
Passing styles as props allows you to reuse the same component with different appearances.
Which of these is a correct way to apply a passed style prop inside a Svelte component?
You use curly braces to insert the variable value inside the style attribute.
What should you do to avoid errors when passing style props?
AHardcode all styles inside components.
BNever use style props.
CUse only inline styles without props.
DProvide default values and validate inputs.
✗ Incorrect
Providing defaults and validating helps prevent broken styles and unexpected behavior.
If you want to pass a font size to a component, which is the best way?
APass font size as a number without units.
Bexport let fontSize = '16px'; and use style="font-size: {fontSize};"
CHardcode font size inside the component.
DUse a global CSS file only.
✗ Incorrect
Passing font size as a prop with units and applying it in style is flexible and clear.
Explain how you can pass and use style properties in a Svelte component to customize its appearance.
Think about how variables and style attributes work together in Svelte.
You got /4 concepts.
Describe common mistakes when passing styles to components and how to avoid them.
Consider what can break styles and how to keep components stable.
You got /4 concepts.
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
Step 1: Understand the purpose of style props
Passing styles as props lets you change how a component looks without editing its code.
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.
Final Answer:
It allows customizing the component's appearance without changing its internal code. -> Option C
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
Step 1: Recall Svelte prop syntax
Svelte uses export let to declare props that components accept.
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.
Final Answer:
export let style; -> Option A
Quick Check:
Svelte props use export let [OK]
Hint: Use 'export let' to declare props in Svelte components [OK]
<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
Step 1: Understand style passing
The parent passes a style string with color red and bold font weight to the child as a prop.
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.
Final Answer:
Red color and bold font weight -> Option D
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
Step 1: Check prop declaration
In Svelte, props must be declared with export let to receive values from parents.
Step 2: Identify the issue
The code declares let style; without export, so it stays local and undefined.
Final Answer:
The style prop is not exported, so it won't receive values from the parent. -> Option B
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: