- 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 - Clearfix
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 定位
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 叠加层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 溢出滚动
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- 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 - !important
- 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 - 伪元素
CSS **伪元素** 用于为元素的特定部分设置样式。浏览网页时,您可能注意到某些段落首字母比其余字母大。这种针对元素特定部分的样式设置是使用 CSS 中的伪元素完成的。在本教程中,我们将解释所有伪元素及其功能。
目录
什么是伪元素?
CSS 中的伪元素用于为元素的特定部分设置样式,这些部分不属于 DOM(文档对象模型)并且不存在于 HTML 标记中。例如,段落首字母、输入元素中的占位符文本或文档中的选定部分。
- 伪元素用双冒号 (::) 表示法表示。
- 选择器中只能使用一个伪元素。
- 选择器中的伪元素必须出现在所有其他组件之后。例如,**p::last-line:hover** 是无效的。
- 伪元素可以用来添加装饰性样式,创建特殊效果,并修改已经应用了状态的元素的某些部分的外观。例如,**p:hover::last-line** 是一个有效的语句,它在将鼠标悬停在段落上时选择段落的最后一行。
语法
selector::pseudo-element {
property: value;
}
浏览器支持四个原始伪元素的单冒号语法,即 ::before、::after、::first-line 和 ::first-letter。
内容插入伪元素
在 CSS 中,伪元素 **::before** 和 **::after** 用于在任何元素之前和之后插入文本内容或图像。
示例
此示例演示如何使用 CSS 在段落的开头和结尾插入文本和图像。
<!DOCTYPE html>
<html>
<head>
<style>
p:before {
content: "NOTE:";
font-weight: bold;
}
p:after {
content: url(/css/images/smiley.png);
}
</style>
</head>
<body>
<p>
We inserted intro at start and emoji at end.
</p>
</body>
</html>
CSS 背景伪元素
在 CSS 中,**::backdrop** 伪元素用于设置处于模态上下文中的元素的背景样式,例如在显示 **<dialog>** 元素时其后面的背景。
示例
以下示例演示了 **::backdrop** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
body{
height: 200px;
}
dialog {
padding: 20px;
border: 2px solid black;
border-radius: 10px;
}
dialog::backdrop {
/* Semi-transparent black */
background-color: rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<h3> Backdrop Example </h3>
<dialog id="myDialog">
<p> This is a dialog with a styled backdrop. </p>
<button id="closeButton"> Close </button>
</dialog>
<button id="openButton">Open Dialog</button>
<script>
const dialog = document.getElementById('myDialog');
const openButton = document.getElementById('openButton');
const closeButton = document.getElementById('closeButton');
openButton.addEventListener('click', () => {
dialog.showModal();
});
closeButton.addEventListener('click', () => {
dialog.close();
});
</script>
</body>
</html>
CSS 提示伪元素
在 CSS 中,伪元素 **::cue** 与 Web 视频文本轨道一起使用,为媒体元素(如 **<video>** 和 **<audio>**)的字幕或标题等文本轨道的特定部分设置样式。
示例
以下示例演示了 ::cue 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
video {
width: 100%;
}
video::cue {
font-size: 1rem;
color: peachpuff;
}
</style>
</head>
<body>
<video controls src="/css/foo.mp4">
<track default kind="captions"
srclang="en" src="/css/cue-sample.vtt" />
</video>
</body>
</html>
CSS 首字母伪元素
在 CSS 中,**::first-letter** 伪元素用于定位任何元素(如 **div**、**段落**、**span** 等)文本内容的首字母。
示例
以下示例演示了 **::first-letter** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
text-transform: uppercase;
font-size: 2em;
color: darkred;
font-style: italic;
}
</style>
</head>
<body>
<p>
this is a paragraph with first letter in lowercase,
we used ::first-letter pseudo-element to capitalize
first-letter of paragraph with a larger font size
and a different color.
</p>
</body>
</html>
CSS 首行伪元素
在 CSS 中,**::first-line** 伪元素用于定位任何元素(如 **div**、**段落**、**span** 等)文本内容的首行。
示例
以下示例演示了 **::first-line** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
p::first-line {
background-color: #f0f0f0;
color: darkred;
font-style: italic;
}
</style>
</head>
<body>
<p>
This is a normal paragraph with no stylings, we used
::first-line pseudo-element to only style first-line of
paragraph by adding a background color, font-style and
text color
</p>
</body>
</html>
CSS 文件选择器按钮伪元素
在 CSS 中,**::file-selector-button** 伪元素用于在现代浏览器中设置文件 **input** 元素 (<input type="file">) 的按钮样式。
示例
以下示例演示了 **::file-selector-button** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: block;
height: 100px;
}
input::file-selector-button {
background-image:url(/css/images/border.png);
background-size: 200%;
border: 2px solid black;
border-radius: 8px;
font-weight: 600;
color: rgb(6, 1, 9);
padding: 15px;
transition: all 0.25s;
}
</style>
</head>
<body>
<h2> Select a file </h2>
<input type="file">
</body>
</html>
CSS 标记伪元素
在 CSS 中,**::marker** 伪元素用于设置 **有序列表** 和 **无序列表** 的标记样式。
示例
以下示例演示了 **::marker** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
ol li::marker {
color: rgb(11, 38, 241);
font-weight: bold;
}
ul li::marker {
content: url('/css/images/smiley.png')
}
</style>
</head>
<body>
<h2>Numbered list</h2>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<h2>Bulleted list</h2>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>
CSS 占位符伪元素
在 CSS 中,**::placeholder** 伪元素用于设置文本 **input** 元素 (<input type="text">) 内的默认文本样式。
示例
以下示例演示了 **::placeholder** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
.form {
border: 2px solid black;
background: lightgray;
padding: 25px;
display: flex;
flex-direction: column;
gap: 10px;
}
input{
padding: 10px;
background-color: cornsilk;
}
input::placeholder {
color: grey;
font-style: italic;
font-size: 20px;
}
</style>
</head>
<body>
<div class="form">
<h2> Your Details:</h2>
<input type="text" placeholder="First Name">
<input type="text" placeholder="Last Name">
<input type="text" placeholder="Address">
<input type="text" placeholder="Phone">
</div>
</body>
</html>
CSS 选择伪元素
在 CSS 中,**::selection** 伪元素用于设置任何元素(如 **div**、**段落**、**span** 等)内用户选中文本的样式。
示例
以下示例演示了 **::selection** 伪元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
.highlight::selection {
color: yellow;
background: brown;
}
</style>
</head>
<body>
<p class="highlight">
Select Me!!! to see the effect.
</p>
<p> No style applied to me. </p>
</body>
</html>
多个伪元素
我们还可以向选择器添加多个伪元素,查看示例。
示例
以下示例演示了多个伪元素 (::first-line 和 ::first-letter) 的用法。
<!DOCTYPE html>
<html>
<head>
<style>
p::first-line {
text-decoration: underline;
}
p::first-letter {
text-transform: uppercase;
font-size: 2em;
color: red;
}
</style>
</head>
<body>
<p>
the first line of this paragraph will be underlined and
first letter is uppercase, 2em and red in color, as the
pseudo-element ::first-line & ::first-letter is applied
on p. The other lines are not underlined.
</p>
</body>
</html>
所有 CSS 伪元素
下表显示了 CSS 中的所有伪元素
| 伪元素 | 描述 | 示例 |
|---|---|---|
| ::after | 添加一个作为所选元素的最后一个子元素的伪元素。 | |
| ::backdrop | 用于设置对话框等元素的背景样式。 | |
| ::before | 添加一个作为所选元素的第一个子元素的伪元素。 | |
| ::cue | 用于设置带有视频文本轨道的媒体中的字幕和提示的样式。 | |
| ::first-letter | 将样式应用于块级元素第一行的首字母。 | |
| ::first-line | 将样式应用于块级元素的第一行。 | |
| ::file-selector-button | 表示 type="file" 的 <input> 的按钮。 | |
| ::marker | 选择列表项的标记框。 | |
| ::part() | 表示具有匹配 part 属性的影子树中的元素。 | |
| ::placeholder | 表示 <input> 或 <textarea> 元素中的占位符文本。 | |
| ::selection | 将样式应用于文档的选定部分(通过单击并拖动鼠标跨越文本进行选择)。 | |
| ::slotted() | 表示已放置到 HTML 模板内的插槽中的元素。 | |
| ::grammar-error | 用于设置浏览器内置语法检查工具已识别为语法错误的文本的样式。 | |
| ::spelling-error | 用于设置浏览器内置拼写检查工具已识别为拼写错误的文本的样式。 |