Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Auto increment numbers if elements are not siblings

Is it possible – with css only – to auto increment numbers if elements are not siblings?

Something like this?

.numbers {
    counter-increment: item;

    &::before {
        content: counter(item) ":";
    }
}
<div class="container">
  <div class="row">
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
  </div>
</div>

And if not, what is the best and most efficient way to achieve it? JavaScript?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instread of incrementing your counter with .numbers, you can do it with the parent.

.col {
  counter-increment: item;
}

.numbers:before {
  content: counter(item) ":";
}
<div class="container">
  <div class="row">
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
    <div class="col">
      <div class="numbers"></div>
      Column
    </div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading