0
0
Svelteframework~5 mins

Passing styles to components (--style-props) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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>&lt;Button bgColor="lightblue" /&gt;</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;
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.
Which of these is a correct way to apply a passed style prop inside a Svelte component?
A<button style="background-color: {bgColor};">Click</button>
B<button style="bgColor: background-color;">Click</button>
C<button style="{bgColor}">Click</button>
D<button style="color: bgColor;">Click</button>
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.
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.
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.