11---
22title : Styling
3- order : 5
3+ order : 6
44---
55
66Web 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.
98
109## Shadow Encapsulation: Scoped Styles
1110
1211Shadow 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.
1413
1514``` html
1615<style >
@@ -33,7 +32,7 @@ boundary unintentionally. This is different to regular CSS where a selector can
3332</fancy-p >
3433```
3534
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.
3736
3837## Inheritance
3938
@@ -65,20 +64,19 @@ element itself (also known as the shadow host).
6564</article >
6665```
6766
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
6968` <span> ` element within the shadow tree inherits its ` color ` from the ` <article-meta> ` custom element which means the
7069value will also be ` deeppink ` .
7170
7271## Styling elements outside of a shadow tree
7372
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.
7675
7776### Writing default styles for the shadow host with ` :host ` and ` :host() `
7877
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_ :
8280
8381``` html
8482<fancy-p >
@@ -92,9 +90,10 @@ action:
9290</fancy-p >
9391```
9492
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:
9897
9998``` css
10099:host ([extra ]) {
@@ -239,11 +238,12 @@ fancy-article::part(header) slot {
239238
240239## How to include default styles for a Web Component
241240
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.
243242
244243### Using ` <style> `
245244
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:
247247
248248``` html
249249<style >
@@ -261,16 +261,16 @@ Using a `<link rel="stylesheet">` element in the shadow tree will allow you to w
261261<link rel =" stylesheet" href =" /fancy-article-element.css" />
262262```
263263
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:
266266
267267``` html
268268<link rel =" preload" href =" /fancy-article-element.css" as =" style" />
269269```
270270
271271### Using Constructable Stylesheets
272272
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
274274stylesheet using the ` CSSStyleSheet ` constructor and set styles with the ` .replaceSync() ` method:
275275
276276``` js
@@ -293,7 +293,7 @@ class FancyArticleElement extends HTMLElement {
293293
294294CSS Module scripts allow developers to import stylesheets as if they were a module script. To do so, use an import
295295assertion 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.
297297
298298``` js
299299import stylesheet from " ./fancy-article-element.css" assert { type : "css " }
0 commit comments