CSS / Reference / content: Generate Pseudo-Element Text and Decoration

The CSS content property supplies text, counters, or images to pseudo-elements such as ::before and ::after. For a decorative dot, set content: "" on ::before, then size and color the generated box. Keep essential status messages and control names in the HTML.

Syntax and common values

::before and ::after render before or after an element’s own content. A space-separated list of content values renders in order. For instance, " (" attr(data-label) ")" joins an opening parenthesis, the attribute value, and a closing parenthesis.

Value What it renders
"…" Text inside matching quotes. An empty string generates a box you can style without displaying text.
attr(data-label) The data-label attribute on the originating HTML element. A missing attribute supplies an empty string by default.
counter(chapter) The current value of a counter set up with declarations such as counter-reset and counter-increment.
open-quote / close-quote Quotation marks selected by quotes according to the current nesting level.
url(…) A generated image. It is sized differently from a background drawn in an explicitly sized box.
none / normal The initial value normal computes to none on ::before and ::after. Neither generates those pseudo-elements.

The content property is non-inherited. The rules for placement and values are defined in the W3C generated content specification.

Run an example with text and a decorative dot

Save the complete example as content-example.html in UTF-8 and open it in a browser. The first paragraph gets a prefix and an attribute-based suffix. The second uses the width, height, and background color of an empty pseudo-element to draw a dot.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>content comparison</title>
  <style>
    body { margin: 0; color: #20242b; background: #fff; font: 16px/1.7 system-ui, sans-serif; }
    main { padding: 24px; max-width: 820px; margin: auto; }
    h1 { font-size: 22px; margin: 0 0 20px; }
    .samples { display: flex; flex-wrap: wrap; gap: 24px; }
    .label { margin: 0 0 8px; font-weight: 700; }
    .target::before { content: "Note: "; color: #006c9c; font-weight: bold; }
    .target::after { content: " (" attr(data-label) ")"; }
    .reference::before {
      content: "";
      display: inline-block;
      width: 12px;
      height: 12px;
      border-radius: 50%;
      background: #006c9c;
      margin-inline-end: 6px;
    }
  </style>
</head>
<body>
  <main>
    <h1>content comparison</h1>
    <section class="samples">
      <div>
        <p class="label">String and attr()</p>
        <p class="target" data-label="New item">Body content</p>
      </div>
      <div>
        <p class="label">Decoration with empty content</p>
        <p class="reference">Body content</p>
      </div>
    </section>
  </main>
</body>
</html>

The first sample displays “Note: Body content (New item)”. In the second, a blue dot appears before “Body content”. The HTML text of each sample paragraph is “Body content”.

Browser example comparing generated text around a paragraph with a blue decorative dot before another paragraph

Hide the parentheses when the attribute is empty

Remove data-label from the first paragraph and attr() supplies an empty string. The literal parentheses still render. A narrower selector can suppress the entire suffix when the attribute is missing or empty.

.target[data-label]:not([data-label=""])::after {
  content: " (" attr(data-label) ")";
}

Replace the example’s .target::after rule with this rule. Delete the attribute or set data-label="" and the parentheses disappear. Set it back to “New item” to restore the suffix. If whitespace-only values should count as empty, trim those values when building the HTML.

Choose between generated images, counters, and quotes

A declaration like content: "Text" on an ordinary <p> cannot rewrite its text. Using an image value to visually replace the contents of an ordinary element is a separate use of the property.

For decorative icons at a fixed size, use the example’s empty box with background-image and background-size. Put the pseudo-element on a wrapper if you need to decorate a replaced element such as <img>. Use an <ol> for steps whose order matters. A CSS counter can add visual numbering to chapters or figures.

Generated content adds no text node to the HTML. Check how assistive technology exposes it, and put required status messages, control names, and alternative descriptions for informative images in the HTML. A slash can introduce alternative text for CSS-generated content, as described in the W3C section on alternative text for generated content.

Check the result in browser developer tools

Load the file and inspect each paragraph in the browser’s element inspector. Change one value at a time to isolate the selector, the attribute, or the styling of the generated box.

  1. Change only content on .reference::before from "" to none and then normal. The dot disappears even with its size and background still set. Restore "".
  2. Change data-label on the first sample to “Revised item”. Only the words inside the parentheses should change. Check that the selector matches and use the Computed panel to see which content value wins.
  3. Run document.querySelector('.target').textContent in the console. It returns “Body content”. If a phrase must carry meaning with CSS turned off, include it in the HTML.

The guide to styling with ::before and ::after explains where each pseudo-element appears. Set pairs of quotation marks for nested quotes with quotes.