Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of children div in css or sass

Assuming we have a css list as such

<ul class="parent">
  <li class="child"></li>
</ul>

with the child items being generated based from an iterator. How can you get the number of children within parent in either css or scss.

So I can dynamically modify css attributes like padding based on the nth child.

like image 756
philip_nunoo Avatar asked Feb 15 '18 12:02

philip_nunoo


People also ask

How do I find the number of children in CSS?

The :nth-child() in CSS Selector is used to select only those elements that are the nth child, regardless of type, of its parent.

How do you find the nth child in sass?

You can pass in a positive number as an argument to :nth-child() , which will select the one element whose index inside its parent matches the argument of :nth-child() . For example, li:nth-child(3) will select the list item with an index value 3; that is, it will select the third list item.

How many children are in CS?

However, from the current medical evidence, most medical authorities do state that if multiple C-sections are planned, the expert recommendation is to adhere to the maximum number of three.”


1 Answers

Neither CSS nor SASS will tell you how many items there are in a list. You'll need JS for that.

However, with SASS you can generate the CSS for as many children as you want automatically:

@for $i from 1 through 8 {

    li:nth-child(#{$i}) {
        padding-left: $i * 20px
    }
}

Change the number 8 to any number you think will have you covered (10? 100? 1000?).

More info: http://clubmate.fi/for-while-and-each-loops-in-sass/

like image 53
Victoria Ruiz Avatar answered Sep 22 '22 13:09

Victoria Ruiz