CSS

::after

::after

在CSS中,::after创建一个位于所选元素的最后一个子元素位置上的伪元素。它通常用于将内容添加到具有content属性的元素。它是默认内联的。

/* Add an arrow after links */ a::after { content: "→"; }

语法

/* CSS3 syntax */ ::after /* CSS2 syntax */ :after

CSS3引入了::after符号(用两个冒号)来区分伪类和伪元素。在CSS2中引入后,浏览器也接受:after

实例

简单用法

我们创建两个类:一个用于无聊的段落,另一个用于令人兴奋的段落。然后,我们可以通过在其末尾添加一个伪元素来标记每个段落。

<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. Just hit the edit button to add new live samples, or improve existing samples.</p>

.exciting-text::after { content: "<- now this *is* exciting!"; color: green; } .boring-text::after { content: "<- BORING!"; color: red; }

输出

装饰实例

我们可以在content属性中以任意方式设置文本或图像的样式。

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

.ribbon { background-color: #5BC8F7; } .ribbon::after { content: "Look at this orange box."; background-color: #FFBA10; border-color: black; border-style: dotted; }

输出

提示信息

以下示例显示了如何将::after伪元素与attr()CSS表达式和data-descr 自定义数据属性相结合来创建纯CSS的词汇表式的提示信息。您可以参看下面的实时预览,或者您可以在单独的页面上查看此示例。

<p>Here is the live example of the above code.<br /> We have some <span data-descr="collection of words and punctuation">text</span> here with a few <span data-descr="small popups which also hide again">tooltips</span>.<br /> Don't be shy, hover over to take a <span data-descr="not to be taken literally">look</span>. </p>

span[data-descr] { position: relative; text-decoration: underline; color: #00F; cursor: help; } span[data-descr]:hover::after { content: attr(data-descr position: absolute; left: 0; top: 24px; min-width: 200px; border: 1px #aaaaaa solid; border-radius: 10px; background-color: #ffffcc; padding: 12px; color: #000000; font-size: 14px; z-index: 1; }

输出

规范

SpecificationStatusComment
CSS Pseudo-Elements Level 4The definition of '::after' in that specification.Working DraftNo significant changes to the previous specification.
CSS TransitionsThe definition of 'transitions on pseudo-element properties' in that specification.Working DraftAllows transitions on properties defined on pseudo-elements.
CSS AnimationsThe definition of 'animations on pseudo-element properties' in that specification.Working DraftAllows animations on properties defined on pseudo-elements.
Selectors Level 3The definition of '::after' in that specification.RecommendationIntroduces the two-colon syntax.
CSS Level 2 (Revision 1)The definition of '::after' in that specification.RecommendationInitial definition, using the one-colon syntax

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
:after support(Yes)(Yes)1.0 (1.7 or earlier)18.044.0
::after support(Yes)(Yes)1.5 (1.8)19.074.0
Support of animations and transitions26(Yes)4.0 (2.0)No supportNo supportNo support

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
:after support(Yes)(Yes)????
::after support(Yes)(Yes)????
Support of animations and transitions(Yes)(Yes)4.0 (4.0)No supportNo supportNo support

3.5版本之前的Firefox只实现了CSS 2.0版本的:after。不支持的属性包括positionfloatlist-style-*和一些显示属性。Firefox 3.5解除了这些限制。