Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS counter output mismatch

Tags:

Could someone please explain why in the section counter value is printed 0 in all h2 tags?

Here is the source code:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    counter-reset: section;
    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>

<h1>Scripting tutorials:</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials:</h1>
<h2>XML</h2>
<h2>XSL</h2>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>

</body>
</html>
like image 597
raven03 Avatar asked Sep 19 '18 14:09

raven03


2 Answers

The hidden trick here is that you are overriding a property. You are using counter-reset twice in the same block so only the last one will be considered, that's why the subsection is ok and not the section.

enter image description here

If you move one of the counter-reset to the html for example it will behave like intended:

body {
  counter-reset: section;
}

html {
  counter-reset: subsection;
}

h1::before {
  counter-increment: section;
  content: "Section " counter(section) ". ";
}

h2::before {
  counter-increment: subsection;
  content: counter(section) "." counter(subsection) " ";
}
<h1>HTML tutorials:</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>

<h1>Scripting tutorials:</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials:</h1>
<h2>XML</h2>
<h2>XSL</h2>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>

Or use you can reset both counter with the same property:

body {
  counter-reset: section subsection;
}


h1::before {
  counter-increment: section;
  content: "Section " counter(section) ". ";
}

h2::before {
  counter-increment: subsection;
  content: counter(section) "." counter(subsection) " ";
}
<h1>HTML tutorials:</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>

<h1>Scripting tutorials:</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials:</h1>
<h2>XML</h2>
<h2>XSL</h2>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>
like image 136
Temani Afif Avatar answered Oct 11 '22 12:10

Temani Afif


In your solution there are two counter-reset properties in the body tag:

body {
    counter-reset: section;     
    counter-reset: subsection;
}

As the name suggests, CSS are cascading, therefore the second occurrence of the counter-reset property overrides the first one. As a result counter-reset: section; will be overwritten by counter-reset: subsection; and section reset won't be defined at all.

Solutions

  1. Move the subsection counter into h1 tag. Then the subsection counter will reset for each h1 tag:

    body {
        counter-reset: section; 
    }
    
    h1 {
        counter-reset: section; 
    }
    

<!DOCTYPE html>
<html>
<head>
<style>
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>

<h1>Scripting tutorials:</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials:</h1>
<h2>XML</h2>
<h2>XSL</h2>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>

</body>
</html>
  1. Define the subsection counter inline with the section counter, as @Temani suggested. Then the subsection counter won't reset:

    body {
        counter-reset: section subsection; 
    }
    

<!DOCTYPE html>
<html>
<head>
<style>
body {
    counter-reset: section 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>

<h1>Scripting tutorials:</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials:</h1>
<h2>XML</h2>
<h2>XSL</h2>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>

</body>
</html>
like image 37
FIL Avatar answered Oct 11 '22 14:10

FIL