- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 度量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z 索引
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载动画
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode 双向
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - 项目对齐
- CSS - 自身对齐
- CSS - 制表符大小
- CSS - 指针事件
- CSS - 内容放置
- CSS - 项目放置
- CSS - 自身放置
- CSS - 最大块级尺寸
- CSS - 最小块级尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移
- CSS - 口音颜色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2d 变换
- CSS - 3d 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 伪元素 - ::slotted()
::slotted() 伪元素表示具有 slot 属性的元素。与 ::part() 伪元素不同,::slotted() 伪元素仍然封装在 Web 组件的 <template> 中的 <style> 元素中。
此伪元素仅在 shadow DOM 内部的 CSS 中使用时才有效。
语法
selector::slotted(<compound-selector>) {
/* ... */
}
CSS ::slotted 示例
这是一个 ::slotted() 伪元素的示例。在此示例中
模板正在使用三个插槽。
定义了一个自定义元素“sample-card”。
CSS 样式已添加到 <template> 内的 <style> 块中。
插槽需要首先声明,使用类名或 ID。
声明的插槽稍后在 html 代码中使用类名或 ID 调用。
如果类名或 ID 标识符与声明的不匹配,则会呈现插槽的描述。
尝试更改类名或 ID 或样式,并查看更改后的效果。
<html>
<head>
</head>
<body>
<template id="sample-template">
<style>
::slotted(.p-text) {
background-color: lavender;
}
h2::slotted(.heading) {
background: silver;
}
::slotted(#footer-text) {
background-color: lightsteelblue;
border: 2px solid black;
}
</style>
<div>
<h2><slot name="heading">title goes here</slot></h2>
<slot name="p-text">content goes here</slot>
<slot name="footer-text">Footer here</slot>
</div>
</template>
<sample-card>
<span class="heading" slot="heading">::Slotted Example</span>
<p class="p-text" slot="p-text">Paragraph text</p>
<p id="footer-text" slot="footer-text">Footer text</p>
</sample-card>
<script>
customElements.define(
'sample-card',
class extends HTMLElement {
constructor() {
super();
const template = document.getElementById('sample-template');
const shadow = this.attachShadow({ mode: 'open' });
shadow.appendChild(template.content.cloneNode(true));
const elementStyle = document.createElement('style');
elementStyle.textContent = `
div {
width: 250px;
border: 5px inset green;
border-radius: 2px;
padding: 5px;
}`;
shadow.appendChild(elementStyle);
const cssTab = document.querySelector('#css-output');
const editorStyle = document.createElement('style');
editorStyle.textContent = cssTab.textContent;
shadow.appendChild(editorStyle);
cssTab.addEventListener('change', () => {
editorStyle.textContent = cssTab.textContent;
});
}
},
);
</script>
</body>
</html>
广告