【问题标题】:CSS: alternating list with odd even with non-repeating color in child list [duplicate]CSS:在子列表中具有不重复颜色的奇偶交替列表[重复]
【发布时间】:2016-04-08 20:13:22
【问题描述】:

HTML:

<ul>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<ul>
  <li>ho</li>
  <li>ho</li>
  <li>ho</li>
  <li>ho</li>
  <li>ho</li>
</ul>

CSS:

ul {
  margin-left: 15px;
}
li {
    background-color: blue;
    position: relative;
    height: 20px;
}

li:before {
  content: "";
  position: absolute;
  display: block;
  top: 0;
  left: -1000px;
  background: blue;
  width: 1000px;
  height: inherit;
}


li:nth-child(odd), li:nth-child(odd):before {
     background-color: green;
}
li:nth-child(odd)::before {
     background-color: green;
}
li:nth-child(even) {
    color:white;
}

这是一个fiddle,所以你可以看到它的样子。如您所见,我想要一个交替列表,但也希望 child-ul 永远不要重复相同的颜色。如您所见,有两条绿线紧随其后。我怎样才能防止这种情况发生?

有什么想法或技巧可以实现吗?

【问题讨论】:

标签: html css


【解决方案1】:

这应该可行:

我反转了嵌套在两个 ul 中的每个 li 的颜色设置。

所以两个 uls 中的每个 li 都会得到绿色背景和白色字体颜色。如果它是奇怪的,它将被染成蓝色。

ul {
  margin-left: 15px;
}
li {
    background-color: blue;
    position: relative;
    height: 20px;
}
ul ul li {
    background-color: green;
    color: white;
}


li:before {
  content: "";
  position: absolute;
  display: block;
  top: 0;
  left: -1000px;
  background: blue;
  width: 1000px;
  height: inherit;
}

ul ul li:before {
  background: green;
}

li:nth-child(odd), li:nth-child(odd):before {
     background-color: green;
}
li:nth-child(odd)::before {
     background-color: green;
}
li:nth-child(even) {
    color:white;
}

ul ul li:nth-child(odd), ul ul li:nth-child(odd):before {
     background-color: blue;
}
ul ul li:nth-child(odd)::before {
     background-color: blue;
}
ul ul li:nth-child(even) {
    color:black;
}
<ul>
    <li>ho</li>
    <li>ho</li>
    <li>ho</li>
    <li>ho</li>
    <li>ho
    <ul>
      <li>ho</li>
      <li>ho</li>
      <li>ho</li>
      <li>ho</li>
      <li>ho</li>
    </ul>
    </li>
</ul>

【讨论】:

  • 不能否认它似乎有效,但解释逻辑会很有帮助
  • 直到你添加另一个 li 然后你的逻辑就会中断
  • 在嵌套列表破坏它之前删除一个“ho”列表项。然而,这只是由于缺少 CSS 样式。这背后的想法是根据嵌套级别和嵌套列表父级的奇数/偶数类设置交替的开始/默认背景颜色。
  • 是的,如果“
  • ”的数量是动态变化的,那将如何工作?
  • 我很怀疑...它很脆弱。 JS是最优解
  • 【解决方案2】:

    @Robert 的回答不太奏效。您需要使用多个嵌套的:nth-child() 来容纳所有可能的组合:

    hr {
      margin: 60px 0;
    }
    
    ul {
      margin-left: 15px;
    }
    li {
        background-color: blue;
        position: relative;
        height: 20px;
    }
    
    li:before {
      content: "";
      position: absolute;
      display: block;
      top: 0;
      left: -1000px;
      background: blue;
      width: 1000px;
      height: inherit;
    }
    
    li:nth-child(even) {
        color:white;
    }
    
    
    li:nth-child(odd), li:nth-child(odd):before {
         background-color: green;
    }
    li:nth-child(odd)::before {
         background-color: green;
    }
    li:nth-child(even) {
        color:white;
    }
    
    /* parent is green so set odd blue */
    li:nth-child(odd) li:nth-child(odd){
         background-color: blue;
         color: #fff;
    }
    li:nth-child(odd) li:nth-child(odd)::before {
         background-color: blue;
    }
    
    /* parent is green so even to green */
    li:nth-child(odd) li:nth-child(even){
         background-color: green;
         color: #000;
    }
    li:nth-child(odd) li:nth-child(even)::before {
         background-color: green;
    }
    
    /* correct the text colour for nested elements */
    li:nth-child(even) li:nth-child(odd){
         color: #000;
    }
    li:nth-child(even) li:nth-child(even){
         color: #fff;
    }
    <ul>
        <li>ho</li>
        <li>ho</li>
        <li>ho</li>
        <li>ho</li>
        <li>ho
        <ul>
          <li>ho</li>
          <li>ho</li>
          <li>ho</li>
          <li>ho</li>
          <li>ho</li>
        </ul>
        </li>
    </ul>
    
    <hr>
    
    <ul>
        <li>ho</li>
        <li>ho</li>
        <li>ho</li>
        <li>ho</li>
        <li>ho</li>
        <li>ho
        <ul>
          <li>ho</li>
          <li>ho</li>
          <li>ho</li>
          <li>ho</li>
          <li>ho</li>
        </ul>
        </li>
    </ul>

    【讨论】:

    • 谢谢,完美运行。
    • 尝试在包含嵌套列表的项之后添加更多的第一级列表项。这将根据嵌套列表项的数量是偶数还是奇数来破坏它。
    • 这实际上与我的代码无关。这是因为 OP 代码中的绝对定位元素。逻辑仍然有效。 OP 有折叠元素的问题,这超出了这个问题的范围。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签