有没有一种方法可以在HTML中创建一个带有破折号的列表样式(即-或- –或- —)

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

输出:

- abc

我想到了用像li:before {content: "-"};这样的东西来做这件事,尽管我不知道这个选项的缺点(非常感谢反馈)。

更一般地说,我不介意知道如何为列表项使用通用字符。


当前回答

我不知道是否有更好的方法,但是您可以创建一个描述破折号的自定义项目符号图形,然后通过list-style-type属性让浏览器知道您想在列表中使用它。该页上的一个示例展示了如何使用图形作为项目符号。

我从来没有试过用你的方法,虽然它可能有用。缺点是一些较老的浏览器不支持它。我的本能反应是,这一点仍然很重要,值得考虑。在未来,这可能不那么重要了。

编辑:我已经用OP的方法做了一些测试。在IE8中,我无法让这项技术发挥作用,所以它肯定还不能跨浏览器。此外,在Firefox和Chrome中,同时将list-style-type设置为none似乎会被忽略。

其他回答

让我再加上我自己的版本,主要是为了让我再次找到自己喜欢的解决方案:

ul { list-style-type: none; /*use padding to move list item from left to right*/ padding-left: 1em; } ul li:before { content: "–"; position: absolute; /*change margin to move dash around*/ margin-left: -1em; } <!-- Just use the following CSS to turn your common disc lists into a list-style-type: 'dash' Give credit and enjoy! --> Some text <ul> <li>One</li> <li>Very</li> <li>Simple Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</li> <li>Approach!</li> </ul>

https://codepen.io/burningTyger/pen/dNzgrQ

ul {
  list-style-type: none;
}

ul > li:before {
  content: "–"; /* en dash */
  position: absolute;
  margin-left: -1.1em; 
}

演示小提琴

另一种方法:

li:before {
  content: '\2014\00a0\00a0'; /* em-dash followed by two non-breaking spaces*/
}
li {
  list-style: none;
  text-indent: -1.5em;
  padding-left: 1.5em;    
}

对于今天遇到这个问题的人来说,解决方法很简单:

列表样式:"- "

下面是一个没有任何相对或绝对位置,也没有文本缩进的版本:

ul.dash {
    list-style: none;
    margin-left: 0;
    padding-left: 1em;
}
ul.dash > li:before {
    display: inline-block;
    content: "-";
    width: 1em;
    margin-left: -1em;
}

喜欢。)