CSS pseudo-elementPseudo-elements

::after

Creates a virtual element at the end of an element’s content. Ideal for arrows, badges, underlines, and overlays.

On this page

Example

#
CSS
.card {
  position: relative;
}

.card a::after {
  content: "";
  position: absolute;
  inset: 0;
}

.card:hover {
  outline: 2px solid #a78bfa;
}
Preview

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

#
Syntax
selector::after { content: …; }

Common uses

#
  • content: " ↗"

    An arrow after the text, typical for links that lead to other sites.
  • content: attr(data-time)

    Text combined with the value of the data-time attribute. You simply write the parts of content one after another.
  • :hover::after

    An underline that expands on hover. Hover over the link.
  • position: absolute

    A badge pinned to a corner of the element. The parent needs position: relative.

Common uses in practice

#

content: " ↗"

An arrow after the text, typical for links that lead to other sites.
.demo::after { content: " ↗"; }

content: attr(data-time)

Text combined with the value of the data-time attribute. You simply write the parts of content one after another.
.demo::after {
  content: " · " attr(data-time);
  color: #a1a1aa;
}

:hover::after

An underline that expands on hover. Hover over the link.
.demo::after {
  content: "";
  display: block;
  height: 2px;
  background: #a78bfa;
  transform: scaleX(0);
  transition: transform 0.25s;
}
.demo:hover::after { transform: scaleX(1); }

position: absolute

A badge pinned to a corner of the element. The parent needs 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

#
Tooltip bubble from an attribute
CSS
.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;
}
Preview

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 ::after with position: absolute; inset: 0 extends a link’s clickable area to the whole parent. It is a simple way to make clickable cards.
  • When an ::after overlay blocks text selection or clicks, add pointer-events: none to it.
  • You can style both pseudo-elements at once in a single rule: .x::before, .x::after { … }.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.