@@ -3,21 +3,16 @@ title: Styling
3
3
order : 5
4
4
---
5
5
6
- Web Components have powerful styling capabilities which make them
7
- portable and extensible. Styles declared within the Shadow DOM serve as
8
- a Web Component’s default styling. It’s similar to writing a user-agent
9
- stylesheet for your custom element and it works quite similarily as
10
- well.
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.
11
9
12
10
## Shadow Encapsulation: Scoped Styles
13
11
14
- Styles declared outside of a shadow tree cannot directly select elements
15
- within a shadow tree. Conversely, styles declared inside a shadow tree
16
- cannot directly select elements outside of a shadow tree. For both of
17
- these points, there are special pseudo-selectors which allow styles to
18
- cross the boundary of the Shadow DOM boundary — and we will cover them
19
- later on — but first we’ll focus on shadow encapsulation and its effect
20
- on styling. Here’s a basic demonstration:
12
+ Styles declared outside of a shadow tree cannot directly select elements within a shadow tree. Conversely, styles
13
+ declared inside a shadow tree cannot directly select elements outside of a shadow tree. For both of these points, there
14
+ are special pseudo-selectors which allow styles to cross the boundary of the Shadow DOM boundary — and we will cover
15
+ them later on — but first we’ll focus on shadow encapsulation and its effect on styling. Here’s a basic demonstration:
21
16
22
17
``` html
23
18
<style >
@@ -40,20 +35,15 @@ on styling. Here’s a basic demonstration:
40
35
</fancy-p >
41
36
```
42
37
43
- The ` <p> ` element within the shadow tree is not effected by the styles
44
- declared outside of the shadow tree.
38
+ The ` <p> ` element within the shadow tree is not effected by the styles declared outside of the shadow tree.
45
39
46
40
## Inheritance
47
41
48
- Custom elements abide by the same rules of inheritance as other HTML
49
- elements. Properties such as ` font-size ` , ` font-family ` , and ` color ` are
50
- inherited from a parent element by default. So are [ CSS custom
51
- properties] . Top-level elements within a shadow tree inherit their
52
- inheritable properties from the custom element itself (also known as the
42
+ Custom elements abide by the same rules of inheritance as other HTML elements. Properties such as ` font-size ` ,
43
+ ` font-family ` , and ` color ` are inherited from a parent element by default. So are [ CSS custom properties] [ ] . Top-level
44
+ elements within a shadow tree inherit their inheritable properties from the custom element itself (also known as the
53
45
shadow host). Here’s a demonstration of this at play:
54
46
55
- [ CSS custom properties ] : https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
56
-
57
47
``` html
58
48
<style >
59
49
article {
@@ -76,24 +66,20 @@ shadow host). Here’s a demonstration of this at play:
76
66
</article >
77
67
```
78
68
79
- The ` <article-meta> ` custom element inherits its ` color ` from the
80
- ` <article> ` element where it is set to ` deeppink ` . The ` <span> ` element
81
- within the shadow tree inherits its ` color ` from the ` <article-meta> `
82
- custom element which means the value will be ` deeppink ` .
69
+ The ` <article-meta> ` custom element inherits its ` color ` from the ` <article> ` element where it is set to ` deeppink ` . The
70
+ ` <span> ` element within the shadow tree inherits its ` color ` from the ` <article-meta> ` custom element which means the
71
+ value will be ` deeppink ` .
83
72
84
73
## Styling elements outside of a shadow tree
85
74
86
- In order to be portable, Web Components can provide default styles for a
87
- couple types of elements which exist outside of its shadow tree: the
88
- custom element itself (also known as the shadow host) and slotted
89
- elements.
75
+ In order to be portable, Web Components can provide default styles for a couple types of elements which exist outside of
76
+ its shadow tree: the custom element itself (also known as the shadow host) and slotted elements.
90
77
91
78
### Writing default styles for the shadow host with ` :host ` and ` :host() `
92
79
93
- There are two selectors which can be used to style the shadow host from
94
- within the shadow tree. They are the ` :host ` pseudo-class and the
95
- ` :host() ` pseudo-class function. The first will always select the shadow
96
- host. Here’s ` :host ` in action:
80
+ There are two selectors which can be used to style the shadow host from within the shadow tree. They are the ` :host `
81
+ pseudo-class and the ` :host() ` pseudo-class function. The first will always select the shadow host. Here’s ` :host ` in
82
+ action:
97
83
98
84
``` html
99
85
<fancy-p >
@@ -107,10 +93,8 @@ host. Here’s `:host` in action:
107
93
</fancy-p >
108
94
```
109
95
110
- The second will select the shadow host if it matches a selector. For
111
- example, given the following default styles for the ` <fancy-p> `
112
- component, only the second element in the subsequent code example will
113
- have the styles applied.
96
+ The second will select the shadow host if it matches a selector. For example, given the following default styles for the
97
+ ` <fancy-p> ` component, only the second element in the subsequent code example will have the styles applied.
114
98
115
99
``` css
116
100
:host ([extra ]) {
@@ -126,9 +110,8 @@ have the styles applied.
126
110
127
111
#### Chaining selectors after ` :host `
128
112
129
- While the ` :host ` selector refers to the shadow host element which is
130
- outside of the shadow tree, if you chain selectors it will select
131
- elements within the shadow tree.
113
+ While the ` :host ` selector refers to the shadow host element which is outside of the shadow tree, if you chain selectors
114
+ it will select elements within the shadow tree.
132
115
133
116
``` html
134
117
<fancy-p >
@@ -148,8 +131,8 @@ You can `::slotted()` to style light DOM descendants of a shadow host.
148
131
149
132
### Writing default styles for slotted elements with ` ::slotted() `
150
133
151
- The ` ::slotted() ` pseudo-element selector allows you to write default
152
- styles for different kinds of elements which are slotted.
134
+ The ` ::slotted() ` pseudo-element selector allows you to write default styles for different kinds of elements which are
135
+ slotted.
153
136
154
137
``` html
155
138
<fancy-elements >
@@ -170,8 +153,7 @@ styles for different kinds of elements which are slotted.
170
153
</fancy-elements >
171
154
```
172
155
173
- If you want to target elements in specific slots you can pass an
174
- attribute selector which matches the slot:
156
+ If you want to target elements in specific slots you can pass an attribute selector which matches the slot:
175
157
176
158
``` html
177
159
<fancy-article >
@@ -202,8 +184,7 @@ attribute selector which matches the slot:
202
184
</fancy-article >
203
185
```
204
186
205
- You cannot chain selectors with ` ::slotted() ` , so the following will not
206
- work:
187
+ You cannot chain selectors with ` ::slotted() ` , so the following will not work:
207
188
208
189
``` css
209
190
::slotted(h1 ) span {
@@ -213,8 +194,8 @@ work:
213
194
214
195
## Parts: styling a shadow tree from the outside
215
196
216
- The CSS Shadow Part API allows elements within a shadow tree to be
217
- styled from outside of it. This allows Web Components to be very extensible.
197
+ The CSS Shadow Part API allows elements within a shadow tree to be styled from outside of it. This allows Web Components
198
+ to be very extensible.
218
199
219
200
``` css
220
201
fancy-article ::part(header ) {
@@ -250,8 +231,8 @@ fancy-article::part(content) {
250
231
</fancy-article >
251
232
```
252
233
253
- Similar to ` ::slotted() ` , you cannot chain selectors after ` ::part() ` to
254
- select children or siblings. The following will not work:
234
+ Similar to ` ::slotted() ` , you cannot chain selectors after ` ::part() ` to select children or siblings. The following will
235
+ not work:
255
236
256
237
``` css
257
238
fancy-article ::part(header ) slot {
@@ -261,13 +242,11 @@ fancy-article::part(header) slot {
261
242
262
243
## How to include default styles for a Web Component
263
244
264
- There are a variety of methods to include styles for a Web Component.
265
- Some will be familiar, but others are newer.
245
+ There are a variety of methods to include styles for a Web Component. Some will be familiar, but others are newer.
266
246
267
247
### Using ` <style> `
268
248
269
- The ` <style> ` tag is the most simple way to write styles for a Web
270
- Component. Just include it in your shadow tree:
249
+ The ` <style> ` tag is the most simple way to write styles for a Web Component. Just include it in your shadow tree:
271
250
272
251
``` html
273
252
<style >
@@ -279,27 +258,23 @@ Component. Just include it in your shadow tree:
279
258
280
259
### Using ` <link rel="stylesheet"> `
281
260
282
- Using a ` <link rel="stylesheet"> ` element in the shadow tree will allow
283
- you to write styles in an external stylesheet.
261
+ Using a ` <link rel="stylesheet"> ` element in the shadow tree will allow you to write styles in an external stylesheet.
284
262
285
263
``` html
286
264
<link rel =" stylesheet" href =" /fancy-article-element.css" >
287
265
```
288
266
289
- If you do this, the stylesheet will be loaded after the script is
290
- loaded. This will likely cause a “flash of unstyled content” (FOUC). To
291
- circumvent this, you can preload the stylesheet like this:
267
+ If you do this, the stylesheet will be loaded after the script is loaded. This will likely cause a “flash of unstyled
268
+ content” (FOUC). To circumvent this, you can preload the stylesheet like this:
292
269
293
270
``` html
294
271
<link rel =" preload" href =" /fancy-article-element.css" as =" style" >
295
272
```
296
273
297
274
### Using Constructable Stylesheets
298
275
299
- Constructable Stylesheets are stylesheets which are programmatically
300
- created in JavaScript. You can create a new stylesheet using the
301
- ` CSSStyleSheet ` constructor and set styles with the ` .replaceSync() `
302
- method:
276
+ Constructable Stylesheets are stylesheets which are programmatically created in JavaScript. You can create a new
277
+ stylesheet using the ` CSSStyleSheet ` constructor and set styles with the ` .replaceSync() ` method:
303
278
304
279
``` js
305
280
const stylesheet = new CSSStyleSheet ()
@@ -319,10 +294,9 @@ class FancyArticleElement extends HTMLElement {
319
294
320
295
### Using CSS Module scripts
321
296
322
- CSS Module scripts allow developers to import stylesheets as if they
323
- were a module script. To do so, use an import assertion where the ` type `
324
- is ` css ` and then you can add the imported stylesheet to the
325
- ` adoptedStyleSheets ` array for the element’s shadow root.
297
+ CSS Module scripts allow developers to import stylesheets as if they were a module script. To do so, use an import
298
+ assertion where the ` type ` is ` css ` and then you can add the imported stylesheet to the ` adoptedStyleSheets ` array for
299
+ the element’s shadow root.
326
300
327
301
``` js
328
302
import stylesheet from ' ./fancy-article-element.css' assert { type : 'css ' }
@@ -333,3 +307,5 @@ class FancyArticleElement extends HTMLElement {
333
307
}
334
308
}
335
309
```
310
+
311
+ [ CSS custom properties ] : https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
0 commit comments