w3resource

CSS Properties: How to set a number sections and sub-sections with section?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<title>How to set a number sections and sub-sections with section</title>
<style type="text/css">
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) " ";
}
</style>
</head>
<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h2>HTML5 Tutorial</h2>
<h1>Database tutorials</h1>
<h2>SQL</h2>
<h2>MySQL</h2>
<h2>PostgreSQL</h2>
<h1>Exercises</h1>
<h2>HTML CSS</h2>
<h2>JavaScript</h2>
<h2>jQuery</h2>
<h2>PHP</h2>
</body>
</html>

Live Demo:

See the Pen counter-reset-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes Yes

Go to Exercise page

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts