使用 Glyphicon 作为 LI 项目符号点 (Bootstrap 3)

Using a Glyphicon as an LI bullet point (Bootstrap 3)

如何更改 HTML 列表中的项目符号点并使用 Bootstrap 3 附带的字形图标?

这样:

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

显示为:

 [icon]  Facere possimus, omnis voluptas assumenda est, numquam eius modi
         omnis dolor repellendus. Non numquam eius modi numam dolor omnis 
         tempora incidunt ut labore.

 [icon]  Facere possimus, omnis voluptas assumenda est, numquam eius modi
         omnis dolor repellendus. Non numquam eius modi numam dolor omnis 
         tempora incidunt ut labore.

我宁愿 而不是 必须注入额外的 HTML 这样的...

<ul>
    <li><i class="glyphicon glyphicon-chevron-right"></i> ...</li>
    <li><i class="glyphicon glyphicon-chevron-right"></i> ...</li>
</ul>

稍微 CSS 就不会太难,而且 比使用图像作为子弹要好 很多 因为你可以缩放它并给它着色它会在所有分辨率下保持清晰。

  1. 打开 Bootstrap docs 并检查您要使用的字符,找到字形的字符代码。

  2. 在下面使用该字符代码CSS

    li {
        display: block;
    }
    
    li:before {
        /*Using a Bootstrap glyphicon as the bullet point*/
        content: "\e080";
        font-family: 'Glyphicons Halflings';
        font-size: 9px;
        float: left;
        margin-top: 4px;
        margin-left: -17px;
        color: #CCCCCC;
    }
    

    您可能希望调整颜色和边距以适合您的字体大小和品味。

View Demo & Code

我正在使用基于@SimonEast 回答的简化版本(仅使用相对位置):

li:before {
    content: "\e080";
    font-family: 'Glyphicons Halflings';
    font-size: 9px;
    position: relative;
    margin-right: 10px;
    top: 3px;
    color: #ccc;
}

如果有人来这里希望使用 Font Awesome 图标(就像我一样)查看这里:https://fontawesome.com/how-to-use/on-the-web/styling/icons-in-a-list

<ul class="fa-ul">
  <li><i class="fa-li fa fa-check-square"></i>List icons</li>
  <li><i class="fa-li fa fa-check-square"></i>can be used</li>
  <li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
  <li><i class="fa-li fa fa-square"></i>in lists</li>
</ul>

fa-ulfa-li 类 可以轻松替换无序列表中的默认项目符号。

如果你想碰巧使用 LESS,可以这样实现:

li {
    .glyphicon-ok();

    &:before {            
        .glyphicon();            
        margin-left:-25px;
       float:left;
    }
}

如果您想为每个列表项设置不同的图标,我建议在 HTML 中添加图标,而不是使用伪元素来保持 CSS 向下。它可以很简单地完成如下:

<ul>
  <li><span><i class="mdi mdi-lightbulb-outline"></i></span>An electric light with a wire filament heated to such a high temperature that it glows with visible light</li>
  <li><span><i class="mdi mdi-clipboard-check-outline"></i></span>A thin, rigid board with a clip at the top for holding paper in place.</li>
  <li><span><i class="mdi mdi-finance"></i></span>A graphical representation of data, in which the data is represented by symbols, such as bars in a bar chart, lines in a line chart, or slices in a pie chart.</li>
  <li><span><i class="mdi mdi-server"></i></span>A system that responds to requests across a computer network worldwide to provide, or help to provide, a network or data service.</li>
</ul>

-

ul {
  list-style-type: none;
  margin-left: 2.5em;
  padding-left: 0;
}
ul>li {
  position: relative;
}
span {
  left: -2em;
  position: absolute;
  text-align: center;
  width: 2em;
  line-height: inherit;
}

在这种情况下,我使用了 Material Design Icons

VIEW DEMO

使用 Font Awesome 5,标记比之前的答案复杂一点。根据 the FA documentation,标记应为:

<ul class="fa-ul">
  <li><span class="fa-li"><i class="fas fa-check-square"></i></span>List icons can</li>
  <li><span class="fa-li"><i class="fas fa-check-square"></i></span>be used to</li>
  <li><span class="fa-li"><i class="fas fa-spinner fa-pulse"></i></span>replace bullets</li>
  <li><span class="fa-li"><i class="far fa-square"></i></span>in lists</li>
</ul>