試一試
a::after {
content: " (" attr(href) ")";
}
.dead-link {
text-decoration: line-through;
}
.dead-link::after {
content: url("/shared-assets/images/examples/warning.svg");
display: inline-block;
width: 12px;
height: 12px;
}
<p>
The sailfish is named for its sail-like dorsal fin and is widely considered
the fastest fish in the ocean.
<a href="https://en.wikipedia.org/wiki/Sailfish"
>You can read more about it here</a
>.
</p>
<p>
The red lionfish is a predatory scorpionfish that lives on coral reefs of the
Indo-Pacific Ocean and more recently in the western Atlantic.
<a href="" class="dead-link">You can read more about it here</a>.
</p>
語法
::after {
content: /* value */;
/* properties */
}
描述
::after 偽元素是一個內聯框,作為與其關聯的元素(或“源元素”)的直接子元素生成。它通常透過 content 屬性向元素新增裝飾性內容,例如圖示、引號或其他裝飾。
::after 偽元素不能應用於替換元素,例如 <img>,其內容由外部資源決定,不受當前文件樣式的影響。
display 值設定為 list-item 的 ::after 偽元素表現得像一個列表項,因此可以像 <li> 元素一樣生成一個 ::marker 偽元素。
如果未指定 content 屬性,或者它的值無效,或者它的值為 normal 或 none,那麼 ::after 偽元素將不會被渲染。它的行為就好像設定了 display: none。
預設情況下,::before 和 ::after 偽元素與其父元素共享相同的堆疊上下文。如果未明確設定 z-index,則 ::after 偽元素生成的內容將出現在 ::before 偽元素生成的內容之上,因為 ::after 在 DOM 流中渲染得更晚。
無障礙
不建議使用 ::after 偽元素新增內容,因為螢幕閱讀器無法可靠地訪問它。
示例
基本用法
我們來建立兩個類:一個用於普通的段落,一個用於令人興奮的段落。我們可以使用這些類在段落末尾新增偽元素。
HTML
<p class="boring-text">Here is some plain old boring text.</p>
<p>Here is some normal text that is neither boring nor exciting.</p>
<p class="exciting-text">Contributing to MDN is easy and fun.</p>
CSS
.exciting-text::after {
content: " <- EXCITING!";
color: darkgreen;
font-weight: bolder;
}
.boring-text::after {
content: " <- BORING";
color: darkviolet;
font-weight: bolder;
}
結果
裝飾性示例
我們可以用我們想要的幾乎任何方式來樣式化 content 屬性中的文字或影像。
HTML
<span class="ribbon">Look at the orange box after this text. </span>
CSS
.ribbon {
background-color: #5bc8f7;
}
.ribbon::after {
content: "This is a fancy orange box.";
background-color: #ffba10;
border-color: black;
border-style: dotted;
}
結果
工具提示
此示例使用 ::after,結合 attr() CSS 表示式和 data-description 自定義資料屬性,來建立工具提示。無需 JavaScript!
我們還可以透過這種技術支援鍵盤使用者,方法是新增 tabindex 為 0 使每個 span 都能透過鍵盤聚焦,並使用 CSS :focus 選擇器。這表明 ::before 和 ::after 可以多麼靈活,儘管為了獲得最無障礙的體驗,透過其他方式建立的語義化顯示小部件(例如使用 details 和 summary 元素)可能更合適。
HTML
<p>
Here we have some
<span tabindex="0" data-description="collection of words and punctuation">
text
</span>
with a few
<span tabindex="0" data-description="small popups that appear when hovering">
tooltips</span
>.
</p>
CSS
span[data-description] {
position: relative;
text-decoration: underline;
color: blue;
cursor: help;
}
span[data-description]:hover::after,
span[data-description]:focus::after {
content: attr(data-description);
position: absolute;
left: 0;
top: 24px;
min-width: 200px;
border: 1px #aaaaaa solid;
border-radius: 10px;
background-color: #ffffcc;
padding: 12px;
color: black;
font-size: 14px;
z-index: 1;
}
結果
::after::marker 巢狀偽元素
::after::marker 巢狀偽元素選擇 ::after 偽元素的列表 ::marker,該偽元素本身是一個列表項,也就是說,它的 display 屬性設定為 list-item。
在此演示中,我們使用 ::before 和 ::after 在列表導航選單之前和之後生成額外的列表項(將它們設定為 display: list-item,使它們表現得像列表項)。然後我們使用 ul::before::marker 和 ul::after::marker 給它們的列表標記不同的顏色。
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
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> 元素而生成的,但“Start”和“End”是透過偽元素插入的,並使用 ::marker 來樣式化它們的專案符號。
規範
| 規範 |
|---|
| CSS 偽元素模組 Level 4 # 生成內容 |
瀏覽器相容性
載入中…