counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
Full code:
The following example creates a counter for the page (main - section) and a counter for each element
(subsection - subsection). Section counter will be counted for each element
with the " Section " content, and the subsection counter will be counted for each element
with "." :
body {
counter-reset: section;
}
h2 {
counter-reset: subsection;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ". ";
}
h3::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
Full code:
In addition, you can use the counter in CSS to create lists, which are also useful and appropriate, which can automatically number the list as you wish.
For example, you create a list with a counter as follows, use the extra function counters () to decentralize nested counters:
ol {
counter-reset: section;
list-style-type: none;
}
li::before {
counter-increment: section;
content: counters(section,".") " ";
}
Full code:
Previous article: Form - Form in CSS
Next lesson: Design Layout - Website layout in CSS