将CSS在n-Child中应用于nth-Child



是否可以在nth-child中添加样式?

我正在忙着一份学生(15名学生(的名单,其中前5名学生的名字将变成绿色,然后6-10为橙色,11-15为红色。

就像将CSS应用于项目范围一样。

我找不到一些相关问题。

希望你了解我。

谢谢。

ol li:first-child{
  color: green;
}
ol li:nth-child(2), ol li:nth-child(3), ol li:nth-child(4), ol li:nth-child(5){
  color: green;
}
ol li:nth-child(6), ol li:nth-child(7), ol li:nth-child(8), ol li:nth-child(9), ol li:nth-child(10){
  color: orange;
}
ol li:nth-child(11), ol li:nth-child(12), ol li:nth-child(13), ol li:nth-child(14), ol li:nth-child(15){
  color: red;
}
<ol>
  <li>student1</li>
  <li>student2</li>
  <li>student3</li>
  <li>student4</li>
  <li>student5</li>
  <li>student6</li>
  <li>student7</li>
  <li>student8</li>
  <li>student9</li>
  <li>student10</li>
  <li>student11</li>
  <li>student12</li>
  <li>student13</li>
  <li>student14</li>
  <li>student15</li>
</ol>

这就是您想要的?

在第一个CSS -n+5中,由于negetive而在5th元素之前选择前五个元素。如果删除Negetive,它将选择所有具有第5个元素的元素。因此,再次为元素11到15添加了n+11

如果您不想在第15个元素之后添加CSS,则可以使用.second CSS。

.first p:nth-child(-n+5) {
  color: green;
}
.first p:nth-child(n + 5) {
  color: orange;
}
.first p:nth-child(n + 11) {
  color: red;
}
.second {
  margin-top: 50px;
}
.second p:nth-child(-n+5) {
  color: green;
}
.second p:nth-child(n + 6):nth-child(-n + 10) {
  color: orange;
}
.second p:nth-child(n + 11):nth-child(-n + 15) {
  color: red;
}
<div class="first">
  <p>Student 1</p>
  <p>Student 2</p>
  <p>Student 3</p>
  <p>Student 4</p>
  <p>Student 5</p>
  <p>Student 6</p>
  <p>Student 7</p>
  <p>Student 8</p>
  <p>Student 9</p>
  <p>Student 10</p>
  <p>Student 11</p>
  <p>Student 12</p>
  <p>Student 13</p>
  <p>Student 14</p>
  <p>Student 15</p>
</div>
<div class="second">
  <p>Student 1</p>
  <p>Student 2</p>
  <p>Student 3</p>
  <p>Student 4</p>
  <p>Student 5</p>
  <p>Student 6</p>
  <p>Student 7</p>
  <p>Student 8</p>
  <p>Student 9</p>
  <p>Student 10</p>
  <p>Student 11</p>
  <p>Student 12</p>
  <p>Student 13</p>
  <p>Student 14</p>
  <p>Student 15</p>
  <p>Student 16</p>
  <p>Student 17</p>
</div>

您可以使用CSS儿童范围来实现您的解决方案。

您的风格应该如下

li:nth-child(n+4):nth-child(-n+8) {
  background: #298EB2;
}
li {
  width: 20px;
  height: 20px;
  background: red;
  margin: 5px;
  display: inline-block;
}
<div>
  <h3>Child Ranges</h3>
  <ul>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
    <li><span></span></li>
  </ul>
</div>

工作演示

如果您想了解有关nth-Child的更多信息,则可以访问此网站http://nthmaster.com/

有一个代码的掠夺。根据您的要求。

ul li{
 margin-bottom: 2px;
}
ul li:nth-child(n+1):nth-child(-n+5) {
  background: #2e8209;
}
ul li:nth-child(n+6):nth-child(-n+10) {
  background: #eb7436;
}
ul li:nth-child(n+11):nth-child(-n+15) {
  background: #b10a0a;
}
<ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li>Child 3</li>
            <li>Child 4</li>
            <li>Child 5</li>
            <li>Child 6</li>
            <li>Child 7</li>
            <li>Child 8</li>
            <li>Child 9</li>
            <li>Child 10</li>
            <li>Child 11</li>
            <li>Child 12</li>
            <li>Child 13</li>
            <li>Child 14</li>
            <li>Child 15</li>
          </ul>
        

<!DOCTYPE html>
<html>
<head>
<style> 
p:not(:nth-child(n+7)) {
    color: green;
}
</style>
</head>
<body>
<p>One</p>
<p>Two</p>
<p>Three</p>
<p>Four</p>
<p>Five</p>
<p>Six</p>
<p>Seven</p>
<p>Eight</p>
</body>
</html>

下面的代码只会给出前五行绿色,假设P标签是您的类,

p:not(:nth-child(n+7)) {
color: green;

}

最新更新