Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increment the counter in my css code? [duplicate]

In my html code I have the following list defined:

<p class="list">first.</p>
<p class="list">second.</p>

Now, in css I have:

.list {
  display: table;
}
.list:before {
  display: table-cell;
  counter-increment: counter;
  content: counter(counter) '.';
  padding-right: 5px;
}

and the result on the page is:

1. first
1. second

How can I increment the second number to the value of 2?

like image 290
user3766930 Avatar asked Jan 09 '17 12:01

user3766930


4 Answers

You should be using counter-reset property on the wrapper of the lists - see demo below:

body {
 counter-reset:counter;
}
.list {
  display: table;
}
.list:before {
  display: table-cell;
  counter-increment: counter;
  content: counter(counter) '.';
  padding-right: 5px;
}
<p class="list">first.</p>
<p class="list">second.</p>
like image 111
kukkuz Avatar answered Nov 15 '22 22:11

kukkuz


Instead of wrapping your HTML in <p> tags, you could use:

<ol>
  <li>first</li>
  <li>second</li>
</ol>

This will give you the output desired.

Here is a working fiddle.

This way you don't need to do any count stuff in CSS, much easier, simpler solution and compatible cross browser.

like image 41
Neelam Khan Avatar answered Nov 15 '22 20:11

Neelam Khan


You need to reset the counter:

body {
    counter-reset: section;
}
.list {
  display: table;
}
.list:before {
  display: table-cell;
  counter-increment: section;
  content:counter(section) ". ";
  padding-right: 5px;
}
<p class="list">first.</p>
<p class="list">second.</p>
like image 29
ab29007 Avatar answered Nov 15 '22 20:11

ab29007


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) " ";
}
<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>

Source: http://www.w3schools.com/css/css_counters.asp

like image 35
ssc-hrep3 Avatar answered Nov 15 '22 21:11

ssc-hrep3