0

I have a bunch of divs like this

<div id="parentDiv">
    <div class="childDiv">some content</div>
    <div class="childDiv">some content</div>
    <div class="childDiv">some content</div>
</div>

is there some way using only CSS that I could show the current index to the left of the childDiv elements and it automatically update if I were to shuffle them around using jQuery or would I have to to manipulate the child div using jquery ?

Or One of the other ways I was thinking to handle it would be to change them to ol li but then I need them to be zero based and I haven't see any thing in css to do that either

3
  • heh don't know how that happend thanks for the edit @George Commented Mar 22, 2015 at 20:08
  • though I'm curious why the down vote Commented Mar 22, 2015 at 20:09
  • you have to use js/jquery to edit html to show index value Commented Mar 22, 2015 at 20:10

1 Answer 1

3

You can use CSS counters:

#parentDiv {
  counter-reset: index; /* Create a `index` counter scope */
}
.childDiv:before {
  content: counter(index) ". "; /* Display `index` counter */
  counter-increment: index; /* Add 1 to `index` */
}
<div id="parentDiv">
  <div class="childDiv">some content</div>
  <div class="childDiv">some content</div>
  <div class="childDiv">some content</div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

any suggestion on how to make it 0 based index ?
@mcgrailm Use counter-reset: index -1 in #parentDiv (demo). Or use counter-increment: index in .childDiv:after instead of in .childDiv:before (demo).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.