Skip to main content

Modern CSS Authoring

Generated Content

Written by Updated Generated Content exercises

Add presentational markers and automatic numbering with pseudo-elements, content, and counters. You will create decorative pseudo-element content, automatic numbering, custom marker systems, and safer list markers with ::marker.

CSS can add small bits of content and count things for you: an arrow after every external link, "Step 1:", "Step 2:" in front of each instruction, numbered figures that renumber themselves when you add one. That is generated content and counters.

Both rely on pseudo-elements like ::before, ::after and ::marker, explained in CSS Pseudo Elements. For simple bullets and numbers, the list properties in CSS Lists are enough.

Add content and count steps

Example

css

.external-link::after {
  content: " ↗";
}

.steps {
  counter-reset: step;
}

.steps > li {
  counter-increment: step;
}

.steps > li::marker {
  content: "Step " counter(step) ": ";
}

The first rule adds an arrow after every external link. The rest creates a counter called step: counter-reset starts it at zero on the list, counter-increment adds one for each item, and counter(step) prints the current number inside the item's marker. Add or remove a step and the numbers update themselves.

Generated content is for decoration and repeated labels. Text added with CSS cannot be selected or copied on most browsers, and screen readers may or may not read it, so never put real information there.

Your own numbering style

Example

css

@counter-style circled {
  system: fixed;
  symbols: ① ② ③ ④ ⑤;
  suffix: " ";
}

ol { list-style: circled; }

@counter-style defines a new list style. Here the numbers are circled digits. After five items this fixed set runs out and the list falls back to normal numbers.

Generated Content Properties

Property or selectorPurposeExample
contentSupplies generated content for a pseudo-element.content: "Note: "
counter-resetCreates or resets a named counter.counter-reset: section
counter-incrementAdvances a named counter.counter-increment: section
::markerStyles a list item's marker box.li::marker { color: red; }

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Generated Content example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.external-link::after {
  content: " ↗";
}

.steps {
  counter-reset: step;
}

.steps > li {
  counter-increment: step;
}

.steps > li::marker {
  content: "Step " counter(step) ": ";
}

@counter-style circled {
  system: fixed;
  symbols: ① ② ③ ④ ⑤;
  suffix: " ";
}

ol { list-style: circled; }
  </style>
</head>
<body>
  <main><ol class="steps"><li>Prepare the markup</li><li>Add the counter</li><li>Style the marker</li></ol><a class="external-link" href="https://example.com">External reference</a></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
::marker86866811.1
@counter-style91913317

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Good to know

  • For list numbers, style ::marker rather than hiding the marker and drawing your own with ::before. It keeps the list a real list.
  • content: "" with a size and background is a common way to draw small decorative shapes without extra HTML.
  • Counters can be nested: with counters(section, ".") you get outline numbers like 1, 1.1, 1.2, 2.

Generated Content exercises

Start a counter

Start a counter named sec on the body.

The HTML your code works on
<ol class="list">
  <li>One</li>
  <li>Two</li>
</ol>
<h2 class="sec">Intro</h2>
<h2 class="sec">Setup</h2>

Result

Check your answer to see it here.