Skip to content

Commit 81782d9

Browse files
committed
Increase text width and fix reference link style + placement
1 parent 7d0d043 commit 81782d9

File tree

1 file changed

+43
-67
lines changed

1 file changed

+43
-67
lines changed

learn/components/styling.md

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ title: Styling
33
order: 5
44
---
55

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.
119

1210
## Shadow Encapsulation: Scoped Styles
1311

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:
2116

2217
```html
2318
<style>
@@ -40,20 +35,15 @@ on styling. Here’s a basic demonstration:
4035
</fancy-p>
4136
```
4237

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.
4539

4640
## Inheritance
4741

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
5345
shadow host). Here’s a demonstration of this at play:
5446

55-
[CSS custom properties]: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
56-
5747
```html
5848
<style>
5949
article {
@@ -76,24 +66,20 @@ shadow host). Here’s a demonstration of this at play:
7666
</article>
7767
```
7868

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`.
8372

8473
## Styling elements outside of a shadow tree
8574

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.
9077

9178
### Writing default styles for the shadow host with `:host` and `:host()`
9279

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:
9783

9884
```html
9985
<fancy-p>
@@ -107,10 +93,8 @@ host. Here’s `:host` in action:
10793
</fancy-p>
10894
```
10995

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.
11498

11599
```css
116100
:host([extra]) {
@@ -126,9 +110,8 @@ have the styles applied.
126110

127111
#### Chaining selectors after `:host`
128112

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.
132115

133116
```html
134117
<fancy-p>
@@ -148,8 +131,8 @@ You can `::slotted()` to style light DOM descendants of a shadow host.
148131

149132
### Writing default styles for slotted elements with `::slotted()`
150133

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.
153136

154137
```html
155138
<fancy-elements>
@@ -170,8 +153,7 @@ styles for different kinds of elements which are slotted.
170153
</fancy-elements>
171154
```
172155

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:
175157

176158
```html
177159
<fancy-article>
@@ -202,8 +184,7 @@ attribute selector which matches the slot:
202184
</fancy-article>
203185
```
204186

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:
207188

208189
```css
209190
::slotted(h1) span {
@@ -213,8 +194,8 @@ work:
213194

214195
## Parts: styling a shadow tree from the outside
215196

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.
218199

219200
```css
220201
fancy-article::part(header) {
@@ -250,8 +231,8 @@ fancy-article::part(content) {
250231
</fancy-article>
251232
```
252233

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:
255236

256237
```css
257238
fancy-article::part(header) slot {
@@ -261,13 +242,11 @@ fancy-article::part(header) slot {
261242

262243
## How to include default styles for a Web Component
263244

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.
266246

267247
### Using `<style>`
268248

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:
271250

272251
```html
273252
<style>
@@ -279,27 +258,23 @@ Component. Just include it in your shadow tree:
279258

280259
### Using `<link rel="stylesheet">`
281260

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.
284262

285263
```html
286264
<link rel="stylesheet" href="/fancy-article-element.css">
287265
```
288266

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:
292269

293270
```html
294271
<link rel="preload" href="/fancy-article-element.css" as="style">
295272
```
296273

297274
### Using Constructable Stylesheets
298275

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:
303278

304279
```js
305280
const stylesheet = new CSSStyleSheet()
@@ -319,10 +294,9 @@ class FancyArticleElement extends HTMLElement {
319294

320295
### Using CSS Module scripts
321296

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.
326300

327301
```js
328302
import stylesheet from './fancy-article-element.css' assert { type: 'css' }
@@ -333,3 +307,5 @@ class FancyArticleElement extends HTMLElement {
333307
}
334308
}
335309
```
310+
311+
[CSS custom properties]: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties

0 commit comments

Comments
 (0)