aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_doublecolon_part/index.html
blob: 459285138673ea4e90696865390cfb1eb086ceff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
title: '::part()'
slug: 'Web/CSS/::part'
tags:
  - '::part'
  - CSS
  - Draft
  - Experimental
  - NeedsBrowserCompatibility
  - NeedsExample
  - Pseudo-element
  - Reference
  - Selector
translation_of: 'Web/CSS/::part'
---
<div>{{CSSRef}}</div>

<p>该 <strong><code>::part</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> <a href="/en-US/docs/Web/CSS/Pseudo-elements">伪元素</a> 表示在 <a href="/en-US/docs/Web/Web_Components/Using_shadow_DOM">阴影树</a> 中任何匹配 {{HTMLAttrxRef("part")}} 属性的元素。</p>

<pre class="brush: css no-line-numbers notranslate">custom-element::part(foo) {
  /* 样式作用于 `foo` 部分 */
}
</pre>

<h2 id="语法">语法</h2>

{{CSSSyntax}}

<h2 id="示例">示例</h2>

<h3 id="HTML">HTML</h3>

<pre class="brush: html notranslate">&lt;template id="tabbed-custom-element"&gt;
&lt;style type="text/css"&gt;
*, ::before, ::after {
  box-sizing: border-box;
  padding: 1rem;
}
:host {
  display: flex;
}
&lt;/style&gt;
&lt;div part="tab active"&gt;Tab 1&lt;/div&gt;
&lt;div part="tab"&gt;Tab 2&lt;/div&gt;
&lt;div part="tab"&gt;Tab 3&lt;/div&gt;
&lt;/template&gt;

&lt;tabbed-custom-element&gt;&lt;/tabbed-custom-element&gt;</pre>

<h3 id="CSS">CSS</h3>

<pre class="brush: css notranslate">tabbed-custom-element::part(tab) {
  color: #0c0dcc;
  border-bottom: transparent solid 2px;
}

tabbed-custom-element::part(tab):hover {
  background-color: #0c0d19;
  border-color: #0c0d33;
}

tabbed-custom-element::part(tab):hover:active {
  background-color: #0c0d33;
}

tabbed-custom-element::part(tab):focus {
  box-shadow:
    0 0 0 1px #0a84ff inset,
    0 0 0 1px #0a84ff,
    0 0 0 4px rgba(10, 132, 255, 0.3);
}

tabbed-custom-element::part(active) {
  color: #0060df;
  border-color: #0a84ff !important;
}
</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js notranslate">let template = document.querySelector("#tabbed-custom-element");
globalThis.customElements.define(template.id, class extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
    this.shadowRoot.appendChild(template.content);
  }
});
</pre>

<h3 id="结果">结果</h3>

<p>{{EmbedLiveSample('Examples')}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS Shadow Parts", "#part", "::part")}}</td>
   <td>{{Spec2("CSS Shadow Parts")}}</td>
   <td>初始定义.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("css.selectors.part")}}</p>

<h2 id="相关阅读">相关阅读</h2>

<ul>
 <li>{{HTMLAttrxRef("part")}} 属性 - 用于定义可以被 <code>::part()</code> 选取的选择器</li>
 <li>{{HTMLAttrxRef("exportparts")}} 属性 - 用于将阴影部分从嵌套的阴影树过渡到包含显影树的传递导出。</li>
 <li><a href="https://github.com/fergald/docs/blob/master/explainers/css-shadow-parts-1.md">Explainer: CSS Shadow ::part and ::theme</a></li>
</ul>