CSS pseudo-elementPseudo-elements

::before

Creates a virtual element at the start of an element’s content. Used for icons, decorations, and labels without extra HTML.

On this page

Example

#
CSS
.alert {
  display: flex;
  align-items: center;
  gap: 10px;
}

.alert::before {
  content: "!";
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f59e0b;
  color: #0c0c10;
  font-weight: 800;
}
Preview

Definition and usage

#

The ::before pseudo-element inserts a new first child into the element that does not exist in the HTML. It is not created before the element but inside it, ahead of all the other content. By default it is an inline element (inline), but you can give it any display, size, background, and position.

It exists only if the content property is set. Without it the pseudo-element is not generated at all, so even a purely decorative shape needs content: "". In content you can insert text, a symbol, an attribute value (attr(data-label)), or a counter.

It usually won’t work on elements that have no content of their own, such as <img>, <input>, or <br>, because there is no room in them for an extra child. The older single-colon syntax (:before) still works, but the double colon distinguishes pseudo-elements from pseudo-classes.

Text from content may be read by screen readers, but it can’t be selected or copied. Use it for decoration and keep important information in the HTML.

Syntax

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

Common uses

#
  • content: "★ "

    A symbol before the text. The space inside the quotes separates it from the content.
  • content: attr(data-level)

    Content taken from the element’s data-level attribute. Change the attribute in the HTML and the label changes.
  • display: inline-block

    Empty content and explicit dimensions give you a status dot. Without content: "" the dot would not be created.
  • position: absolute

    A positioned pseudo-element draws a bar on the left edge, regardless of the text length.

Common uses in practice

#

content: "★ "

A symbol before the text. The space inside the quotes separates it from the content.
.demo::before { content: "★ "; color: #fbbf24; }

content: attr(data-level)

Content taken from the element’s data-level attribute. Change the attribute in the HTML and the label changes.
.demo::before {
  content: attr(data-level);
  margin-right: 8px;
  padding: 2px 6px;
  border-radius: 6px;
  background: #7c3aed;
  font-size: 12px;
  font-weight: 700;
}

display: inline-block

Empty content and explicit dimensions give you a status dot. Without content: "" the dot would not be created.
.demo::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 8px;
  border-radius: 50%;
  background: #10b981;
}

position: absolute

A positioned pseudo-element draws a bar on the left edge, regardless of the text length.
.demo { position: relative; padding-left: 14px; }
.demo::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  border-radius: 4px;
  background: #a78bfa;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Every pseudo-element needs content. If something doesn’t show up, first check that this declaration is in place.
  • When positioning ::before absolutely, give the parent position: relative. Otherwise the pseudo-element will be positioned relative to another ancestor.
  • For custom list bullets, ::marker or list-style-type is often more convenient. Keep ::before for decorations that a list doesn’t provide.

Related entries

#

Practise it in lessons

#

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