【问题标题】:How to color every four elements alternating in css? [duplicate]如何在css中交替为每四个元素着色? [复制]
【发布时间】:2019-05-15 18:37:34
【问题描述】:

我需要通过 CSS 为每四个可选元素制作背景颜色。我怎样才能做到这一点?

我找到了替代两个元素的解决方案,但我不明白如何在四个元素中制作它。 每两个元素都在这里找到:-

How to select every two elements alternating?

【问题讨论】:

标签: css css-selectors


【解决方案1】:
li:nth-of-type(4n) {
    color: red;
}

更新:选择四人组

li:nth-of-type(8n+1),
li:nth-of-type(8n+2),
li:nth-of-type(8n+3),
li:nth-of-type(8n+4)
{
    color: red;
}

li:nth-of-type(8n+5),
li:nth-of-type(8n+6),
li:nth-of-type(8n+7),
li:nth-of-type(8n+8)
{
    color: blue;
}

看到这支笔[更新] https://codepen.io/prny/pen/GarEMK

更多关于这里的 W3S https://www.w3schools.com/cssref/sel_nth-of-type.asp

【讨论】:

  • 它只选择每四个元素,我需要选择每四个元素,就像在链接中我分享了它选择每两个元素。
  • 请在 1 到 20 的列表中说明您想要选择的所有内容
  • 1 到 23 1,2,3,4 ->红色 5,6,7,8 ->蓝色 9,10,11,12 ->红色 13,14,15,16 ->蓝色 16,17,18,19,-> 红色 20,21,22,23,->蓝色
  • stackoverflow.com/a/55913237/1273882 这是您正在寻找的解决方案。
  • @AnkushJain 是的,就是这样,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
相关资源
最近更新 更多