简体   繁体   English

如何使用css为li内的每个span元素添加颜色

[英]How to add color for every span element inside li by using css

Hi there i was trying to add different color for span element by using pseudo. 嗨,我尝试使用伪为span元素添加不同的颜色。 can you help me with that 你能帮我吗

<ol class="hidden-xs">
  <li class="active dt-nav"><span class="dot-nav">1</span></li>
  <li class="dt-nav"><span class="dot-nav">2</span></li>
  <li class="dt-nav"><span class="dot-nav">3</span></li>
  <li class="dt-nav"><span class="dot-nav">4</span></li>
</ol>
.dt-nav:nth-child(1) {
  color:red;
}
.dt-nav:nth-child(2) {
  color:blue;
}
.dt-nav:nth-child(3) {
  color:green;
}
.dt-nav:nth-child(4) {
  color:orange;
}

use :nth-child() 使用:nth-​​child()

li span:nth-child(1) {
  color: green;
}
li span:nth-child(2) {
  color: red;
}
li span:nth-child(3) {
  color: blue;
}

and so on 等等

You have to select the correct li cholds to your css elements. 您必须为css元素选择正确的电容。 For example: 例如:

ol.hidden-xs > li:first-child > span.dot-nav {
    color: #000000;
}

ol.hidden-xs > li:nth-child(2) > span.dot-nav {
    color: #333333;
}

ol.hidden-xs > li:nth-child(3) > span.dot-nav {
    color: #db0036;
}

ol.hidden-xs > li:last-child > span.dot-nav {
    color: #cccccc;
}

You have to fix your css and use proper selectors : 您必须修复CSS并使用适当的选择器

 ol > li.dt-nav:nth-child(1) > .dot-nav { background: red; } ol > li.dt-nav:nth-child(2) > .dot-nav { background: black; } ol > li.dt-nav:nth-child(3) > .dot-nav { background: red; } ol > li.dt-nav:nth-child(4) > .dot-nav { background: black; } 
 <ol class="hidden-xs"> <li class="active dt-nav"><span class="dot-nav">1</span></li> <li class="dt-nav"><span class="dot-nav">2</span></li> <li class="dt-nav"><span class="dot-nav">3</span></li> <li class="dt-nav"><span class="dot-nav">4</span></li> </ol> 

Also your html is not well-formed ( </i> ). 另外,您的html格式也不正确( </i> )。

You could try "nth-of-type" for each li item, like this: 您可以为每个li项目尝试“ nth-of-type”,如下所示:

li:nth-of-type(1) span { color: red; }
li:nth-of-type(2) span { color: white; }
li:nth-of-type(3) span { color: blue; }
li:nth-of-type(4) span { color: rebeccapurple; }

You can do the following with Sass: 您可以使用Sass执行以下操作:

$colors: orange, blue, red, green;

li {
  @for $i from 1 to 5 {

    &:nth-child(#{$i}) {
      span {
        $color: nth($colors, $i);
        color: $color;
      }
    }
  } 
}

https://jsfiddle.net/o6w613y3/1/ https://jsfiddle.net/o6w613y3/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM