Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] style directive tutorial (#7161)
- Loading branch information
Showing
14 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions
15
site/content/tutorial/13-advanced-styling/03-inline-styles/app-a/App.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>This is a paragraph.</p> | ||
|
||
<style> | ||
p { | ||
font-family: "Comic Sans MS", cursive; | ||
background: rgba(255, 62, 0, var(--opacity)); | ||
} | ||
</style> |
15 changes: 15 additions & 0 deletions
15
site/content/tutorial/13-advanced-styling/03-inline-styles/app-b/App.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
10 changes: 10 additions & 0 deletions
10
site/content/tutorial/13-advanced-styling/03-inline-styles/text.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: Inline styles | ||
--- | ||
|
||
Apart from adding styles inside style tags, you can also add styles to individual elements using the style attribute. Usually you will want to do styling through CSS, but this can come in handy for dynamic styles, especially when combined with CSS custom properties. | ||
|
||
Add the following style attribute to the paragraph element: | ||
`style="color: {color}; --opacity: {bgOpacity};"` | ||
|
||
Great, now you can style the paragraph using variables that change based on your input without having to make a class for every possible value. |
15 changes: 15 additions & 0 deletions
15
site/content/tutorial/13-advanced-styling/04-style-directive/app-a/App.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
15 changes: 15 additions & 0 deletions
15
site/content/tutorial/13-advanced-styling/04-style-directive/app-b/App.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
18 changes: 18 additions & 0 deletions
18
site/content/tutorial/13-advanced-styling/04-style-directive/text.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: The style directive | ||
--- | ||
|
||
Being able to set CSS properties dynamically is nice. However, this can get unwieldy if you have to write a long string. 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}" | ||
> | ||
``` | ||
|
||
The style directive shares a few qualities with the class directive. 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`. | ||
|
||
Similar to the class directive, the style directive will take precedence if you try to set the same property through a style attribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"title": "Advanced styling" | ||
} |
This file was deleted.
Oops, something went wrong.