bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/CSS/CSS Foundations Practice
CSS•CSS Foundations Practice

CSS Nested Counters

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind CSS Nested Counters?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___-reset: section;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

CSS Counter Properties
The CSS counters() Function
CSS Using Two Counters

CSS Using Two Counters

You can create multiple counters for different levels of numbering. The following example creates one counter for the page (named "section") and one counter for each <h1> element (named "subsection"):

Example

Formatted code
body {
  counter-reset: section;
}
h1 {
  counter-reset: subsection;
}
h1::before {
  counter-increment: section;
  content: "Section " counter(section) ". ";
}
h2::before {
  counter-increment: subsection;
  content: counter(section) "." counter(subsection) " ";
}

Live preview

The CSS counters() Function

The counters() function returns the current values of the named and nested counters, as a string.

Here we use the counters() function to insert a string between different levels of nested counters:

Example

Formatted code
ol {
  counter-reset: section;
  list-style-type: none;
}
li::before {
  counter-increment: section;
  content: counters(section,".") " ";
}

Live preview

CSS Counter Properties

PropertyDescription
contentUsed with the ::before and ::after pseudo-elements, to insert generated content
counter-incrementIncrements one or more counter values
counter-resetCreates or resets one or more counters
counter()Returns the current value of the named counter
counters()Returns the current values of the named and nested counters

Previous

CSS Fonts

Next

CSS Performance Optimization