【发布时间】:2009-10-27 16:23:00
【问题描述】:
有序列表的默认低字母列表类型使用点“.”。有没有办法使用右括号代替 a)... b) ..etc?
【问题讨论】:
-
也许其中一个答案可以被选为正确的?...
有序列表的默认低字母列表类型使用点“.”。有没有办法使用右括号代替 a)... b) ..etc?
【问题讨论】:
这是一个巧妙的解决方案。 (老实说,我对此感到惊讶。) CSS 有一个名为 counters 的东西,例如,您可以在其中设置每个标题上的自动章节编号。稍作修改即可为您提供以下内容;您需要自己整理填充等。
ol {
counter-reset: list;
}
ol > li {
list-style: none;
}
ol > li:before {
content: counter(list, lower-alpha) ") ";
counter-increment: list;
}
<span>custom list style type (v1):</span>
<ol>
<li>Number 1</li>
<li>Number 2</li>
<li>Number 3</li>
<li>Number 4</li>
<li>Number 5</li>
<li>Number 6</li>
</ol>
适用于所有现代浏览器和 IE9+(可能还有 IE8,但可能有问题)。
更新:我添加了子选择器以防止嵌套列表拾取父样式。 trejder 在 cmets 中也有一个好处,即列表项对齐也搞砸了。 article on 456bereastreet 有一个很好的解决方案,它涉及绝对定位计数器。
ol {
counter-reset: list;
}
ol > li {
list-style: none;
position: relative;
}
ol > li:before {
counter-increment: list;
content: counter(list, lower-alpha) ") ";
position: absolute;
left: -1.4em;
}
<span>custom list style type (v2):</span>
<ol>
<li>Number 1</li>
<li>Number 2</li>
<li>Number 3</li>
<li>Number 4</li>
<li>Number 5</li>
<li>Number 6</li>
</ol>
Here is a jsFiddle 显示结果,包括嵌套列表。
【讨论】:
, lower-alpha。所以content 的值是counter(list) ") ";
ol 元素的start 属性
在最初的问题出现 10 多年后,标准(以及在某种程度上,实现)似乎已经赶上了。
CSS 现在提供::marker 伪类,可用于实现自定义列表标记:MDN。
使用::marker 自动缩进li 的内容,没有任何黑客攻击。据 MDN 称,截至 2021 年 2 月,它在 Firefox、Chrome 和 Edge 中得到支持,在 Safari 中部分支持(不适用于此用例)。
.container {
width: 400px;
}
ol.custom-marker {
counter-reset: list;
}
ol.custom-marker > li {
list-style: none;
counter-increment: list;
}
ol.custom-marker.parens-after.decimal > li::marker {
content: counter(list) ")\a0";
}
ol.custom-marker.parens-around.lower-roman > li::marker {
content: "(" counter(list, lower-roman) ")\a0";
}
<div class='container'>
<ol class='custom-marker parens-after decimal'>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
<li>Another list here
<ol class='custom-marker parens-around lower-roman'>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
</ol>
</li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
</ol>
</div>
content 中的\a0 是&nbsp;,因为::marker 不支持边距或填充。
【讨论】:
将它添加到 CSS 中会产生一些有趣的结果。很接近,但没有雪茄。
li:before {
display: inline-block;
width: 1em;
position: relative;
left: -0.5em;
content: ')'
}
-- 已编辑以在 cmets 中包含 Iazel 的解决方案 -----
我已经完善了您的解决方案:
li {
position: relative;
}
li:before {
display: inline-block;
width: 7px;
position: absolute;
left: -12px;
content: ')';
background-color: #FFF;
text-align: center;
}
背景和position: absolute 成功了!
【讨论】:
li { position: relative; } li:before { display: inline-block; width: 7px; position: absolute; left: -12px; content: ')'; background-color: #FFF; text-align: center; } 背景和位置:absolute 成功了! :)
基于 DisgruntledGoat 的回答,我根据需要对其进行了扩展以支持子列表和样式。在这里分享它以防它对某人有帮助。
https://jsfiddle.net/0a8992b9/ 输出:
(i)first roman
(a)first alpha
(b)second alpha
(c)third alpha
(d)fourth alpha
(ii)second roman
(iii)third roman
(a)first alpha
(b)second alpha
【讨论】:
text-indent 的负值添加到li 级别
这适用于 IE7、FF3.6、Opera 9.64 和 Chrome 6.0.4:
<ol start="a" type="a" style="font-weight: normal;">
<li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) </span> content for line number one;</li>
<li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) </span> content for line number two;</li>
<li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) </span> content for line number three;</li>
<li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) </span> content for line number four;</li>
<li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) </span> content for line number five;</li>
<li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) </span> content for line number six;</li>
</ol>
这是内联的,因为它是为电子邮件编码的,但要点是跨度充当内容块并将括号拉入负左区域,使其与列表编号对齐。两个边距是为了弥补IE7和FF的差异
希望这会有所帮助。
【讨论】:
display:inline-block; 吗?
这似乎有效:
ol {
counter-reset: list;
margin: 0;
}
ol > li {
list-style: none;
position: relative;
}
ol > li:before {
counter-increment: list;
content: counter(list, lower-alpha) ") ";
position: absolute;
left: -1.4em;
}
【讨论】:
在 Firefox 和较新版本的 Chrome/Edge/Chromium 中,您可以使用 @counter-style 定义自己的计数器样式,并使用 prefix 和 suffix 属性来定义计数器之前/之后的内容。 According to MDN,Safari 仍不支持此功能(截至 2021 年 10 月)。
@counter-style my-new-list-style {
system: extends lower-alpha;
suffix: ') ';
}
.container ol {
list-style: my-new-list-style;
}
<div class="container">
<ol>
<li>One.</li>
<li>Two!</li>
<li>Three?</li>
<li>Four...</li>
</ol>
</div>
【讨论】: