CSS 伪元素:创造虚拟元素的魔法
伪元素的本质
伪元素(Pseudo-element)是 CSS 中一种特殊的选择器,它能够让你在不修改 HTML 结构的情况下,向文档中插入"虚拟"的元素或选择元素的特定部分进行样式化。
伪元素使用双冒号 :: 语法(CSS3 规范,但 : 单冒号语法仍然有效以保持向后兼容)。可以把伪元素想象成"CSS 创造的幽灵元素"——它们存在于渲染结果中,但不存在于 HTML 代码中。
css
/* 现代语法(推荐) */
selector::pseudo-element {
property: value;
}
/* 传统语法(向后兼容) */
selector:pseudo-element {
property: value;
}伪元素与伪类的区别:
- 伪类(:hover, :focus)选择元素的特定状态
- 伪元素(::before, ::after)创建或选择元素的特定部分
::before 和 ::after
::before 和 ::after 是最常用的伪元素,它们在元素内容的前后插入生成的内容。
基本语法
css
.element::before {
content: ""; /* content 属性是必需的 */
/* 其他样式 */
}
.element::after {
content: "";
/* 其他样式 */
}重要规则:
content属性是必需的,即使是空字符串- 伪元素默认是行内元素,通常需要设置
display属性 - 伪元素不在 DOM 中,不能被 JavaScript 直接访问
- 伪元素不能应用于替换元素(如
<img>,<input>)
装饰性图标
css
/* 添加图标 */
.tip::before {
content: "💡";
margin-right: 8px;
}
.warning::before {
content: "⚠️";
margin-right: 8px;
}
.success::after {
content: "✓";
margin-left: 8px;
color: #27ae60;
}
/* 使用 Unicode */
.external-link::after {
content: "\2197"; /* ↗ */
font-size: 0.8em;
margin-left: 4px;
}
/* 使用图片 */
.pdf-link::before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
background: url("pdf-icon.svg") no-repeat center;
background-size: contain;
margin-right: 6px;
vertical-align: middle;
}引用符号
css
/* 自动添加引号 */
blockquote::before {
content: open-quote;
font-size: 3em;
color: #3498db;
line-height: 0;
margin-right: 10px;
vertical-align: -0.4em;
}
blockquote::after {
content: close-quote;
font-size: 3em;
color: #3498db;
line-height: 0;
margin-left: 10px;
vertical-align: -0.4em;
}
blockquote {
quotes: "" " " "" "'" "'"; /* 定义引号样式 */
font-style: italic;
padding: 20px;
}装饰性形状
css
/* 丝带效果 */
.ribbon {
position: relative;
background-color: #3498db;
color: white;
padding: 10px 20px;
display: inline-block;
}
.ribbon::before,
.ribbon::after {
content: "";
position: absolute;
top: 0;
width: 0;
height: 0;
border-style: solid;
}
.ribbon::before {
left: -20px;
border-width: 20px 20px 20px 0;
border-color: transparent #3498db transparent transparent;
}
.ribbon::after {
right: -20px;
border-width: 20px 0 20px 20px;
border-color: transparent transparent transparent #3498db;
}
/* 标签角标 */
.tag {
position: relative;
background-color: #e74c3c;
color: white;
padding: 5px 10px;
margin-right: 15px;
}
.tag::after {
content: "";
position: absolute;
right: -15px;
top: 0;
width: 0;
height: 0;
border-top: 15px solid #e74c3c;
border-right: 15px solid transparent;
border-bottom: 15px solid #e74c3c;
}计数器
css
/* 自动编号 */
body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
color: #3498db;
font-weight: bold;
}
/* 嵌套计数器 */
.chapter {
counter-reset: subsection;
}
.chapter h2::before {
counter-increment: section;
content: counter(section) ". ";
}
.chapter h3::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
/* 列表样式 */
ol {
counter-reset: custom-counter;
list-style: none;
}
ol li::before {
counter-increment: custom-counter;
content: counter(custom-counter) ". ";
color: #3498db;
font-weight: bold;
margin-right: 8px;
}属性值显示
css
/* 显示链接的 URL */
a[href]::after {
content: " (" attr(href) ")";
font-size: 0.8em;
color: #777;
}
/* 打印样式中显示缩写全称 */
@media print {
abbr[title]::after {
content: " (" attr(title) ")";
font-style: italic;
}
}
/* 显示图片的 alt 文本 */
img::after {
content: attr(alt);
display: block;
text-align: center;
font-size: 0.9em;
color: #666;
margin-top: 5px;
}::first-letter - 首字母
选择块级元素的第一个字母,常用于创建首字下沉效果:
css
/* 经典首字下沉 */
p::first-letter {
font-size: 3em;
font-weight: bold;
float: left;
line-height: 1;
margin: 0 10px 0 0;
color: #e74c3c;
}
/* 装饰性首字母 */
article p:first-of-type::first-letter {
font-size: 4em;
font-family: "Georgia", serif;
color: #3498db;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
float: left;
padding: 10px;
margin-right: 10px;
}
/* 带边框的首字母 */
.chapter::first-letter {
font-size: 2.5em;
font-weight: bold;
color: white;
background-color: #2c3e50;
padding: 10px 15px;
margin-right: 10px;
float: left;
border-radius: 4px;
}::first-line - 首行
选择块级元素的第一行文本:
css
/* 首行突出显示 */
p::first-line {
font-weight: bold;
color: #2c3e50;
font-size: 1.1em;
}
/* 新闻文章样式 */
article p:first-of-type::first-line {
text-transform: uppercase;
letter-spacing: 2px;
font-weight: 600;
}
/* 组合使用 */
.intro::first-line {
font-variant: small-caps;
color: #3498db;
}
.intro::first-letter {
font-size: 2em;
color: #e74c3c;
}::selection - 文本选择
自定义用户选中文本时的样式:
css
/* 全局选择样式 */
::selection {
background-color: #3498db;
color: white;
}
/* 特定元素的选择样式 */
code::selection {
background-color: #2c3e50;
color: #f39c12;
}
.highlight::selection {
background-color: #f39c12;
color: #2c3e50;
}
/* Firefox 兼容 */
::-moz-selection {
background-color: #3498db;
color: white;
}::placeholder - 占位符
自定义表单输入框占位符的样式:
css
/* 基础占位符样式 */
input::placeholder {
color: #95a5a6;
font-style: italic;
opacity: 1;
}
textarea::placeholder {
color: #bdc3c7;
font-size: 0.9em;
}
/* 聚焦时占位符变化 */
input:focus::placeholder {
color: transparent;
}
/* 不同浏览器前缀 */
input::-webkit-input-placeholder {
color: #95a5a6;
}
input::-moz-placeholder {
color: #95a5a6;
opacity: 1;
}
input:-ms-input-placeholder {
color: #95a5a6;
}实战应用
清除浮动
css
.clearfix::after {
content: "";
display: table;
clear: both;
}加载动画
css
.loading::after {
content: "";
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-left: 10px;
vertical-align: middle;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}工具提示
css
.tooltip {
position: relative;
cursor: help;
}
.tooltip::before {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background-color: #2c3e50;
color: white;
padding: 8px 12px;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
margin-bottom: 8px;
}
.tooltip::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
border: 6px solid transparent;
border-top-color: #2c3e50;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
margin-bottom: 2px;
}
.tooltip:hover::before,
.tooltip:hover::after {
opacity: 1;
}面包屑导航
css
.breadcrumb {
list-style: none;
display: flex;
padding: 0;
}
.breadcrumb li {
display: flex;
align-items: center;
}
.breadcrumb li:not(:last-child)::after {
content: "›";
margin: 0 10px;
color: #95a5a6;
font-size: 1.2em;
}
.breadcrumb li:last-child {
color: #3498db;
font-weight: bold;
}进度条
css
.progress {
width: 100%;
height: 30px;
background-color: #ecf0f1;
border-radius: 15px;
overflow: hidden;
position: relative;
}
.progress::before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: var(--progress, 0%);
background: linear-gradient(90deg, #3498db, #2ecc71);
border-radius: 15px;
transition: width 0.3s ease;
}
.progress::after {
content: attr(data-label);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #2c3e50;
font-weight: bold;
z-index: 1;
}卡片装饰
css
.card {
position: relative;
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
/* 顶部装饰条 */
.card::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #667eea, #764ba2);
}
/* 角标 */
.card::after {
content: "NEW";
position: absolute;
top: 20px;
right: -30px;
background-color: #e74c3c;
color: white;
padding: 5px 40px;
transform: rotate(45deg);
font-size: 0.8em;
font-weight: bold;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}按钮悬停效果
css
.button {
position: relative;
padding: 12px 24px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
overflow: hidden;
cursor: pointer;
transition: color 0.3s;
}
.button::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background-color: #2980b9;
transition: left 0.3s;
z-index: -1;
}
.button:hover::before {
left: 0;
}
.button:hover {
color: white;
}分隔线
css
.section-title {
text-align: center;
position: relative;
margin: 40px 0;
}
.section-title::before,
.section-title::after {
content: "";
position: absolute;
top: 50%;
width: 40%;
height: 1px;
background-color: #ddd;
}
.section-title::before {
left: 0;
}
.section-title::after {
right: 0;
}
/* 装饰性分隔线 */
.divider {
text-align: center;
margin: 30px 0;
position: relative;
}
.divider::before {
content: "";
position: absolute;
left: 0;
top: 50%;
width: 100%;
height: 1px;
background: linear-gradient(90deg, transparent, #3498db, transparent);
}
.divider::after {
content: "✦";
position: relative;
display: inline-block;
padding: 0 20px;
background-color: white;
color: #3498db;
font-size: 20px;
}最佳实践
- 始终包含 content 属性:即使是空字符串也要写上
content: "" - 注意 z-index:伪元素可能会遮挡其他内容
- 性能考虑:过度使用伪元素可能影响渲染性能
- 语义清晰:装饰性内容用伪元素,实质内容用 HTML
- 可访问性:伪元素的内容对屏幕阅读器可能不可见
- 浏览器兼容:旧版浏览器可能需要单冒号语法
总结
伪元素是 CSS 中最具创造力的特性之一。它让我们能够在不污染 HTML 结构的前提下,添加丰富的视觉效果和装饰元素。
核心要点:
- ::before 和 ::after:最常用的伪元素,在内容前后插入虚拟元素
content属性是必需的- 可用于图标、引号、形状、计数器、属性值显示等
- 默认为行内元素,常需设置
display
- ::first-letter:选择首字母,实现首字下沉等效果
- ::first-line:选择首行,突出显示第一行文本
- ::selection:自定义文本选中样式
- ::placeholder:自定义表单占位符样式
- 实战应用:清除浮动、加载动画、工具提示、面包屑导航、进度条、装饰效果
- 最佳实践:合理使用、注意性能、保持语义、考虑可访问性