HTML列表样式类型破折号

Bri*_*unt 209 html css xhtml html-lists

有没有办法用破折号(即 - 或 - –或 - —)创建HTML格式的列表样式

<ul>
  <li>abc</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

输出:

- abc
Run Code Online (Sandbox Code Playgroud)

li:before { content: "-" };虽然我不知道该选项的缺点(并且非常有必要提供反馈),但是我想到了这样的事情.

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

小智 214

有一个简单的修复(文本缩进)来保持缩进列表效果与:before伪类.

ul {
  margin: 0;
}
ul.dashed {
  list-style-type: none;
}
ul.dashed > li {
  text-indent: -5px;
}
ul.dashed > li:before {
  content: "-";
  text-indent: -5px;
}
Run Code Online (Sandbox Code Playgroud)
Some text
<ul class="dashed">
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
</ul>
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
</ul>
Last text
Run Code Online (Sandbox Code Playgroud)

  • 可以工作,但不要忘记将`list-style-type:none;`放到`<ul>`元素中. (14认同)
  • 没有缩进只是`ul li:before {content:" - ";}` (5认同)
  • 我更喜欢添加`display:block; float:left;`到`li:before`,它可以更容易地将它从内容流中拉出来,否则让第一行和后续行正确排列是非常困难/错误的.或者Keyan Zhang的答案也是以绝对定位来做到的. (4认同)
  • 愚蠢的是,“列表样式类型破折号”已经不是CSS的一部分 (4认同)

Dar*_*o Z 105

您可以使用:beforecontent:牢记IE 7或更低版​​本不支持此功能.如果您对此感到满意,那么这是您最好的解决方案.有关完整详细信息,请参阅Can I UseQuirksMode CSS兼容性表.

在旧版浏览器中应该使用的稍微有点麻烦的解决方案是使用图像作为项目符号,并使图像看起来像破折号.有关示例,请参阅W3C list-style-image页面.

  • `:before`的问题是,当列表项跨越多行时,第二行的缩进从破折号所在的位置开始,从而破坏了缩进列表效果.您需要使用悬挂缩进来避免这种情况.见:http://www.alistapart.com/articles/taminglists/ (26认同)
  • 将mdash放在`content`中使用`content:"\ 2014\a0"` (2认同)

vel*_*lop 66

这是一个没有任何位置相对或绝对且没有文本缩进的版本:

ul.dash {
    list-style: none;
    margin-left: 0;
    padding-left: 1em;
}
ul.dash > li:before {
    display: inline-block;
    content: "-";
    width: 1em;
    margin-left: -1em;
}
Run Code Online (Sandbox Code Playgroud)

请享用 ;)


小智 64

用这个:

ul
{
    list-style: square inside url('data:image/gif;base64,R0lGODlhBQAKAIABAAAAAP///yH5BAEAAAEALAAAAAAFAAoAAAIIjI+ZwKwPUQEAOw==');
}
Run Code Online (Sandbox Code Playgroud)

  • 电子邮件唯一可行的解​​决方案 (7认同)
  • 不幸的是,它不尊重css文本的颜色. (3认同)

小智 30

ul {
  list-style-type: none;
}

ul > li:before {
  content: "–"; /* en dash */
  position: absolute;
  margin-left: -1.1em; 
}
Run Code Online (Sandbox Code Playgroud)

演示小提琴

  • 使用`content:'\ 2014';`用于unicode em dash (4认同)

thr*_*ree 23

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

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;
}
Run Code Online (Sandbox Code Playgroud)
<!-- 
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>
Run Code Online (Sandbox Code Playgroud)

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


小智 10

最重要的答案之一对我不起作用,因为经过一些反复试验,li:before 还需要 css 规则 display:inline-block。

所以这对我来说是一个完全有效的答案:

ul.dashes{
  list-style: none;
  padding-left: 2em;
  li{
    &:before{
      content: "-";
      text-indent: -2em;
      display: inline-block;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*ang 9

ul {
  list-style-type: '-';
}
Run Code Online (Sandbox Code Playgroud)

你可以参考MDN


小智 7

ul {
margin:0;
list-style-type: none;
}
li:before { content: "- ";}
Run Code Online (Sandbox Code Playgroud)


小智 6

就我而言,将此代码添加到CSS

ul {
    list-style-type: '- ';
}
Run Code Online (Sandbox Code Playgroud)

够了。就这么简单。

  • 这似乎仅在桌面Firefox中受支持:https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type (4认同)
  • 假设这是 CSS 的一个相对较新的补充,因为我找不到另一个提到它的答案,但这确实应该是当今公认的答案。 (2认同)

biz*_*lop 5

这是我的小提琴版本:

该(Modernizr的)级.generatedcontent<html>几乎意味着IE8 +和所有其他浏览器理智。

<html class="generatedcontent">
  <ul class="ul-dash hanging">
    <li>Lorem ipsum dolor sit amet stack o verflow dot com</li>
    <li>Lorem ipsum dolor sit amet stack o verflow dot com</li>
  </ul>
Run Code Online (Sandbox Code Playgroud)

CSS:

.ul-dash {
  margin: 0;
}

.ul-dash {
  margin-left: 0em;
  padding-left: 1.5em;
}

.ul-dash.hanging > li { /* remove '>' for IE6 support */
  padding-left: 1em;
  text-indent: -1em;
}  

.generatedcontent .ul-dash {
  list-style: none;
}
.generatedcontent .ul-dash > li:before {
  content: "–";
  text-indent: 0;
  display: inline-block;
  width: 0;
  position: relative;
  left: -1.5em;
}
Run Code Online (Sandbox Code Playgroud)