In CSS, ::after
creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content
property. It is inline by default.
In CSS, ::after
creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content
property. It is inline by default.
Note: The pseudo-elements generated by ::before
and ::after
are contained by the element's formatting box, and thus don't apply to replaced elements such as <img>
, or to <br>
elements.
::after { /* ... */ }
Note: CSS introduced the ::after
notation (with two colons) to distinguish pseudo-classes from pseudo-elements. For backward compatibility, browsers also accept :after
, introduced earlier.
Let's create two classes: one for boring paragraphs and one for exciting ones. We can use these classes to add pseudo-elements to the end of paragraphs.
<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>
.exciting-text::after { content: " <- EXCITING!"; color: green; } .boring-text::after { content: " <- BORING"; color: red; }
We can style text or images in the content
property almost any way we want.
<span class="ribbon">Look at the orange box after this text. </span>
.ribbon { background-color: #5bc8f7; } .ribbon::after { content: "This is a fancy orange box."; background-color: #ffba10; border-color: black; border-style: dotted; }
This example uses ::after
, in conjunction with the attr()
CSS expression and a data-descr
custom data attribute, to create tooltips. No JavaScript is required!
We can also support keyboard users with this technique, by adding a tabindex
of 0
to make each span
keyboard focusable, and using a CSS :focus
selector. This shows how flexible ::before
and ::after
can be, though for the most accessible experience a semantic disclosure widget created in some other way (such as with details and summary elements) is likely to be more appropriate.
<p> Here we have some <span tabindex="0" data-descr="collection of words and punctuation"> text </span> with a few <span tabindex="0" data-descr="small popups that appear when hovering"> tooltips</span >. </p>
span[data-descr] { position: relative; text-decoration: underline; color: #00f; cursor: help; } span[data-descr]:hover::after, span[data-descr]:focus::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; }
Using an ::after
pseudo-element to add content is discouraged, as it is not reliably accessible to screen readers.
Specification |
---|
CSS Pseudo-Elements Module Level 4 # generated-content |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
::after |
11 | 1212 |
1.5["Before Firefox 57, Firefox had a bug where::after pseudo-elements were still generated, even if the content property value were set to normal or none .", "Before Firefox 3.5, only the CSS level 2 behavior of :after was supported, which disallowed position , float , list-style-* and some display properties."] |
98 | 74 | 44 | ≤37≤37 | 1818 |
4["Before Firefox 57, Firefox had a bug where::after pseudo-elements were still generated, even if the content property value were set to normal or none .", "Before Firefox 3.5, only the CSS level 2 behavior of :after was supported, which disallowed position , float , list-style-* and some display properties."] |
10.110.1 | 3.23.2 | 1.01.0 |
animation_and_transition_support |
26 | 12 | 4 | No | 15 | No | 4.4 | 26 | 4 | 14 | No | 1.5 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/::after