SO i was introduced to this topic today and i was playing around with the code .
<body>
<p class="ct">First make a variable increase every time a p element occurs.</p>
<p class="ct">Then insert the counter in front of all p elements</p>
<p class="ct"> counter in kh rews df</p>
</body>
in css
.ct {
counter-reset: myindex 2;
}
.ct::before {
content: "SECTION " counter(myindex) ": ";
}
I figured out that
-
When the 1st paragraph is being encountered then the code flows to first .ct style where counter is created which is starting from 2
-
then it goes to before element where along with the wrd SECTION the value of counter is inserted .
-
Then it goes to the 2nd paragraph and then the code flows to
first .ct style where counter is reset to 2 from the previous value and then it goes to before element where along with the wrd SECTION the current value of counter is inserted .
And for the other paragraph same case.
<body>
<p class="ct">First make a variable increase every time a p element occurs.</p>
<p class="ct">Then insert the counter in front of all p elements</p>
<p class="ct"> counter in kh rews df</p>
</body>
IN css :
body {
counter-reset: myindex 2;
}
.ct::before {
content: "SECTION " counter(myindex) ": ";
}
-
When the code encounters the body part then the counter is created starting from 2 .
-
When the 1st paragraph is being encountered then the code flows to .ct style :: before element where along with the wrd SECTION the value of counter is inserted .
Then it again encounters another paragraph which is linked to before element styling so the word Section is inserted along with the SAME previous counter value.
<body>
<p class="ct">First make a variable increase every time a p element occurs.</p>
<p class="ct">Then insert the counter in front of all p elements</p>
<p class="ct"> counter in kh rews df</p>
</body>
IN css :
.ct {
counter-reset: myindex 2;
}
.ct::before {
content: "SECTION " counter(myindex) ": ";
counter-increment:myindex ;
}
OUTPUT:
Section 3 : First make a variable increase every time a p element occurs.
Section 3 : Then insert the counter in front of all p elements
Section 3 : counter in kh rews df
Explantion:
-
When the 1st paragraph is being encountered then the code flows to first .ct style where counter is created starting from 2
-
then it goes to before element where along with the wrd SECTION the value of counter is first incremented and then inserted
Now when the code meets the second paragraph then the code flows to .ct style where counter is reset to 2 again . -
Now the code goes to ::before element where the counter is first AGAIN incremented to 3 then printed along with word section .
Same case for other paragraphs too.
I was saying that although i have written counter-increment after content but the HTML parser is first incrementing in every loop then inserting before the content . Is this a rule??
.ct::before {
content: "SECTION " counter(myindex) ": ";
counter-increment:myindex ;
}
q2: " The counter’s name must not be “none”, “inherit”, or “initial”; otherwise the declaration is ignored. "
WHy this is so?