首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

::after

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

代码语言:javascript
复制
/* Add an arrow after links */
a::after {
  content: "→";
}

语法

代码语言:javascript
复制
/* CSS3 syntax */
::after

/* CSS2 syntax */
:after

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

实例

简单用法

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

代码语言:javascript
复制
<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>
代码语言:javascript
复制
.exciting-text::after {
  content: "<- now this *is* exciting!"; 
  color: green;
}

.boring-text::after {
   content: "<- BORING!";
   color: red;
}

输出

装饰实例

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

代码语言:javascript
复制
<span class="ribbon">Notice where the orange box is.</span>
代码语言:javascript
复制
.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的词汇表式的提示信息。您可以参看下面的实时预览,或者您可以在单独的页面上查看此示例

代码语言:javascript
复制
<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>
代码语言:javascript
复制
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;
}

输出

规范

Specification

Status

Comment

CSS Pseudo-Elements Level 4The definition of '::after' in that specification.

Working Draft

No significant changes to the previous specification.

CSS TransitionsThe definition of 'transitions on pseudo-element properties' in that specification.

Working Draft

Allows transitions on properties defined on pseudo-elements.

CSS AnimationsThe definition of 'animations on pseudo-element properties' in that specification.

Working Draft

Allows animations on properties defined on pseudo-elements.

Selectors Level 3The definition of '::after' in that specification.

Recommendation

Introduces the two-colon syntax.

CSS Level 2 (Revision 1)The definition of '::after' in that specification.

Recommendation

Initial definition, using the one-colon syntax

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari (WebKit)

:after support

(Yes)

(Yes)

1.0 (1.7 or earlier)1

8.0

4

4.0

::after support

(Yes)

(Yes)

1.5 (1.8)1

9.0

7

4.0

Support of animations and transitions

26

(Yes)

4.0 (2.0)

No support

No support

No support

Feature

Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

:after support

(Yes)

(Yes)

?

?

?

?

::after support

(Yes)

(Yes)

?

?

?

?

Support of animations and transitions

(Yes)

(Yes)

4.0 (4.0)

No support

No support

No support

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

扫码关注腾讯云开发者

领取腾讯云代金券