9

Is there a way to count the total number of divs within a div using CSS?

So far I have developed a solution using JavaScript.

$('#nextPrev > div').length

But I just wondered whether this is possible to do via CSS.

4
  • You can't get count with CSS only :) Commented Jun 2, 2014 at 10:03
  • 1
    There is no way to count or perform tasks like this in CSS. You can select elements and apply style rules to them. You will have to use Javascript for this. Commented Jun 2, 2014 at 10:04
  • Not possible. But why would you wan't to? Commented Jun 2, 2014 at 10:04
  • use var nb = $('div.name').length; Commented Jun 2, 2014 at 10:04

1 Answer 1

13

You can use a css counter to get the count. Consider the following html:

<div id="test">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>
<div id="result"></div>

You can display the count using the following css:

#test {counter-reset:test;}
#test > div {counter-increment:test;}
#result:before {content:counter(test);}

Example

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

1 Comment

I completely forgot about the counter.

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.