简体   繁体   English

如何更改<li>项目的每个项目符号点的颜色?

[英]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: 一个例子:

  • List item with a red bullet 列出具有红色子弹的项目
  • List item with a blue bullet 列出具有蓝色子弹的项目
  • List item with a green bullet 列出带有绿色子弹的项目
  • etc. 等等

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;
}

DEMO DEMO

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.

相关问题 如何更改特定菜单项的文本颜色? 背景变化但文字颜色不变? - how can I change specific menu item text colour? background changing but text colour not changing? 如何更改每个的 src img 属性<li><img>物品?</li> - How do i change the src img attribute for each <li> <img> item? 如何缩小listview元素/项目?如何更改图片和文字的高度? - How can I make the listview li-element/item smaller?How change height with image and text? 在 js 中更改 li 项目符号颜色 - Change li bullet color in js 如何使用jquery为每个页面加载更改ul li列表的列出顺序 - how can i change listing order of my ul li list for each page load using jquery 如何使用jQuery更改HTML,以使每个文本 <li> 被一个包围<span>?</span> - How can I use jQuery to change my HTML so each text in an <li> is surrounded by a <span>? 动态更改元素li的颜色不会更改项目符号点的颜色 - Dynamically changing the color of an element li doesn't change the color of the bullet point 如何更改通过XMLHttpRequest添加的第一个LI值的颜色? - How do I change the colour of the first LI value that is added through a XMLHttpRequest? 如何按顺序将数组中的1个类应用于每个li? - How can I applying 1 class from an array to each li in order? 如何包装文本节点中的每个字符 <li> ? - How can I wrap each character from a text node in a <li>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM