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
+64-63
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,21 @@
1
-
# Shadow DOM styling
1
+
# 给 Shadow DOM 添加样式
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 for multiple components that use same template.
3
+
shadow DOM 可以包含 `<style>`和`<link rel="stylesheet" href="…">`标签。在后一种情况下,样式表是 HTTP 缓存的,因此不会为使用同一模板的多个组件重新下载样式表。
4
4
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.
To summarize, we can use `:host`-family of selectors to style the main element of the component, depending on the context. These styles (unless `!important`) can be overridden by the document.
In the example below, slotted `<span>` is bold, as per document style, but does not take `background` from the local style:
138
140
```html run autorun="no-epub" untrusted height=80
139
141
<style>
140
142
*!*
@@ -163,11 +165,11 @@ customElements.define('user-card', class extends HTMLElement {
163
165
</script>
164
166
```
165
167
166
-
The result is bold, but not red.
168
+
结果是粗体,但不是红色。
167
169
168
-
If we'd like to style slotted elements in our component, there are two choices.
170
+
如果我们想要在我们的组件中设置占槽元素的样式,有两种选择。
169
171
170
-
First, we can style the `<slot>` itself and rely on CSS inheritance:
172
+
首先,我们可以对 `<slot>` 本身进行样式化,并借助 CSS 继承:
171
173
172
174
```html run autorun="no-epub" untrusted height=80
173
175
<user-card>
@@ -191,14 +193,14 @@ customElements.define('user-card', class extends HTMLElement {
191
193
</script>
192
194
```
193
195
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.
Another option is to use`::slotted(selector)` pseudo-class. It matches elements based on two conditions:
198
+
另一个选项是使用 `::slotted(selector)` 伪类。它根据两个条件来匹配元素:
197
199
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.
How do we style internal elements of a component from the main document?
245
+
如何在主文档中设置组件的内建元素的样式?
244
246
245
-
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to styleshadow DOM elements inside them?
247
+
像 `:host` 这样的选择器应用规则到 `<custom-dialog>` 元素或 `<user-card>`,但是如何设置在它们内部的 shadow DOM 元素的样式呢?
246
248
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.
249
+
没有选择器可以从文档中直接影响 shadow DOM 样式。但是,正如我们暴露用来与组件交互的方法那样,我们也可以暴露 CSS 变量(自定义 CSS 属性)来对其进行样式设置。
248
250
249
-
**Custom CSS properties exist on all levels, both in light and shadow.**
251
+
**自定义 CSS 属性存在于所有层次,包括 light DOM 和 shadow DOM。**
250
252
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:
253
+
例如,在 shadow DOM 中,我们可以使用 `--user-card-field-color` CSS 变量来设置字段的样式,而外部文档可以设置它的值:
252
254
253
255
```html
254
256
<style>
255
257
.field {
256
258
color: var(--user-card-field-color, black);
257
-
/*if --user-card-field-color is not defined, use black color*/
- slotted elements and their contents (as that's also in the outer document)
325
+
文档样式可以影响:
326
+
- shadow宿主(因为它位于外部文档中)
327
+
- 占槽元素及占槽元素的内容(因为它们同样位于外部文档中)
327
328
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