How to Add Automatic Chapter Numbers with CSS Counters

Use counter-reset to set a starting value, counter-increment to advance it, and counter() in the content of ::before to display it. In this example the chapters are 1 and 2, and the sections are 1.1, 1.2, 2.1, and 2.2.

Reset, increment, and display a counter

Initialize the named counter on the container with counter-reset: chapter 0. Each chapter’s counter-increment: chapter adds the default amount of 1. That makes the first displayed value 1. counter() reads the value. Incrementing without outputting it does not display a number. CSS Lists and Counters · Automatic Numbering

Build the complete chapter and section example

Save the code as css-counters.html and open it. main owns the overall chapter counter. Each .chapter starts a new topic counter at zero. The section pseudo-element joins the current chapter and topic values with a dot.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Number chapters and sections with CSS counters</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    main { counter-reset: chapter 0; max-width: 650px; }
    .chapter { counter-increment: chapter; counter-reset: topic 0; }
    .chapter > h2::before { content: "Chapter " counter(chapter) ". "; color: #126f86; }
    .chapter h3 { counter-increment: topic; }
    .chapter h3::before { content: counter(chapter) "." counter(topic) " "; color: #126f86; }
  </style>
</head>
<body>
<main>
  <h1>CSS learning outline</h1>
  <section class="chapter">
    <h2>Selectors</h2>
    <h3>Class selectors</h3>
    <p>Select elements by their class attribute.</p>
    <h3>ID selectors</h3>
    <p>Select elements by their id attribute.</p>
  </section>
  <section class="chapter">
    <h2>Box model</h2>
    <h3>Padding</h3>
    <p>Padding sets the space between the content and border.</p>
    <h3>Margin</h3>
    <p>Margin sets the space outside the element.</p>
  </section>
</main>
</body>
</html>

The first section of the second chapter is 2.1 because the topic counter restarts in each chapter. Insert another h3 in the first chapter to see later sections in that chapter shift. The second chapter still starts at 2.1.

Change the starting number, step, and format

Set counter-reset: chapter 4 to get chapters 5 and 6, since the first increment adds one. Restore zero and try counter-increment: chapter 2 to get 2 and 4. Restore the step of one before trying the rule below, which displays I and II on the chapter headings. CSS Counter Styles · upper-roman

.chapter > h2::before {
  content: "Chapter " counter(chapter, upper-roman) ". ";
}

Keep display numbering separate from document structure

Roman numerals change the output format. Add a style to counter(chapter) in the section rule as well if both levels need it. Store meaningful identifiers in HTML or data and use <ol> with <li> for an ordered list. If your theme already numbers the headings, avoid adding a second counter to the same elements. HTML Standard · ol