diff --git a/documentation/docs/03-template-syntax/17-style.md b/documentation/docs/03-template-syntax/17-style.md index 749376c6e2..226359ebb4 100644 --- a/documentation/docs/03-template-syntax/17-style.md +++ b/documentation/docs/03-template-syntax/17-style.md @@ -1,7 +1,75 @@ --- -title: style: +title: style and style: --- +There are two ways to set styles on elements: the `style` attribute, and the `style:` directive. + +## Attributes + +Primitive values are treated like any other attribute: + +```svelte +
...
+``` + +### Objects and arrays + +Since Svelte 5.XX, `style` can be an object or array, and is converted to a string according to the following rules : + +If the value is an + +If the value is an object, the key/value are converted to CSS properties if the value is not-null and not-empty. + +```svelte + +
...
+``` + +> [!NOTE] +> The CSS properties are case-insensitive and use `kebab-case`, which requires quoting key's name in JavaScript. +> In order to avoid this, object keys will be 'converted' according to the following rules : +> * Uppercase keys like `COLOR` will be converted to the lowercase format `color`. +> * `camelCase` keys like `fontSize` will be converted to the kebab-case format `font-size`. +> * `snake_case` keys like `border_color` will be converted to the kebab-case format `border-color`. +> Note that this will not apply to key that starts with a double hyphens, because CSS variable don't have naming rules and are case-sensitive (`--myvar` is different from `--myVar`). +> But we can use a double underscores to enable the same rules. Ex: `__myVar` or `__my_var` will be converted to `--my-var`. + +If the value is an array, the truthy values are combined, string are passed without change, and array/objects are flatten : + +```svelte + +
...
+``` + +This is useful for combining local styles with props, for example: + +```svelte + + + + +``` + + +Svelte also exposes the `StyleValue` type, which is the type of value that the `style` attribute on elements accept. This is useful if you want to use a type-safe class name in component props: + +```svelte + + +
...
+``` + + +## The `style:` directive + The `style:` directive provides a shorthand for setting multiple styles on an element. ```svelte