简体   繁体   English

使用nth-child改变图案的背景颜色

[英]Make Background Color Change in Pattern Using nth-child

I am trying to create a grid where the background colors alternate using the nth-child selector. 我正在尝试使用nth-child选择器创建背景颜色交替的网格。

I have a grid with three columns and the pattern is 4 colors: blue, orange, green, purple. 我有一个由三列组成的网格,图案是4种颜色:蓝色,橙色,绿色,紫色。

So my pattern is supposed to go like this: 所以我的模式应该是这样的:

o | b | g
p | o | b
g | p | o

The thing is, I can't figure out what formula to use in order to acchieve this affect. 问题是,我不知道要使用哪种公式来实现这种效果。 Here's my code as it is now: 这是我现在的代码:

.mask { /*orange*/
    background-color: rgba(235,167,32, 0.7);
}

.view:nth-child(2n+2) .mask { /*blue*/
    background-color: rgba(4,141,195, 0.7);
}

.view:nth-child(3n+3) .mask { /*green*/
    background-color: rgba(0,211,130, 0.7);
}

.view:nth-child(4n+4) .mask { /*purple*/
    background-color: rgba(185,40,141, 0.7);
}

This is the wrong pattern that results: 这是导致的错误模式:

b | g | p
o | g | o
p | g | b

If anybody could offer some guidance here, I'd greatly appreciate it! 如果有人可以在这里提供一些指导,我将不胜感激!

JSFIDDLE DEMO JSFIDDLE演示

 .view:nth-child(4n+2) .mask 
   { /*orange*/
     background-color: orange;
   }

   .view:nth-child(4n+3) .mask 
   { /*blue*/
      background-color: blue;
   }

  .view:nth-child(4n+4) .mask 
  { /*green*/
    background-color: green;
  }

  .view:nth-child(4n+5) .mask 
  { /*purple*/
    background-color: purple;
  }

Explanation 说明

 I don't see your HTML. I assumed totally 9 divs. 
 First one is Orange and then blue, green, purple .
 It repeats for one more cycle. Ends with one orange.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM