Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-elements

The ::before pseudo-element

Reference for the CSS ::before pseudo-element: insert generated content before an element's content, with the content property, examples, pitfalls and accessibility.

The ::before pseudo-element creates a generated box as the first child of the element you select. It exists only in CSS, not in the HTML, and it appears only when its content property has a value other than normal or none. Use it for decoration, icons, quote marks, badges and small visual effects that don't belong in the document itself.

By default the generated box is display: inline, so you can put text in it, or give it content: "" and style it as a shape with a size, background and position.

CSS
.note::before {
  content: "Note: ";
  font-weight: 700;
}

Try it

Text, shapes and counters before content

Syntax

CSS
selector::before {
  content: <value>;
  /* any other declarations */
}
  • Initial value: a pseudo-element is not generated until content is set.
  • Applies to: any element that can have children. Not generated on replaced elements such as <img>, <input> or <br>.
  • Inherited: no, but the box inherits inherited properties (such as color and font) from its originating element.
  • Animatable: it can be styled and transitioned like any box. content itself is not animatable.
  • Specificity: counts as a type selector (0,0,1) added to the rest of the selector.

Values

The pseudo-element takes no arguments; what it shows comes from content.

Value of content Description
"text" Inserts the string. Use "\201C"-style escapes for special characters.
"" Generates an empty box, the usual way to draw shapes, overlays and lines.
attr(data-label) Inserts the value of an attribute from the element.
counter(name) Inserts the current value of a CSS counter.
url(image.svg) Inserts an image (sized to the image's own dimensions).
open-quote, close-quote Inserts the quote marks defined by the quotes property.
"text" / "alt" Adds alternative text for assistive technology, or / "" to mark it decorative (where supported).
none, normal No pseudo-element is generated.

Usage notes

  • Without content, nothing appears. The most common mistake is forgetting content: "" on a decorative shape.
  • Write two colons. ::before is the modern syntax; the single-colon :before form still works for old pages, but use the two-colon form.
  • It is the first child, not a sibling. The box goes inside the element, before its content. To place something after, use ::after.
  • Positioning. Give the parent position: relative and the pseudo-element position: absolute for overlays, badges and corner ribbons.
  • Not in the DOM. Users can't select or copy generated text, scripts can't query it, and search engines don't index it. Put real content in HTML.
  • Accessibility. Some screen readers read generated text. Keep it decorative, use alt text after a slash where supported (content: "โ˜…" / ""), and never use it for important information.
  • Can't be used on replaced elements. To decorate an image, wrap it in an element and put the pseudo-element on the wrapper.
  • Clicks. A pseudo-element is part of its element for pointer events; a large overlay can block clicks, so add pointer-events: none when it should be click-through.

Browser support

Supported in every browser; the two-colon syntax works in all current ones. Alternative text after a slash in content is newer, so don't depend on it for essential meaning.