如何在无序列表中更改点的颜色?

时间:2022-11-21 08:40:05

I want to change the color of dots in an unordered list:

我想在无序列表中更改点的颜色:

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

Is there a way that I can do this with CSS? I can't see a property?

有没有办法用CSS做到这一点?我看不到房产?

5 个解决方案

#1


41  

The easiest (but rather unsemantic) way is to wrap the content in span tags, then apply the bullet color to li and text color to span.

最简单(但相当无意义)的方法是将内容包装在span标签中,然后将子弹颜色应用于li,将文本颜色应用于span。

In code:

在代码中:

<ul>
    <li><span>text</span></li>
    <li><span>text</span></li>
    <li><span>text</span></li>
</ul>
ul li {
    /* Bullet color */
    color: red;
    list-style-type: disc;
}

ul li span {
    /* Text color */
    color: black;
}

jsFiddle preview

jsFiddle预览

If you can't modify your HTML, you can either use list-style-image with a custom-colored dot, or use generated content (i.e. li:before) and color it accordingly (but watch out for list bullet position problems).

如果您无法修改HTML,您可以使用带有自定义颜色点的list-style-image,或使用生成的内容(即li:before)并相应地对其进行着色(但要注意列出项目符号位置问题)。

Here's an example with li:before:

这是li的一个例子:之前:

ul li {
    /* Text color */
    color: black;
    list-style-type: none;
}

ul li:before {
    /* Unicode bullet symbol */
    content: '\2022 ';
    /* Bullet color */
    color: red;
    padding-right: 0.5em;
}

#2


10  

Further developing the answer given by @BoltClock:

进一步发展@BoltClock给出的答案:

ul li {
    color: black;
    list-style-type: none;
}

ul li:before {
    color: red;
    float: left;
    margin: 0 0 0 -1em;
    width: 1em;
    content: '\2022';
}

In this way all lines of a multi-line bullet are indented properly. Beware: I’ve not had the chance to test it on IE yet!

这样,多线子弹的所有线都正确缩进。注意:我还没有机会在IE上测试它!

#3


2  

You can create your own image.

您可以创建自己的图像。

li {
    list-style-image: url(myImage.gif);
}

#4


2  

None of the above answers work for me, as I had content that wrapped onto multiple lines. However the solution provided by W3C is perfect: https://www.w3.org/Style/Examples/007/color-bullets.en.html

上述答案都不适合我,因为我的内容包含多行。然而,W3C提供的解决方案是完美的:https://www.w3.org/Style/Examples/007/color-bullets.en.html

In short, remove styling:

简而言之,删除样式:

ul {list-style: none}

Then add your own bullet

然后添加自己的子弹

li::before {
content: "•";
color: red;
display: inline-block;
width: 1em;
margin-left: -1em
}

The key points are inline-block, width and margin to position it correctly.

关键点是内联块,宽度和边距,以正确定位它。

#5


0  

I think you have to use a graphic:

我想你必须使用图形:

http://catcode.com/csstips/graphic_list.html

http://catcode.com/csstips/graphic_list.html

As an aside, this will also give you granular control over the bullet positioning (by using margins and image offset).

另外,这也可以让您精确控制子弹定位(通过使用边距和图像偏移)。

#1


41  

The easiest (but rather unsemantic) way is to wrap the content in span tags, then apply the bullet color to li and text color to span.

最简单(但相当无意义)的方法是将内容包装在span标签中,然后将子弹颜色应用于li,将文本颜色应用于span。

In code:

在代码中:

<ul>
    <li><span>text</span></li>
    <li><span>text</span></li>
    <li><span>text</span></li>
</ul>
ul li {
    /* Bullet color */
    color: red;
    list-style-type: disc;
}

ul li span {
    /* Text color */
    color: black;
}

jsFiddle preview

jsFiddle预览

If you can't modify your HTML, you can either use list-style-image with a custom-colored dot, or use generated content (i.e. li:before) and color it accordingly (but watch out for list bullet position problems).

如果您无法修改HTML,您可以使用带有自定义颜色点的list-style-image,或使用生成的内容(即li:before)并相应地对其进行着色(但要注意列出项目符号位置问题)。

Here's an example with li:before:

这是li的一个例子:之前:

ul li {
    /* Text color */
    color: black;
    list-style-type: none;
}

ul li:before {
    /* Unicode bullet symbol */
    content: '\2022 ';
    /* Bullet color */
    color: red;
    padding-right: 0.5em;
}

#2


10  

Further developing the answer given by @BoltClock:

进一步发展@BoltClock给出的答案:

ul li {
    color: black;
    list-style-type: none;
}

ul li:before {
    color: red;
    float: left;
    margin: 0 0 0 -1em;
    width: 1em;
    content: '\2022';
}

In this way all lines of a multi-line bullet are indented properly. Beware: I’ve not had the chance to test it on IE yet!

这样,多线子弹的所有线都正确缩进。注意:我还没有机会在IE上测试它!

#3


2  

You can create your own image.

您可以创建自己的图像。

li {
    list-style-image: url(myImage.gif);
}

#4


2  

None of the above answers work for me, as I had content that wrapped onto multiple lines. However the solution provided by W3C is perfect: https://www.w3.org/Style/Examples/007/color-bullets.en.html

上述答案都不适合我,因为我的内容包含多行。然而,W3C提供的解决方案是完美的:https://www.w3.org/Style/Examples/007/color-bullets.en.html

In short, remove styling:

简而言之,删除样式:

ul {list-style: none}

Then add your own bullet

然后添加自己的子弹

li::before {
content: "•";
color: red;
display: inline-block;
width: 1em;
margin-left: -1em
}

The key points are inline-block, width and margin to position it correctly.

关键点是内联块,宽度和边距,以正确定位它。

#5


0  

I think you have to use a graphic:

我想你必须使用图形:

http://catcode.com/csstips/graphic_list.html

http://catcode.com/csstips/graphic_list.html

As an aside, this will also give you granular control over the bullet positioning (by using margins and image offset).

另外,这也可以让您精确控制子弹定位(通过使用边距和图像偏移)。