Skip to content

Commit ff4b193

Browse files
committed
more
1 parent 9bfce44 commit ff4b193

File tree

5 files changed

+185
-235
lines changed

5 files changed

+185
-235
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: Scoped styles
3+
---
4+
5+
Svelte components can include a `<style>` element containing CSS that belongs to the component. This CSS is _scoped_ by default, meaning that styles will not apply to any elements on the page outside the component in question.
6+
7+
This works by adding a class to affected elements, which is based on a hash of the component styles (e.g. `svelte-123xyz`).
8+
9+
```svelte
10+
<style>
11+
p {
12+
/* this will only affect <p> elements in this component */
13+
color: burlywood;
14+
}
15+
</style>
16+
```
17+
18+
## Specificity
19+
20+
Each scoped selector receives a [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) increase of 0-1-0, as a result of the scoping class (e.g. `.svelte-123xyz`) being added to the selector. This means that (for example) a `p` selector defined in a component will take precedence over a `p` selector defined in a global stylesheet, even if the global stylesheet is loaded later.
21+
22+
In some cases, the scoping class must be added to a selector multiple times, but after the first occurrence it is added with `:where(.svelte-xyz123)` in order to not increase specificity further.
23+
24+
## Scoped keyframes
25+
26+
If a component defines `@keyframes`, the name is scoped to the component using the same hashing approach. Any `animation` rules in the component will be similarly adjusted:
27+
28+
```svelte
29+
<style>
30+
.bouncy {
31+
animation: bounce 10s;
32+
}
33+
34+
/* these keyframes are only accessible inside this component */
35+
@keyframes bounce {
36+
/* ... *.
37+
}
38+
</style>
39+
```
40+
41+
42+

documentation/docs/04-styling/01-styles-and-classes.md

Lines changed: 0 additions & 235 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Global styles
3+
---
4+
5+
## :global(...)
6+
7+
To apply styles to a single selector globally, use the `:global(...)` modifier:
8+
9+
```svelte
10+
<style>
11+
:global(body) {
12+
/* applies to <body> */
13+
margin: 0;
14+
}
15+
16+
div :global(strong) {
17+
/* applies to all <strong> elements, in any component,
18+
that are inside <div> elements belonging
19+
to this component */
20+
color: goldenrod;
21+
}
22+
23+
p:global(.big.red) {
24+
/* applies to all <p> elements belonging to this component
25+
with `class="big red"`, even if it is applied
26+
programmatically (for example by a library) */
27+
}
28+
</style>
29+
```
30+
31+
If you want to make @keyframes that are accessible globally, you need to prepend your keyframe names with `-global-`.
32+
33+
The `-global-` part will be removed when compiled, and the keyframe will then be referenced using just `my-animation-name` elsewhere in your code.
34+
35+
```svelte
36+
<style>
37+
@keyframes -global-my-animation-name {
38+
/* code goes here */
39+
}
40+
</style>
41+
```
42+
43+
## :global
44+
45+
To apply styles to a group of selectors globally, create a `:global {...}` block:
46+
47+
```svelte
48+
<style>
49+
:global {
50+
/* applies to every <div> in your application */
51+
div { ... }
52+
53+
/* applies to every <p> in your application */
54+
p { ... }
55+
}
56+
57+
.a :global {
58+
/* applies to every `.b .c .d` element, in any component,
59+
that is inside an `.a` element in this component */
60+
.b .c .d {...}
61+
}
62+
</style>
63+
```
64+
65+
> [!NOTE] The second example above could also be written as an equivalent `.a :global .b .c .d` selector, where everything after the `:global` is unscoped, though the nested form is preferred.

0 commit comments

Comments
 (0)