24

3 つの色のリストを持つことが可能です。

$color-list: xyz;

次に、これらの 3 つの色を循環させて適用し、順序付けされていないリスト項目に追加します。

私が欲しい:

<li>row 1</li> (gets color x)
<li>row 2</li> (gets color y)
<li>row 3</li> (gets color z)
<li>row 4</li> (gets color x)

などなど。

@each ( http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#each-directive ) 関数を使用しようとしましたが、最初にリストを通過した後に色の適用を停止します。色を適用するリスト項目がなくなるまで、色を循環させ続けたいと思います。

これはsassで可能ですか?

4

1 に答える 1

48

純粋な CSS で可能であれば、Sass で可能です。これは、任意の数の色で機能します。

http://codepen.io/cimmanon/pen/yoCDG

$colors: red, orange, yellow, green, blue, purple;

@for $i from 1 through length($colors) {
    li:nth-child(#{length($colors)}n+#{$i}) {
        background: nth($colors, $i)
    }
}

出力:

li:nth-child(6n+1) {
  background: red;
}

li:nth-child(6n+2) {
  background: orange;
}

li:nth-child(6n+3) {
  background: yellow;
}

li:nth-child(6n+4) {
  background: green;
}

li:nth-child(6n+5) {
  background: blue;
}

li:nth-child(6n+6) {
  background: purple;
}
于 2013-03-18T11:40:21.907 に答える