::before

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

在 CSS 中,::before 会创建一个伪元素,作为选中元素的第一个子元素。它通常与 content 属性一起使用,向元素添加装饰性内容。它默认是行内元素。

试一试

a {
  color: blue;
  text-decoration: none;
}

a::before {
  content: "🔗";
}

.local-link::before {
  content: url("/shared-assets/images/examples/firefox-logo.svg");
  display: inline-block;
  width: 15px;
  height: 15px;
  margin-right: 5px;
}
<p>
  Learning resources for web developers can be found all over the internet. Try
  out
  <a href="https://webdev.ac.cn/">web.dev</a>,
  <a href="https://w3schools.org.cn/">w3schools.com</a> or our
  <a href="https://mdn.org.cn/" class="local-link">MDN web docs</a>.
</p>

语法

css
::before {
  content: /* value */;
  /* properties */
}

描述

::before 伪元素是一个行内盒模型,作为其关联元素或“源元素”的直接子元素生成。它通常通过 content 属性向元素添加装饰性内容,例如图标、引号或其他装饰。

::before 伪元素不能应用于替换元素,例如 <img>,因为它们的内容由外部资源决定,不受当前文档样式的影响。

::before 伪元素的 display 值为 list-item 时,它表现得像一个列表项,因此可以像 <li> 元素一样生成一个 ::marker 伪元素。

如果 content 属性未指定、值无效,或值为 normalnone,则 ::before 伪元素不会被渲染。它的行为等同于设置了 display: none

注意: 选择器级别 3 规范引入了双冒号表示法 ::before,以区分伪类伪元素。浏览器也接受 CSS2 中引入的单冒号表示法 :before

默认情况下,::before::after 伪元素与其父元素共享相同的堆叠上下文。如果没有明确设置 z-index::after 伪元素生成的内容将显示在 ::before 伪元素生成的内容之上,因为 ::after 在 DOM 流中渲染较晚。

无障碍

不建议使用 ::before 伪元素添加内容,因为它不能可靠地被屏幕阅读器访问。

示例

引号

使用 ::before 伪元素的一个例子是提供引号。在这里,我们使用 ::before::after 来插入引号字符。

HTML

html
<q>Some quotes</q>, he said, <q>are better than none.</q>

CSS

css
q::before {
  content: "«";
  color: blue;
}

q::after {
  content: "»";
  color: red;
}

结果

装饰性示例

我们可以以我们想要的任何方式对 content 属性中的文本或图像进行样式设置。

HTML

html
<span class="ribbon">Notice where the orange box is.</span>

CSS

css
.ribbon {
  background-color: #5bc8f7;
}

.ribbon::before {
  content: "Look at this orange box.";
  background-color: #ffba10;
  border-color: black;
  border-style: dotted;
}

结果

待办事项列表

在此示例中,我们将使用伪元素创建一个待办事项列表。此方法通常可用于为 UI 添加小细节并改善用户体验。

HTML

html
<ul>
  <li>Buy milk</li>
  <li>Take the dog for a walk</li>
  <li>Exercise</li>
  <li>Write code</li>
  <li>Play music</li>
  <li>Relax</li>
</ul>

CSS

css
li {
  list-style-type: none;
  position: relative;
  margin: 2px;
  padding: 0.5em 0.5em 0.5em 2em;
  background: lightgrey;
  font-family: sans-serif;
}

li.done {
  background: #ccff99;
}

li.done::before {
  content: "";
  position: absolute;
  border-color: #009933;
  border-style: solid;
  border-width: 0 0.3em 0.25em 0;
  height: 1em;
  top: 1.3em;
  left: 0.6em;
  margin-top: -1em;
  transform: rotate(45deg);
  width: 0.5em;
}

JavaScript

js
const list = document.querySelector("ul");
list.addEventListener("click", (ev) => {
  if (ev.target.tagName === "LI") {
    ev.target.classList.toggle("done");
  }
});

这是上面代码示例的实时运行效果。请注意,没有使用图标,而复选标记实际上是使用 CSS 样式化的 ::before。现在就去完成一些事情吧。

结果

Unicode 转义序列

由于生成的内容是 CSS,而不是 HTML,因此您不能在内容值中使用标记实体。如果您需要使用特殊字符,但无法将其直接输入到 CSS 内容字符串中,请使用 Unicode 转义序列。它由反斜杠后跟字符的十六进制 Unicode 值组成。

HTML

html
<ol>
  <li>Crack Eggs into bowl</li>
  <li>Add Milk</li>
  <li>Add Flour</li>
  <li aria-current="step">Mix thoroughly into a smooth batter</li>
  <li>Pour a ladleful of batter onto a hot, greased, flat frying pan</li>
  <li>Fry until the top of the pancake loses its gloss</li>
  <li>Flip it over and fry for a couple more minutes</li>
  <li>serve with your favorite topping</li>
</ol>

CSS

css
li {
  padding: 0.5em;
}

li[aria-current="step"] {
  font-weight: bold;
}

li[aria-current="step"]::before {
  content: "\21E8 "; /* Unicode escape sequence for "Rightwards White Arrow" */
  display: inline;
}

结果

::before::marker 嵌套伪元素

::before::marker 嵌套伪元素选择其自身为列表项的 ::before 伪元素的列表::marker,即,其 display 属性设置为 list-item

在这个演示中,我们使用 ::before::after(将它们设置为 display: list-item,使其行为像列表项)在列表导航菜单之前和之后生成额外的 列表项。然后,我们使用 ul::before::markerul::after::marker 来为其列表标记设置不同的颜色。

HTML

html
<ul>
  <li><a href="#">Introduction</a></li>
  <li><a href="#">Getting started</a></li>
  <li><a href="#">Understanding the basics</a></li>
</ul>

CSS

css
ul {
  font-size: 1.5rem;
  font-family: "Helvetica", "Arial", sans-serif;
}

ul::before,
ul::after {
  display: list-item;
  color: orange;
}

ul::before {
  content: "Start";
}

ul::after {
  content: "End";
}

ul::before::marker,
ul::after::marker {
  color: red;
}

结果

虽然三个导航项的列表项目符号是因为它们是 <li> 元素而生成的,但“开始”和“结束”是通过伪元素插入的,并使用 ::marker 来设置它们的项目符号样式。

规范

规范
CSS 伪元素模块 Level 4
# 生成内容

浏览器兼容性

另见