Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css increment nth-child attribute values

Is there a way to increment attribute values using n in nth-child(n) to output the result of:

div:nth-child(1){
    top: -5px;
}
div:nth-child(2){
    top: -10px;
}
div:nth-child(3){
    top: -15px;
}
div:nth-child(4){
    top: -20px;
}
div:nth-child(5){
    top: -25px;
}
like image 685
Eleonora Velikova Avatar asked Nov 17 '14 13:11

Eleonora Velikova


People also ask

How do I apply for nth child CSS?

Syntax. :nth-child() takes a single argument that describes a pattern for matching element indices in a list of siblings. Element indices are 1-based. :nth-child( <nth> [ of <complex-selector-list> ]? )

How do you target the nth child in SCSS?

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.

What's the difference between the nth of type () and Nth child () selector?

As a general rule, if you want to select an interval of a selector regardless of the type of element it is, use nth-child . However, if you want to select a specific type only and apply an interval selection from there, use nth-of-type .


1 Answers

You could if you use a preprocessor like sass.

An example:

div {
  $var: 5;

  @for $i from 1 through 5 {
    &:nth-child(#{$i}) {
      top: -#{$var}px;
      $var: $var + 5;
    }
  }
}

A demo: http://sassmeister.com/gist/7d7a8ed86e857be02d1a

Another way to achieve this:

div {

  $list: 1 2 3 4 5;

  @each $i in $list {
    &:nth-child(#{$i}) {
      top: -5px * $i;
    }
  }
}

A demo: https://www.sassmeister.com/gist/fb5ee7aa774aafb896cd71a828882b99

like image 51
Vangel Tzo Avatar answered Nov 07 '22 05:11

Vangel Tzo