nth-Child repeat in a loop

偶尔善良 提交于 2020-01-01 06:54:34

问题


Folks,

i have 100 < p> tags and i'm trying to repeat a set of four color to every row in a bellow manner.

  • RED
  • BLUE
  • GREEN
  • PURPLE
  • RED
  • BLUE
  • GREEN
  • PURPLE ....and so on
  • The output generated is not as i desired. The output is

  • RED
  • BLUE
  • GREEN
  • PURPLE
  • RED
  • GREEN
  • RED
  • PURPLE
  • GREEN
  • BLUE
  • RED
  • PURPLE
  • so if you have any suggestion that would be helpful for me, :)

    here is my css code.

    <style>    
    p:nth-child(1n) {
            background: red;
        }
        p:nth-child(2n) {
            background: blue;
        }
        p:nth-child(3n) {
            background: green;
        }
        p:nth-child(4n) {
            background: purple;
        }
    </style>
    

    回答1:


    The following CSS will give you the solution you require.

    <style>    
    p:nth-child(4n+1) {
            background: red;
        }
        p:nth-child(4n+2) {
            background: blue;
        }
        p:nth-child(4n+3) {
            background: green;
        }
        p:nth-child(4n+4) {
            background: purple;
        }
    </style>
    

    Simple working example at fiddle http://jsfiddle.net/yugi47/Nwf2A/59/



    来源:https://stackoverflow.com/questions/39484034/nth-child-repeat-in-a-loop

    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!