Discover how passing styles as props can save you hours of frustrating CSS fixes!
Why Passing styles to components (--style-props) in Svelte? - Purpose & Use Cases
Imagine you build a button component and want to change its color or size every time you use it in your app by editing CSS files or inline styles everywhere.
Manually changing styles for each component use is tiring, error-prone, and makes your code messy. You might forget to update some styles or create conflicting CSS rules.
Passing styles as props lets you customize components easily by sending style values directly when you use them, keeping your code clean and flexible.
<Button style="color: red; font-size: 20px;">Click me</Button><Button style="--color: red; --fontSize: 20px;">Click me</Button>You can create reusable components that adapt their look instantly without touching their internal code or global styles.
Think of a card component used in many places but needing different background colors or padding depending on the page--passing style props solves this smoothly.
Manual styling for each component use is slow and messy.
Passing styles as props keeps components flexible and clean.
This approach makes your UI easier to maintain and customize.