【问题标题】:CSS nth-child circular stylingCSS nth-child 圆形样式
【发布时间】:2016-11-13 17:38:39
【问题描述】:

我有 6 种颜色的序列:

  • 红色

  • 绿色

  • 蓝色

  • 青色

  • 洋红色

  • 黄色

而元素 1 是红色的。

元素 2 是绿色的……等等。

列表可以有无限数量的项目,并且应该保持颜色顺序。

最简单的方法是使用 nth-child(n%6),但我们知道 nth-child 没有模块运算符。

顺序:

  • nth-child(n): 红色
  • nth-child(2n):绿色
  • nth-child(3n):蓝色
  • nth-child(4n): 青色

行不通,因为第 8 个元素是青色,但它应该是绿色。

偏移量也不起作用,因为它只适用于第一次出现。

这个问题能解决吗?

【问题讨论】:

  • 根据您的序列,为什么第一个元素是黄色而不是红色?
  • Is 而元素 1 是黄色的。 一个错字,应该是 而元素 1 是红色的。?
  • “黄色”是我在提出问题时犯的一个错误(我的实际应用程序完全不同)。道歉。

标签: css css-selectors sequence


【解决方案1】:

你可以这样做

  • nth-child(6n-5) 每 6:th,从 6-5 = 1 开始
  • nth-child(6n-4) 每 6:th,从 6-4 = 2 开始
  • nth-child(6n-0) 每 6:th,从 6-0 = 6 开始(可以写成nth-child(6n)

或像这样(已更新,在这种情况下可能更合适)

  • nth-child(6n+1) 每 6:th,从 1 开始
  • nth-child(6n+2) 每 6:th,从 2 开始
  • nth-child(6n+6) 每 6:th,从 6 开始(可以写成nth-child(6n)

/* left div's */
.left div:nth-child(6n-5) {
  background: red;
}
.left div:nth-child(6n-4) {
  background: green;
}
.left div:nth-child(6n-3) {
  background: blue;
}
.left div:nth-child(6n-2) {
  background: cyan;
}
.left div:nth-child(6n-1) {
  background: magenta;
}
.left div:nth-child(6n) {
  background: yellow;  
}
/* right div's */
.right div:nth-child(6n+1) {
  background: red;
}
.right div:nth-child(6n+2) {
  background: green;
}
.right div:nth-child(6n+3) {
  background: blue;
}
.right div:nth-child(6n+4) {
  background: cyan;
}
.right div:nth-child(6n+5) {
  background: magenta;
}
.right div:nth-child(6n) {
  background: yellow;  
}

/* for this demo only */
div div:nth-child(6n) + div {
  margin-top: 15px;  
}
.left, .right {
  display: inline-block;
  width: 33%;
}
<div class="left">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
  <div>9</div>
  <div>10</div>
  <div>11</div>
  <div>12</div>
  <div>13</div>
  <div>14</div>
  <div>15</div>  
</div>
<div class="right">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
  <div>9</div>
  <div>10</div>
  <div>11</div>
  <div>12</div>
  <div>13</div>
  <div>14</div>
  <div>15</div>  
</div>

【讨论】:

  • 但是第一个元素不是黄色的(这似乎是一个OP要求......我不太明白)
  • @vals 我认为这是一个错字,如果不是,我会更新我的答案......我想我们都在等待 j08691 的回复的评论
  • 我明白你做了什么,基本上: - (6n) = [0, 6, 12, 18, ...] % 6 = 0. - (6n - 5) = [1, 7, 13 , 19, ...] % 6 = 1 - ... - (6n - 1) = [-1, 5, 11, 17, ...] % 6 = 5。您首先决定它应该只选择 N 的倍数所需的模块,然后减去每个可能的 [模块] 结果。
  • nth-child 中有没有类似3n-n 的方法?
  • @l3lue 可能。第二个n 是什么意思?
【解决方案2】:

但我们知道没有用于 nth-child 的模块运算符。

是什么让您认为:nth-child 没有模数语法?

如果你想要:nth-child(<i>x</i>),其中<i>x</i> ∈ ℤ<i><sub>a</sub></i><i>a</i> ∈ ℕ,那么语法是

:nth-child(<i>a</i>n + <i>b</i>)

其中<i>b</i> ∈ ℤ<i>x</i> 的任何代表,因此<i>b</i> &lt; <i>a</i>

正如您在 LGSon 的回答中看到的,通常在其中一组中选择 <i>b</i>

  • {0, 1, …, <i>a</i>-1}
  • {-<i>a</i>, -<i>a</i>+1, …, -1}
  • {-<i>a</i>+1, …, -1, 0}

注意:在这个答案中,ℤ<i><sub>a</sub></i> 表示ℤ⧸<i>a</i>ℤ,即integers modulo <i>a</i>

【讨论】:

  • 很好的答案......但可能是 OP 不会理解数学符号
猜你喜欢
  • 2021-11-20
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2018-07-30
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
相关资源
最近更新 更多