Skip to content

Commit 20bfbe6

Browse files
author
Rich Harris
committed
style: directive
1 parent b66e90b commit 20bfbe6

File tree

9 files changed

+125
-1
lines changed

9 files changed

+125
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: The style directive
3+
---
4+
5+
As with `class`, you can write your inline `style` attributes literally, because Svelte is really just HTML with fancy bits:
6+
7+
```svelte
8+
/// file: App.svelte
9+
<button
10+
class="card"
11+
+++style="transform: {flipped ? 'rotateY(0)' : ''}; --bg-1: palegoldenrod; --bg-2: black; --bg-3: goldenrod"+++
12+
on:click={() => flipped = !flipped}
13+
>
14+
```
15+
16+
When you have a lot of styles, it can start to look a bit wacky. We can tidy things up by using the `style:` directive:
17+
18+
```svelte
19+
/// file: App.svelte
20+
<button
21+
class="card"
22+
+++ style:transform={flipped ? 'rotateY(0)' : ''}
23+
style:--bg-1="palegoldenrod"
24+
style:--bg-2="black"
25+
style:--bg-3="goldenrod"+++
26+
on:click={() => flipped = !flipped}
27+
>
28+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<script>
2+
let flipped = false;
3+
</script>
4+
5+
<div class="container">
6+
Flip the card
7+
<button
8+
class="card"
9+
style:transform={flipped ? 'rotateY(0)' : ''}
10+
style:--bg-1="palegoldenrod"
11+
style:--bg-2="black"
12+
style:--bg-3="goldenrod"
13+
on:click={() => flipped = !flipped}
14+
>
15+
<div class="front">
16+
<span class="symbol">♠</span>
17+
</div>
18+
<div class="back">
19+
<div class="pattern"></div>
20+
</div>
21+
</button>
22+
</div>
23+
24+
<style>
25+
.container {
26+
display: flex;
27+
flex-direction: column;
28+
gap: 1em;
29+
height: 100%;
30+
align-items: center;
31+
justify-content: center;
32+
perspective: 100vh;
33+
}
34+
35+
.card {
36+
position: relative;
37+
aspect-ratio: 2.5 / 3.5;
38+
font-size: min(1vh, 0.25rem);
39+
height: 80em;
40+
background: var(--bg-1);
41+
border-radius: 2em;
42+
transform: rotateY(180deg);
43+
transition: transform 0.4s;
44+
transform-style: preserve-3d;
45+
padding: 0;
46+
user-select: none;
47+
cursor: pointer;
48+
}
49+
50+
.card.flipped {
51+
transform: rotateY(0);
52+
}
53+
54+
.front, .back {
55+
display: flex;
56+
align-items: center;
57+
justify-content: center;
58+
position: absolute;
59+
width: 100%;
60+
height: 100%;
61+
left: 0;
62+
top: 0;
63+
backface-visibility: hidden;
64+
border-radius: 2em;
65+
border: 1px solid var(--fg-2);
66+
box-sizing: border-box;
67+
padding: 2em;
68+
}
69+
70+
.front {
71+
background: url(./svelte-logo.svg) no-repeat 5em 5em, url(./svelte-logo.svg) no-repeat calc(100% - 5em) calc(100% - 5em);
72+
background-size: 8em 8em, 8em 8em;
73+
}
74+
75+
.back {
76+
transform: rotateY(180deg);
77+
}
78+
79+
.symbol {
80+
font-size: 30em;
81+
color: var(--fg-1);
82+
}
83+
84+
.pattern {
85+
width: 100%;
86+
height: 100%;
87+
background-color: var(--bg-2);
88+
/* pattern from https://projects.verou.me/css3patterns/#marrakesh */
89+
background-image:
90+
radial-gradient(var(--bg-3) 0.9em, transparent 1em),
91+
repeating-radial-gradient(var(--bg-3) 0, var(--bg-3) 0.4em, transparent 0.5em, transparent 2em, var(--bg-3) 2.1em, var(--bg-3) 2.5em, transparent 2.6em, transparent 5em);
92+
background-size: 3em 3em, 9em 9em;
93+
background-position: 0 0;
94+
border-radius: 1em;
95+
}
96+
</style>

content/tutorial/02-advanced-svelte/06-classes/meta.json renamed to content/tutorial/02-advanced-svelte/06-classes-and-styles/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "Classes",
2+
"title": "Classes and styles",
33
"scope": {
44
"prefix": "/src/lib/",
55
"name": "src"

0 commit comments

Comments
 (0)