如何在有序列表中左对齐数字?
1. an item // skip some items for brevity 9. another item 10. notice the 1 is under the 9, and the item contents also line up
更改有序列表中的数字后面的字符?
1) an item
还有一个CSS解决方案,可以从数字更改为字母/罗马列表,而不是使用ol元素上的type属性.
我最感兴趣的是适用于Firefox 3的答案.
这是我在Firefox 3,Opera和Google Chrome中使用的解决方案.该列表仍显示在IE7中(但没有关闭括号和左对齐数字):
ol {
counter-reset: item;
margin-left: 0;
padding-left: 0;
}
li {
display: block;
margin-bottom: .5em;
margin-left: 2em;
}
li::before {
display: inline-block;
content: counter(item) ") ";
counter-increment: item;
width: 2em;
margin-left: -2em;
}
- One
- Two
- Three
- Four
- Five
- Six
- Seven
- Eight
- Nine
Items
- Ten
Items
样式列表的CSS在这里,但基本上是:
li {
list-style-type: decimal;
list-style-position: inside;
}
但是,你可能只能通过钻研的东西,如布局的内部结构来实现后是具体布局这个(请注意,我还没有真正尝试过):
ol { counter-reset: item }
li { display: block }
li:before { content: counter(item) ") "; counter-increment: item }
您还可以在HTML中指定自己的数字 - 例如,如果数字由数据库提供:
ol {
list-style: none;
}
ol>li:before {
content: attr(seq) ". ";
}
seq
使用类似于其他答案中给出的方法使该属性可见.但content: counter(foo)
我们不使用,而是使用content: attr(seq)
:
- Item one
- Item twenty
- Item three hundred
CodePen中的演示具有更多样式
从其他答案中偷了很多这个,但这对我来说在FF3中有效.它有upper-roman
均匀的凹痕,一个紧密的支架.
new document
- One
- Two
- Three
- Four
- Five
- Six
- Seven
- Eight
- Nine
- Ten
我建议使用:before属性,看看你能用它实现的目标.这将意味着你的代码确实是有限的,以漂亮的新的浏览器,并排除市场仍然使用旧的垃圾浏览器(烦人大)部分,
类似下面的内容,强制固定在项目上.是的,我知道这是那么优雅有自己选择的宽度,但使用CSS布局就像是卧底警察的工作:但是好你的动机,它总是会混乱.
li:before { content: counter(item) ") "; counter-increment: item; display: marker; width: 2em; }
但是你将不得不尝试找到确切的解决方案.
如果通过将list-style-type设置为:向数字添加前导零,则数字排列更好:
ol { list-style-type: decimal-leading-zero; }