1
1
---
2
2
title : Styling
3
- order : 5
3
+ order : 6
4
4
---
5
5
6
6
Web Components have powerful styling capabilities which make them portable and extensible. Styles declared within the
7
- Shadow DOM serve as a Web Component’s default styling. It’s similar to writing a user-agent stylesheet for your custom
8
- element and it works quite similarily as well.
7
+ Shadow DOM serve as a Web Component's default styling. They work similarly to writing a user-agent stylesheet.
9
8
10
9
## Shadow Encapsulation: Scoped Styles
11
10
12
11
Shadow DOM offers a boundary line between styles outside the tree, and styles inside the tree. Styles cannot cross this
13
- boundary unintentionally. This is different to regular CSS where a selector can effect every element on a page.
12
+ boundary unintentionally. This is different to regular CSS where a selector can affect every element on a page.
14
13
15
14
``` html
16
15
<style >
@@ -33,7 +32,7 @@ boundary unintentionally. This is different to regular CSS where a selector can
33
32
</fancy-p >
34
33
```
35
34
36
- The ` <p> ` element within the shadow tree is not effected by the styles declared outside of the shadow tree.
35
+ The ` <p> ` element within the shadow tree is not affected by the styles declared outside of the shadow tree.
37
36
38
37
## Inheritance
39
38
@@ -65,20 +64,19 @@ element itself (also known as the shadow host).
65
64
</article >
66
65
```
67
66
68
- The ` <article-meta> ` custom element inherits its ` color ` from the ` <article> ` element where it is set to ` deeppink ` . The
67
+ The ` <article-meta> ` custom element inherits its ` color ` from the ` <article> ` element where it's set to ` deeppink ` . The
69
68
` <span> ` element within the shadow tree inherits its ` color ` from the ` <article-meta> ` custom element which means the
70
69
value will also be ` deeppink ` .
71
70
72
71
## Styling elements outside of a shadow tree
73
72
74
- In order to be portable, Web Components can provide default styles for the element itself (also known as the shadow
75
- host). They can also style slotted elements with some default styles.
73
+ In order to be portable, Web Components can add default styles for the element itself (also known as the shadow host).
74
+ They can also style slotted elements with some default styles.
76
75
77
76
### Writing default styles for the shadow host with ` :host ` and ` :host() `
78
77
79
- There are two selectors which can be used to style the shadow host from within the shadow tree. They are the ` :host `
80
- pseudo-class and the ` :host() ` pseudo-class function. The first will always select the shadow host. Here’s ` :host ` in
81
- action:
78
+ You can use host selectors to style an element from within the shadow tree. The ` :host ` pseudo-class always applies
79
+ styles to the custom element, aka the _ shadow host_ :
82
80
83
81
``` html
84
82
<fancy-p >
@@ -92,9 +90,10 @@ action:
92
90
</fancy-p >
93
91
```
94
92
95
- The ` :host() ` selector will select the shadow host if it matches a given selector. For example, it can be used to select
96
- for hosts that have a given attribute. While ` :host ` may apply to ` <fancy-p> ` component, ` :host([extra]) ` would apply
97
- only to ` <fancy-p extra> ` elements:
93
+ You can also add conditional styles to the _ shadow host_ using ` :host() ` _ selector function_ . This can be used to style
94
+ the host so long as it matches the given selector. For example, it can be used to select for hosts that have a given
95
+ attribute. While ` :host ` may apply to ` <fancy-p> ` component, ` :host([extra]) ` would apply only to ` <fancy-p extra> `
96
+ elements:
98
97
99
98
``` css
100
99
:host ([extra ]) {
@@ -239,11 +238,12 @@ fancy-article::part(header) slot {
239
238
240
239
## How to include default styles for a Web Component
241
240
242
- There are a variety of methods to include styles for a Web Component . Some will be familiar, but others are newer.
241
+ You can load a stylesheet for a Web Component with a variety of methods . Some may be familiar, but others are newer.
243
242
244
243
### Using ` <style> `
245
244
246
- The ` <style> ` tag is the most simple way to write styles for a Web Component. Just include it in your shadow tree:
245
+ You can include a ` <style> ` tag within your Shadow Tree, and it'll only apply to the Shadow Tree itself - it won't
246
+ affect the rest of the page:
247
247
248
248
``` html
249
249
<style >
@@ -261,16 +261,16 @@ Using a `<link rel="stylesheet">` element in the shadow tree will allow you to w
261
261
<link rel =" stylesheet" href =" /fancy-article-element.css" />
262
262
```
263
263
264
- If you do this, the stylesheet will be loaded after the script is loaded. This will likely cause a “flash of unstyled
265
- content” (FOUC). To circumvent this, you can preload the stylesheet like this:
264
+ If you do this, the stylesheet will be loaded after the script is loaded. This will likely cause a _ flash of unstyled
265
+ content _ (FOUC). To circumvent this, you can preload the stylesheet like this:
266
266
267
267
``` html
268
268
<link rel =" preload" href =" /fancy-article-element.css" as =" style" />
269
269
```
270
270
271
271
### Using Constructable Stylesheets
272
272
273
- Constructable Stylesheets are stylesheets which are programmatically created in JavaScript. You can create a new
273
+ _ Constructable Stylesheets _ are stylesheets which are programmatically created in JavaScript. You can create a new
274
274
stylesheet using the ` CSSStyleSheet ` constructor and set styles with the ` .replaceSync() ` method:
275
275
276
276
``` js
@@ -293,7 +293,7 @@ class FancyArticleElement extends HTMLElement {
293
293
294
294
CSS Module scripts allow developers to import stylesheets as if they were a module script. To do so, use an import
295
295
assertion where the ` type ` is ` css ` and then you can add the imported stylesheet to the ` adoptedStyleSheets ` array for
296
- the element’ s shadow root.
296
+ the element' s shadow root.
297
297
298
298
``` js
299
299
import stylesheet from " ./fancy-article-element.css" assert { type : "css " }
0 commit comments