::part()
**::part
** CSS 伪元素 代表 阴影树 中任何具有匹配 part
属性的元素。
css
custom-element::part(foo) {
/* Styles to apply to the `foo` part */
}
语法
css
::part(<ident>+) {
/* ... */
}
示例
HTML
html
<template id="tabbed-custom-element">
<style>
*,
::before,
::after {
box-sizing: border-box;
padding: 1rem;
}
:host {
display: flex;
}
</style>
<div part="tab active">Tab 1</div>
<div part="tab">Tab 2</div>
<div part="tab">Tab 3</div>
</template>
<tabbed-custom-element></tabbed-custom-element>
CSS
css
tabbed-custom-element::part(tab) {
color: #0c0dcc;
border-bottom: transparent solid 2px;
}
tabbed-custom-element::part(tab):hover {
background-color: #0c0d19;
color: #ffffff;
border-color: #0c0d33;
}
tabbed-custom-element::part(tab):hover:active {
background-color: #0c0d33;
color: #ffffff;
}
tabbed-custom-element::part(tab):focus {
box-shadow:
0 0 0 1px #0a84ff inset,
0 0 0 1px #0a84ff,
0 0 0 4px rgb(10 132 255 / 30%);
}
tabbed-custom-element::part(active) {
color: #0060df;
border-color: #0a84ff !important;
}
JavaScript
js
let template = document.querySelector("#tabbed-custom-element");
globalThis.customElements.define(
template.id,
class extends HTMLElement {
constructor() {
super().attachShadow({ mode: "open" }).append(template.content);
}
},
);
结果
规范
规范 |
---|
CSS 阴影部分 # part |
浏览器兼容性
BCD 表格仅在浏览器中加载
另请参阅
part
属性:state()
伪类函数exportparts
属性- CSS 阴影部分 模块