mirror of https://github.com/sveltejs/svelte
parent
61a549e4ec
commit
6a2a944cae
@ -0,0 +1,15 @@
|
||||
<script>
|
||||
let bgOpacity = 0.5;
|
||||
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
|
||||
</script>
|
||||
|
||||
<input type="range" min="0" max="1" step="0.1" bind:value={bgOpacity} />
|
||||
|
||||
<p style="color: {color}; --opacity: {bgOpacity};">This is a paragraph.</p>
|
||||
|
||||
<style>
|
||||
p {
|
||||
font-family: "Comic Sans MS", cursive;
|
||||
background: rgba(255, 62, 0, var(--opacity));
|
||||
}
|
||||
</style>
|
||||
@ -0,0 +1,15 @@
|
||||
<script>
|
||||
let bgOpacity = 0.5;
|
||||
$: color = bgOpacity < 0.6 ? "#000" : "#fff";
|
||||
</script>
|
||||
|
||||
<input type="range" min="0" max="1" step="0.1" bind:value={bgOpacity} />
|
||||
|
||||
<p style:color style:--opacity={bgOpacity}>This is a paragraph.</p>
|
||||
|
||||
<style>
|
||||
p {
|
||||
font-family: "Comic Sans MS", cursive;
|
||||
background: rgba(255, 62, 0, var(--opacity));
|
||||
}
|
||||
</style>
|
||||
@ -0,0 +1,16 @@
|
||||
---
|
||||
title: Inline styles and the style directive
|
||||
---
|
||||
|
||||
Being able to set css properties dynamically is nice, however this can get unwieldly if you have to write a long string, and mistakes like missing any of the semicolons could make the whole string invalid. Therefore, Svelte provides a nicer way to write inline styles with the style directive.
|
||||
|
||||
Change the style attribute of the paragraph to the following:
|
||||
|
||||
```html
|
||||
<p
|
||||
style:color
|
||||
style:--opacity="{bgOpacity}"
|
||||
>
|
||||
```
|
||||
|
||||
Style directives share a few qualities with class directives. You can use a shorthand when the name of the property and the variable are the same. So `style:color="{color}"` can be written as just `style:color`. And just like class directives, the directives will take precedence if you set the same property through a style attribute.
|
||||
Loading…
Reference in new issue