试一试
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 # 生成内容 |
浏览器兼容性
加载中…