::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://web.dev/">web.dev</a>,
  <a href="https://w3schools.tw/">w3schools.com</a> or our
  <a href="https://mdn.club.tw/" 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
# 生成內容

瀏覽器相容性

另見