CSS ::before and ::after Pseudo-Elements (With Examples)
Contents
::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.
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.
.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.
<!-- 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.
.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.
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.
.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.
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.
.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::beforeand::afterdon’t show up. Wrap them in a<span>or<div>and style the wrapper. - JavaScript can’t select them.
querySelectorwon’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
contentis 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
::beforeand 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.