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

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

输出:

- abc

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

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


当前回答

有一个简单的修复(文本缩进)来保持缩进列表效果,在伪类之前使用:。

ul { 保证金:0; } ul。{破灭 list-style-type:没有; } ul。虚线> li { indent: 5 px; } ul。虚线> li:before { 内容:“-”; indent: 5 px; } 一些文本 < ul类=“冲”> <李>第一李< / > <李>第二李< / > <李>第三李< / > < / ul > < ul > <李>第一李< / > <李>第二李< / > <李>第三李< / > < / ul > 最后的文本

其他回答

CSS:

li:before {
  content: '— ';
  margin-left: -20px;
}

li {
  margin-left: 20px;
  list-style: none;
}

HTML:

<ul>
  <li>foo</li>
  <li>bar</li>
</ul>
ul {
margin:0;
list-style-type: none;
}
li:before { content: "- ";}

我的解决方案是在添加额外的span标签与mdash:

<ul class="mdash-list">
    <li><span class="mdash-icon">&mdash;</span>ABC</li>
    <li><span class="mdash-icon">&mdash;</span>XYZ</li>
</ul>

并添加到css:

ul.mdash-list 
{
    list-style:none;
}

ul.mdash-list  li
{
    position:relative;
}

ul.mdash-list li .mdash-icon
{
    position:absolute;
    left:-20px;
}

有一个简单的修复(文本缩进)来保持缩进列表效果,在伪类之前使用:。

ul { 保证金:0; } ul。{破灭 list-style-type:没有; } ul。虚线> li { indent: 5 px; } ul。虚线> li:before { 内容:“-”; indent: 5 px; } 一些文本 < ul类=“冲”> <李>第一李< / > <李>第二李< / > <李>第三李< / > < / ul > < ul > <李>第一李< / > <李>第二李< / > <李>第三李< / > < / ul > 最后的文本

你可以使用:before和content:,记住这在ie7或以下版本中是不支持的。如果你同意,那么这就是你最好的解决方案。详见Can I Use或QuirksMode CSS兼容性表。

在老版本的浏览器中,还有一种稍微糟糕一点的解决方案,那就是使用图像作为项目符号,并使图像看起来像一个破折号。有关示例,请参阅W3C列表样式的图像页面。