You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 8-web-components/6-shadow-dom-style/article.md
+15-14
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Shadow DOM styling
2
2
3
-
Shadow DOM may include both `<style>` and `<link rel="stylesheet" href="…">` tags. In the latter case, stylesheets are HTTP-cached, so they are not redownloaded. There's no overhead in @importing or linking same styles for many components.
3
+
Shadow DOM may include both `<style>` and `<link rel="stylesheet" href="…">` tags. In the latter case, stylesheets are HTTP-cached, so they are not redownloaded for multiple components that use same template.
4
4
5
5
As a general rule, local styles work only inside the shadow tree, and document styles work outside of it. But there are few exceptions.
6
6
@@ -44,7 +44,7 @@ customElements.define('custom-dialog', class extends HTMLElement {
44
44
45
45
## Cascading
46
46
47
-
The shadow host (`<custom-dialog>` itself) resides in the light DOM, so it's affected by the main CSS cascade.
47
+
The shadow host (`<custom-dialog>` itself) resides in the light DOM, so it's affected by document CSS rules.
48
48
49
49
If there's a property styled both in `:host` locally, and in the document, then the document style takes precedence.
50
50
@@ -58,7 +58,7 @@ custom-dialog {
58
58
```
59
59
...Then the `<custom-dialog>` would be without padding.
60
60
61
-
It's very convenient, as we can setup "default" styles in the component`:host` rule, and then easily override them in the document.
61
+
It's very convenient, as we can setup "default" component styles in its`:host` rule, and then easily override them in the document.
62
62
63
63
The exception is when a local property is labelled `!important`, for such properties, local styles take precedence.
64
64
@@ -79,6 +79,7 @@ For example, we'd like to center the `<custom-dialog>` only if it has `centered`
79
79
left: 50%;
80
80
top: 50%;
81
81
transform: translate(-50%, -50%);
82
+
border-color: blue;
82
83
}
83
84
84
85
:host {
@@ -108,13 +109,13 @@ customElements.define('custom-dialog', class extends HTMLElement {
108
109
</custom-dialog>
109
110
```
110
111
111
-
Now the additional centering styles are only applied to the first dialog `<custom-dialog centered>`.
112
+
Now the additional centering styles are only applied to the first dialog:`<custom-dialog centered>`.
112
113
113
114
## :host-context(selector)
114
115
115
116
Same as `:host`, but applied only if the shadow host or any of its ancestors in the outer document matches the `selector`.
116
117
117
-
E.g. `:host-context(.dark-theme)` matches only if there's `dark-theme` class on `<custom-dialog>` on above it:
118
+
E.g. `:host-context(.dark-theme)` matches only if there's `dark-theme` class on `<custom-dialog>` on anywhere above it:
118
119
119
120
```html
120
121
<bodyclass="dark-theme">
@@ -190,11 +191,11 @@ customElements.define('user-card', class extends HTMLElement {
190
191
</script>
191
192
```
192
193
193
-
Here `<p>John Smith</p>` becomes bold, because CSS inheritance is in effect between the `<slot>` and its contents. But not all CSS properties are inherited.
194
+
Here `<p>John Smith</p>` becomes bold, because CSS inheritance is in effect between the `<slot>` and its contents. But in CSS itself not all properties are inherited.
194
195
195
196
Another option is to use `::slotted(selector)` pseudo-class. It matches elements based on two conditions:
196
197
197
-
1. The element from the light DOM that is inserted into a `<slot>`. Then slot name doesn't matter. Just any slotted element, but only the element itself, not its children.
198
+
1. That'sa slotted element, that comes from the light DOM. Slot name doesn't matter. Just any slotted element, but only the element itself, not its children.
198
199
2. The element matches the `selector`.
199
200
200
201
In our example, `::slotted(div)` selects exactly `<div slot="username">`, but not its children:
@@ -239,21 +240,21 @@ Also, `::slotted` can only be used in CSS. We can't use it in `querySelector`.
239
240
240
241
## CSS hooks with custom properties
241
242
242
-
How do we stylea componentin-depth from the main document?
243
+
How do we styleinternal elements of a component from the main document?
243
244
244
-
Naturally, document styles apply to `<custom-dialog>` element or `<user-card>`, etc. But how can we affect its internals? For instance, in `<user-card>` we'd like to allow the outer document change how user fields look.
245
+
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to styleshadow DOM elements inside them?
245
246
246
-
Just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it.
247
+
There's no selector that can directly affect shadow DOM styles from the document. But just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it.
247
248
248
249
**Custom CSS properties exist on all levels, both in light and shadow.**
249
250
250
-
For example, in shadow DOM we can use `--user-card-field-color` CSS variable to style fields:
251
+
For example, in shadow DOM we can use `--user-card-field-color` CSS variable to style fields, and the outer document can set its value:
251
252
252
253
```html
253
254
<style>
254
255
.field {
255
256
color: var(--user-card-field-color, black);
256
-
/* if --user-card-field-color is not defined, use black */
257
+
/* if --user-card-field-color is not defined, use black color */
- slotted elements (coming from light DOM), `::slotted(selector)` allows to select slotted elements themselves, but not their children.
322
323
323
324
Document styles can affect:
324
-
- shadow host (as it's in the outer document)
325
-
- slotted elements and their contents (as it'sphysically in the outer document)
325
+
- shadow host (as it lives in the outer document)
326
+
- slotted elements and their contents (as that'salso in the outer document)
326
327
327
328
When CSS properties conflict, normally document styles have precedence, unless the property is labelled as `!important`. Then local styles have precedence.
0 commit comments