如何在不使用任何图像或span标签的情况下通过CSS在UL / LI html列表中设置Bullet颜色

时间:2011-03-15 01:50:29

标签: css css3 layout colors html-lists

想象一下包含一些<li>项的简单未排序列表。现在,我已经通过list-style:square;将子弹定义为方形但是,如果我使用<li>设置color: #F00;项的颜色,那么所有都会变为红色!

虽然我只想设置方形子弹的颜色。是否有优雅的方式来定义CSS中的项目符号的颜色...

...不使用任何精灵图像也不使用span标签!

HTML

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<ul>

CSS

li{
   list-style:square;
}

16 个答案:

答案 0 :(得分:998)

最常见的方法是这样做:

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

li {
  padding-left: 1em; 
  text-indent: -.7em;
}

li::before {
  content: "• ";
  color: red; /* or whatever color you prefer */
}
<ul>
  <li>Foo</li>
  <li>Bar</li>
  <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</li>
</ul>

JSFiddle:http://jsfiddle.net/leaverou/ytH5P/

适用于所有浏览器,包括版本8及更高版本的IE。

答案 1 :(得分:87)

浏览前一段时间,发现这个网站,你试过这个替代方案吗?

li{
    list-style-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAE0lEQVQIW2NkYGD4D8RwwEi6AACaVAQBULo4sgAAAABJRU5ErkJggg==");
}

听起来很难,但你可以制作自己的png图像/模式 here ,然后复制/粘贴你的代码并自定义你的子弹=)仍然优雅?

编辑:

根据@ lea-verou关于另一个答案的想法并应用这种外部来源增强的理念,我已经找到了另一个解决方案:

  1. 将Webfont图标库的样式表嵌入您的脑海中,在本例中为Font Awesome:
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    1. 从FontAwesome cheatsheet(或任何其他webfont图标)获取代码。
    2. i.e.:
      fa-angle-right [&#xf105;]
      

      并使用f的最后一部分......后跟一个这样的数字,同时使用font-family

      li:before {
          content: "\f105";
          font-family: FontAwesome;
          color: red; /* or whatever color you prefer */
          margin-right: 4px;
      }
      

      那就是它!现在你也有自定义项目符号提示=)

      fiddle

答案 2 :(得分:45)

CSS 3 Lists模块的当前规范确实指定了::marker pseudo-element,这将完全符合您的要求; FF已经过测试 不支持::marker,我怀疑Safari或Opera是否拥有它。 IE当然不支持它。

所以现在,唯一的方法是使用list-style-image的图像。

我猜你可以用li包裹span的内容,然后你可以设置每个的颜色,但这对我来说似乎有些不自在。

答案 3 :(得分:45)

我只是解决了这个问题,这应该适用于所有浏览器:

<强> HTML:

<ul>
  <li><span>Foo</span></li>
  <li><span>Bar</span></li>
  <li><span>Bat</span></li>
</ul>

<强> CSS:

ul li{
    color: red
}

ul li span{
    color: blue;
}

答案 4 :(得分:41)

一种方法是将li:beforecontent: ""一起使用,并将其设置为inline-block元素。

以下是一个有效的代码段:

ul {
  list-style-type: none; /* no default bullets */
}

li { 
  font-family: Arial;
  font-size: 18px;
}

li:before { /* the custom styled bullets */
  background-color: #14CCBB;
  border-radius: 50%;
  content: "";
  display: inline-block;
  margin-right: 10px;
  margin-bottom: 2px;
  height: 10px;
  width: 10px;
}
<ul>
  <li>Swollen joints</li>
  <li>Pain in hands and knees</li>
  <li>Redness around joints</li>
  <li>Constant fatigue</li>
  <li>Morning stiffness in joints</li>
  <li>High fevers</li>
  <li>Rheumatoid nodules, which develop around joints</li>
</ul>

答案 5 :(得分:24)

然而,另一种解决方案是使用:before伪元素和border-radius: 50%。这适用于所有浏览器,包括IE 8及更高版本。

使用em单元可以响应字体大小的变化。您可以通过调整jsFiddle窗口来测试它。

ul {
    list-style: none;
    line-height: 1em;
    font-size: 3vw;
}

ul li:before {
    content: "";
    line-height: 1em;
    width: .5em;
    height: .5em;
    background-color: red;
    float: left;
    margin: .25em .25em 0;
    border-radius: 50%;
}

jsFiddle

您甚至可以使用box-shadow来创建一些漂亮的阴影,使用content: "• "解决方案看起来效果不佳。

答案 6 :(得分:18)

我试过这个,事情让我很奇怪。 (css在本教程的:after {content: "";}部分之后停止工作。我发现只需在color:#ddd;项目本身使用li就可以为项目符号着色。

这是 example

li{
    color:#ff0000;    
    list-style:square;                
}
a {
    text-decoration: none;
    color:#00ff00;
}

a:hover {
    background-color: #ddd;
}

答案 7 :(得分:17)

我使用jQuery:

jQuery('li').wrapInner('<span class="li_content" />');

&安培;用一些CSS:

li { color: red; }
li span.li_content { color: black; }

可能有点矫枉过正,但如果您正在为CMS编码而且您不想让编辑在每个列表项中添加额外的跨度,那就太方便了。

答案 8 :(得分:10)

我建议给LI background-imagepadding-leftlist-style-image属性在跨浏览器环境中很有用,添加额外元素(例如span)是不必要的。所以你的代码最终会看起来像这样:

li {
  background:url(../images/bullet.png) 0 0 no-repeat;
  list-style:none;
  padding-left:10px;
}

答案 9 :(得分:9)

您可以做的最简单的事情是将<li>的内容包装在<span>或等效内容中,然后您可以单独设置颜色。

或者,您可以使用所需的子弹颜色制作图像,并使用list-style-image属性进行设置。

答案 10 :(得分:8)

多行条目中具有完美缩进的Lea Verou解决方案的变体可能是这样的:

ul{
    list-style: none;
    position: relative;
    padding: 0;
    margin: 0;
}

li{
    padding-left: 1.5em; 
}

li:before {
    position: absolute;
    content: "•";
    color: red;
    left: 0;
}

答案 11 :(得分:6)

我正在为这个问题添加解决方案。

我不想使用图像和验证器会惩罚你在CSS中使用负值,所以我这样走:

ul          { list-style:none; padding:0; margin:0; }

li          { padding-left:0.75em; position:relative; }

li:before       { content:"•"; color:#e60000; position:absolute; left:0em; }

答案 12 :(得分:5)

我知道这篇文章有点迟到,但需要参考......

CSS

ul {
    color: red;
}

li {
    color: black;
}

子弹颜色在ul标签上定义,然后我们将li颜色切换回来。

答案 13 :(得分:3)

采用Lea的演示,这是一种制作带有边框的无序列表的不同方式:http://jsfiddle.net/vX4K8/7/

<强> HTML

<ul>
    <li>Foo</li>
    <li>Bar</li>
    <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</li>
        <ul>
        <li>Son</li>
        <li>Of</li>
            <ul>
            <li>Foo</li>
            <li>Bar</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</li>
            </ul>
        </ul>
</ul>

<强> CSS

ul {
list-style: none;
margin: 0;
}

ul:first-child {
   padding: 0;
}

li {
    line-height: 180%;
    border-bottom: 1px dashed #CCC;
    margin-left: 14px;
    text-indent: -14px;
}

li:last-child {
    border: none;
}

li:before {
    content: "";
    border-left: 4px solid #CCC;
    padding-left: 10px;
}

答案 14 :(得分:3)

Lea Verous解决方案很好,但我想更多地控制子弹的位置,所以这是我的方法:

.entry ul {
    list-style: none;
    padding: 0;
    margin: 0;
    /* hide overflow in the case of floating elements around ... */
    overflow: hidden;
}
.entry li { 
    position: relative;
    padding-left: 24px;
}
.entry li:before {
    /* with absolute position you can move this around or make it bigger without getting unwanted scrollbars */
    position: absolute;
    content: "• ";
    color: #E94E24;
    font-size: 30px;
    left: 0;
    /* use fonts like "arial" or use "sans-serif" to make the dot perfect round */ 
    font-family: Arial, sans-serif;
}

答案 15 :(得分:-13)

这样做..

li{
  color: #fff;
}