Skip to content

Commit c45f5b7

Browse files
committed
document cascade: false - fixes #137
1 parent fa6e83a commit c45f5b7

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

content/guide/03-scoped-styles.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,54 @@ This is vastly simpler than achieving the same effect via [Shadow DOM](http://ca
3434

3535
### Cascading rules
3636

37-
The usual cascading mechanism still applies – any global `.foo` styles would still be applied, and if our template had [nested components](#nested-components) with `class="foo"` elements, they would inherit our styles.
37+
By default, the usual cascading mechanism still applies – any global `.foo` styles would still be applied, and if our template had [nested components](#nested-components) with `class="foo"` elements, they would inherit our styles.
38+
39+
If the `cascade: false` option is passed to the compiler, styles will *only* apply to the current component, unless you opt in to cascading with the `:global(...)` modifier:
40+
41+
<!-- TODO `cascade: false` in the REPL -->
42+
43+
```html-no-repl
44+
<div>
45+
<Widget/>
46+
</div>
47+
48+
<style>
49+
p {
50+
/* this block will be disregarded, since
51+
there are no <p> elements here */
52+
color: red;
53+
}
54+
55+
div :global(p) {
56+
/* this block will be applied to any <p>
57+
elements inside the <div> */
58+
font-weight: bold;
59+
}
60+
</style>
61+
62+
<script>
63+
import Widget from './Widget.html';
64+
65+
export default {
66+
components: { Widget }
67+
};
68+
</script>
69+
```
70+
71+
```html-nested-Widget
72+
<p>only the :global(...) styles apply here</p>
73+
```
74+
75+
The `cascade: false` behaviour is recommended, and will be switched on by default in future versions of Svelte.
3876

3977
> Scoped styles are *not* dynamic – they are shared between all instances of a component. In other words you can't use `{{tags}}` inside your CSS.
4078
4179

80+
### Unused style removal
81+
82+
If you're using the recommended `cascade: false` option, Svelte will identify and remove any styles that it can guarantee are not being used in your app. It will also emit a warning so that you can remove them from the source.
83+
84+
4285
### Special selectors
4386

4487
If you have a [ref](#refs) on an element, you can use this as a CSS selector. An element with `ref:foo` can be styled with `ref:foo { color: whatever; }`. The `ref:*` selector has the same specificity as a class or attribute selector.

0 commit comments

Comments
 (0)