CSS ::before and ::after Pseudo-Elements (With Examples)

By Marko Denic
Contents
  1. How ::before and ::after work
  2. The content property is required
  3. ::before vs ::after
  4. Single or double colon?
  5. They're inline by default
  6. Practical examples
  7. Required field asterisk
  8. Tooltips from a data attribute
  9. Numbered headings with counters
  10. Image overlay and gradient borders
  11. Limitations
  12. FAQ

::before and ::after are pseudo-elements: they let you add content to an element with CSS alone, without touching the HTML. ::before inserts it as the element’s first child, ::after as its last child.

They only appear if you set the content property, even to an empty string. Everything below uses that one rule.

Demo:

Simplicity is the soul of efficiency.

Hover me
  • Custom bullets
  • No images
Inbox

None of the quote mark, underline, check marks or badge exist in the HTML. Hover the link to see the underline animate. A big quote mark like this is a common way to style a blockquote.

How ::before and ::after work

The content property is required

Without content, the pseudo-element isn’t created at all. Use an empty string for purely visual shapes, text for labels, or attr() to pull in an attribute’s value.

css
.note::before {
  content: "Note: ";   /* text */
}

.divider::after {
  content: "";         /* empty: just a box to style */
  display: block;
  height: 1px;
  background: #e2e8f0;
}

a[href^="http"]::after {
  content: " \2197";   /* unicode escape: ↗ */
}

::before vs ::after

The only difference is position. ::before is rendered before the element’s content, ::after after it. With position: absolute you can place either one anywhere, so pick whichever reads better. When you need two decorations on one element, use both.

html
<!-- What the browser renders for <p class="price">49</p> -->
<p class="price">
  ::before   <!-- content: "$" -->
  49
  ::after    <!-- content: " USD" -->
</p>

Single or double colon?

The CSS3 syntax uses two colons (::before) to tell pseudo-elements apart from pseudo-classes like :hover. The old single-colon syntax (:before) still works everywhere for backwards compatibility. Use the double colon.

They're inline by default

A pseudo-element starts as an inline box, so width and height don’t apply until you change its display or make it position: absolute. To place it relative to its element, give the element position: relative. Here’s how absolute and relative positioning work together.

css
.card {
  position: relative; /* the pseudo-element is placed inside this */
}

.card::before {
  content: "";
  position: absolute;
  inset: 0;           /* cover the whole card */
  background: linear-gradient(transparent, rgb(0 0 0 / 0.6));
}

Practical examples

Required field asterisk

Mark required fields once in CSS instead of in every label.

css
label.required::after {
  content: " *";
  color: #dc2626;
}

Tooltips from a data attribute

attr() reads an attribute into content, so the tooltip text lives in the HTML. See the CSS tooltip demo for the full version.

css
.tooltip {
  position: relative;
}

.tooltip:hover::after {
  content: attr(data-tip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  translate: -50% -6px;
  padding: 4px 8px;
  border-radius: 6px;
  background: #0f172a;
  color: #fff;
  white-space: nowrap;
}

Numbered headings with counters

CSS counters plus ::before number your headings automatically, and the numbers update when you add or remove sections.

css
article {
  counter-reset: section;
}

article h2::before {
  counter-increment: section;
  content: counter(section) ". ";
  color: #3b82f6;
}

Image overlay and gradient borders

An absolutely positioned pseudo-element is the classic way to put a tint over an image, or a gradient behind a card for an animated gradient border. z-index: -1 puts it behind the content.

css
.hero {
  position: relative;
  isolation: isolate; /* keeps z-index: -1 inside .hero */
}

.hero::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: rgb(15 23 42 / 0.55);
}

Limitations

  • No pseudo-elements on replaced elements. <img>, <input>, <video> and <iframe> have no content box, so ::before and ::after don’t show up. Wrap them in a <span> or <div> and style the wrapper.
  • JavaScript can’t select them. querySelector won’t find a pseudo-element. Change them by toggling a class or a CSS variable on the element.
  • Screen readers may read the content. Text in content is usually announced. Keep real information in the HTML, and give decorative symbols empty alternative text: content: "★" / "";
  • One of each per element. You get exactly one ::before and one ::after.

FAQ

Why isn’t my ::before or ::after showing?

Almost always a missing content property. Add content: "". If it’s there, check that it has a size (it’s inline by default) and that the element isn’t an img or input.

What’s the difference between a pseudo-element and a pseudo-class?

A pseudo-class (:hover, :first-child) selects an element in a certain state. A pseudo-element (::before, ::first-letter) creates or targets a part of an element that isn’t in the HTML.

Can I add HTML inside content?

No. content only accepts text, images (url()), counters and attribute values. Tags are shown as plain text.

Can I animate ::before and ::after?

Yes, with transitions and keyframe animations, like the underline in the demo. Animate transform and opacity for the smoothest result. A hamburger icon that turns into an X is built on the same transform transitions.