[英]How can I change the colour of each bullet point of a <li> item?
Say, for example purposes I wanted a list of items that each had a different colour bullet point for each item. 比方说,例如,我想要一个项目列表,每个项目的每个项目都有不同的颜色项目符号点。
The kicker is I need to do this relatively semantically & automatically, avoiding a user having to add a class or a piece of HTML whilst keeping the colour of the text black. 踢球者是我需要相对语义和自动地做这件事,避免用户必须添加一个类或一段HTML,同时保持文本的颜色为黑色。
An example: 一个例子:
It poses an interesting problem - How would one go about do this so that a user can simply add a list of items and my code updates them accordingly. 它提出了一个有趣的问题 - 如何做到这一点,以便用户可以简单地添加项目列表,我的代码相应地更新它们。
Remove the default bullets, and add bullet characters (U+2022 BULLET or some other) as generated content. 删除默认项目符号,并添加项目符号(U + 2022 BULLET或其他)作为生成的内容。 You can style the generated content separately. 您可以单独设置生成的内容的样式。 This is flexible and requires no extra markup (beyond some markup that distinguishes between different items, obviously) but has the drawback of failing on old versions of IE (no support to generated content). 这是灵活的,并且不需要额外的标记(显然区别于不同项目的某些标记之外),但是具有在旧版本的IE上失败的缺点(不支持生成的内容)。
Assuming markup 假设加价
<ul class="mixed">
<li class="red">One.</li>
<li class="green">Two.</li>
</ul>
the style sheet could be 样式表可以
ul.mixed {
list-style-type: none;
}
ul.mixed li:before {
content: "\2022";
padding-right: 0.5em;
}
li.red:before {
color: red;
}
li.green:before {
color: green;
}
@sapan's solution above covers keeping the text a set colour. @ sapan上面的解决方案包括保持文本的颜色。 For automatically changing the bullet colour you will need nth-child: 要自动更改子弹颜色,您需要nth-child:
ul li:nth-child(3n) { color:green; }
ul li:nth-child(3n+1) { color:red; }
ul li:nth-child(3n+2) { color:blue; }
It's supported in ie9 and all good browsers, but for earlier versions of ie you'll need a javascript fallback/manually added classes in the html/ accept it's not going to happen . ie9和所有好的浏览器都支持它,但对于早期版本的ie,你需要一个javascript后备/手动添加的类在html / 接受它不会发生 。 Also, the above assumes you have a finite number of colours you want to cycle through in a fixed order. 此外,上面假设您希望以固定顺序循环显示有限数量的颜色。 If you want something more flexible then javascript is probably your best bet overall. 如果你想要更灵活的东西,那么javascript可能是你最好的选择。
Wheresrhys solution (below) is for me the best, I've just been using it to create a menu for wordpress/joomla where the css can automatically change the colour when a new li is added. Wheresrhys解决方案(下面)对我来说是最好的,我刚刚使用它为wordpress / joomla创建菜单,其中css可以在添加新li时自动更改颜色。 I would upvote but dont have the reputation points yet. 我会upvote但是还没有声望点。
ul li:nth-child(3n) { color:green; }
ul li:nth-child(3n+1) { color:red; }
ul li:nth-child(3n+2) { color:blue; }
It's supported in ie9 and all good browsers, but for earlier versions of ie you'll need a javascript fallback/manually added classes in the html/accept it's not going to happen. ie9和所有好的浏览器都支持它,但对于早期版本的ie,你需要一个javascript后备/手动添加的类在html /接受它不会发生。 Also, the above assumes you have a finite number of colours you want to cycle through in a fixed order. 此外,上面假设您希望以固定顺序循环显示有限数量的颜色。 If you want something more flexible then javascript is probably your best bet overall." 如果你想要更灵活的东西,那么javascript可能是你最好的选择。“
One insight for those who are not that experienced in css is that this works by making the nth child's css style a child of the main class in the same way you would if you wrote ul li:hover. 对那些没有css经验的人来说,一个见解是,这可以通过使第n个孩子的css风格成为主类的孩子,就像你编写ul li:hover一样。 So your code must reference the object in the same way thus ul li:nth-child. 所以你的代码必须以相同的方式引用对象,因此ul li:nth-child。 This means that if you wanted to apply this to an element when it is hovering your code will look like this ul li:hover:nth-child (odd/even or a number or equation.) I found this tutorial quite useful as it has a link to a demo at the bottom. 这意味着如果你想在它悬停时将它应用于一个元素,你的代码将看起来像这样:lover:nth-child(奇数/偶数或数字或等式。)我发现这个教程非常有用,因为它有底部演示的链接。 One thing to note is that this isnt compatible with IE 8 and below. 需要注意的一点是,这与IE 8及以下版本不兼容。
http://css-tricks.com/how-nth-child-works/ http://css-tricks.com/how-nth-child-works/
Biggest problem here will be cross-browser compatibility. 这里最大的问题是跨浏览器兼容性。 Oddly, I think your best bet would be to use css sprites as the LI's background image. 奇怪的是,我认为你最好的选择是使用css sprites作为LI的背景图像。
You can wrap the text in the li
with <span>
or <p>
and then use the following css: 您可以使用<span>
或<p>
将文本包装在li
,然后使用以下css:
ul li.green { color:green; }
ul li.red { color:red; }
ul li span { color:navy; }
Example html: 示例html:
<ul>
<li class="red"><span>One.</span></li>
<li class="green"><span>Two.</span></li>
</ul>
Using the :before
pseudo-class, you can customize your lists quite easily: 使用:before
伪类,您可以非常轻松地自定义列表:
li:before {
content: "\00BB \0020";
color: Red;
}
This article from A-List-Apart pretty well covers the subject 来自A-List-Apart的这篇文章很好地涵盖了这个主题
use separate classes for each effect and apply them to the li's 为每个效果使用单独的类并将它们应用于li
li{padding-left:10px;}
li.red{background-image: url(images/red.gif) no-repeat left 50%;}
li.blue{background-image: url(images/blue.gif) no-repeat left 50%;}
li.green{background-image: url(images/green.gif) no-repeat left 50%;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.