::after
Creates a virtual element at the end of an element’s content. Ideal for arrows, badges, underlines, and overlays.
Example
#.card {
position: relative;
}
.card a::after {
content: "";
position: absolute;
inset: 0;
}
.card:hover {
outline: 2px solid #a78bfa;
}<style>
.card { padding: 14px; border-radius: 12px; background: #18181b; }
.card a { color: #fff; font-weight: 600; text-decoration: none; }
.card p { margin: 4px 0 0; color: #a1a1aa; }
</style>
<div class="card">
<a href="#">CSS course from scratch</a>
<p>Hover anywhere over the card: the whole thing is a clickable link.</p>
</div>The link’s invisible ::after covers the entire card, so clicking anywhere works like clicking the title.
Definition and usage
#The ::after pseudo-element works like ::before, except that it inserts a new child at the end of the element’s content, after everything in the HTML. It also requires the content property and is also an inline element by default.
Typical uses are marks after text (an arrow next to an external link, an asterisk next to a required field), tooltip bubbles, and overlays. A pseudo-element with position: absolute and inset: 0 covers the whole element, which is useful for darkening a photo or stretching a link’s clickable area across an entire card.
An element can have both ::before and ::after, so you get two extra boxes without touching the HTML. A positioned ::after is painted above the rest of the element’s content. If an overlay covers text or captures clicks, z-index and pointer-events: none can help.
Syntax
#selector::after { content: …; }Common uses
#- An arrow after the text, typical for links that lead to other sites.
- Text combined with the value of the
data-timeattribute. You simply write the parts ofcontentone after another. - An underline that expands on hover. Hover over the link.
- A badge pinned to a corner of the element. The parent needs
position: relative.
Common uses in practice
#content: " ↗"
.demo::after { content: " ↗"; }content: attr(data-time)
data-time attribute. You simply write the parts of content one after another..demo::after {
content: " · " attr(data-time);
color: #a1a1aa;
}:hover::after
.demo::after {
content: "";
display: block;
height: 2px;
background: #a78bfa;
transform: scaleX(0);
transition: transform 0.25s;
}
.demo:hover::after { transform: scaleX(1); }position: absolute
position: relative..demo { position: relative; }
.demo::after {
content: "NOWE";
position: absolute;
top: -14px;
right: -30px;
padding: 1px 6px;
border-radius: 6px;
background: #f43f5e;
color: #fff;
font-size: 10px;
font-weight: 700;
}More examples
#.tip {
position: relative;
}
.tip:is(:hover, :focus)::after {
content: attr(data-tip);
position: absolute;
bottom: calc(100% + 6px);
left: 50%;
translate: -50% 0;
padding: 4px 8px;
border-radius: 6px;
background: #3f3f46;
color: #fff;
font-size: 12px;
white-space: nowrap;
}<p style="margin: 34px 0 0;">For every lesson you get <span class="tip" tabindex="0" data-tip="Experience points" style="border-bottom: 1px dashed #a1a1aa; cursor: help;">EXP</span>.</p>Hover over the abbreviation EXP or reach it with the Tab key. The tooltip text comes from the data-tip attribute.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- An invisible
::afterwithposition: absolute; inset: 0extends a link’s clickable area to the whole parent. It is a simple way to make clickable cards. - When an
::afteroverlay blocks text selection or clicks, addpointer-events: noneto it. - You can style both pseudo-elements at once in a single rule:
.x::before, .x::after { … }.
Related entries
#- ::beforeCreates a virtual element at the start of an element’s content. Used for icons, decorations, and labels without extra HTML.
- contentInserts generated content in the ::before and ::after pseudo-elements.
- :hoverMatches the element the mouse cursor is currently over. The basis of hover effects on buttons, links, and cards.
- positionChooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
- insetA shorthand that sets top, right, bottom, and left in a single declaration.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.