content

Inserts generated content in the ::before and ::after pseudo-elements.

Initial value
normal
Inherited
No
Animatable
no
Applies to
::before and ::after pseudo-elements (partly also ordinary elements)
JavaScript
getComputedStyle(element, "::before").content
On this page

Example

#
CSS
.external::after {
  content: " ↗";
  font-size: 0.8em;
}

.required label::after {
  content: " *";
  color: #f43f5e;
}
Preview

Definition and usage

#

The content property specifies what appears in the ::before and ::after pseudo-elements. Without it, these pseudo-elements aren’t created at all, so even decorations without text require content: "".

The value can be a string, an element attribute value (attr(data-label)), a counter (counter()), a quotation mark (open-quote), or an image (url()).

Content from content is for ornaments, icons, and helper labels. Screen readers may read it, but it can’t be selected or copied, so don’t put important information there.

Syntax

#
Syntax
content: normal | none | "<string>" | attr(<attribute>) | counter(…) | open-quote | close-quote | url(…) [ / "<alt text>" ]?;

Values

#
  • "★ "

    string

    A fixed string before the content.
  • attr(data-label)

    The value of the element’s data-label attribute.
  • open-quote

    An opening quotation mark, depending on the page language.
  • ""

    empty string

    An empty pseudo-element, e.g. as a coloured bar.

Values in practice

#

"★ "

A fixed string before the content.
.demo::before { content: "★ "; }

attr(data-label)

The value of the element’s data-label attribute.
.demo::before { content: attr(data-label); }

open-quote

An opening quotation mark, depending on the page language.
.demo::before { content: open-quote; }
.demo::after { content: close-quote; }

""

An empty pseudo-element, e.g. as a coloured bar.
.demo::before { content: ""; display: inline-block; width: 24px; height: 4px; vertical-align: middle; 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

#
  • Remember content: "" on every decorative pseudo-element, otherwise you won’t see it on the page.
  • Don’t insert content via content without which the page is incomprehensible. Such text can’t be copied.

Related entries

#

Practise it in lessons

#

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