Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count total number of divs with in a div using CSS

Tags:

css

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.

like image 539
BenEgan1991 Avatar asked Jun 02 '14 10:06

BenEgan1991


1 Answers

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

like image 58
Pete Avatar answered Sep 18 '22 18:09

Pete