CSS Reference ยท Pseudo-elements
The ::after pseudo-element
Reference for the CSS ::after pseudo-element: add generated content as the last child of an element, with the content property, icons, badges and clearfix tips.
The ::after pseudo-element creates a box as the last child of the selected element. It is invisible until you give it a content value, and it is used for decorations that do not belong in the HTML: arrows, badges, underline bars, quotation marks, or tiny overlays.
The generated box is inline by default, can be styled and positioned like any other element, and cannot be selected or focused by the user. Screen readers may announce text generated with content, so keep it decorative.
.external-link::after { content: " โ"; }
Try it
The empty-string content: "" is the common trick for purely visual shapes: the box exists, and you size and color it yourself.
Syntax
selector::after { content: <value>; /* any other declarations */ }
| Property | Value |
|---|---|
Initial display of the box |
inline |
| Applies to | Elements that can have children (not replaced elements such as <img> or <input>) |
| Inherited | Inherits from the originating element, like a child |
| Animatable | Properties of the box can be animated and transitioned |
Values
The pseudo-element takes no arguments. What it shows comes from the content property.
content value |
Description |
|---|---|
"" |
An empty box, for shapes, overlays and clearfix. |
"text" |
A string. Use escapes such as "\2197" for special characters. |
attr(data-label) |
The value of an attribute of the element. |
counter(name) |
The current value of a CSS counter. |
url(...) |
An image. |
none / normal |
No pseudo-element is generated. |
Usage notes
- No
content, no box. Ifcontentis missing ornone, nothing is generated. This is the most common reason a rule seems to do nothing. - Not for replaced elements.
<img>,<input>,<br>and similar elements have no children, so::afterdoes not work on them. Put the effect on a wrapper. - Position it. To overlay, make the parent
position: relativeand the pseudo-elementposition: absolute. - Clearfix.
.group::after { content: ""; display: block; clear: both; }makes a container wrap floated children. Modern layout (flexbox, grid,display: flow-root) usually removes the need. - Accessibility. Text in
contentmay be read aloud and cannot be selected or translated reliably. Never put essential information there. A CSS alternative text is possible withcontent: "x" / "". - Single-colon form.
:afterworks for historical reasons; write::afterto tell pseudo-elements from pseudo-classes. - Can't be targeted by JavaScript directly. Change the element's class or a custom property instead.
Browser support
Supported in every browser, including the double-colon syntax.
Related
::beforeโ the first-child counterpartcontentโ what the pseudo-element shows::marker- Lesson: Pseudo-elements