【问题标题】:How to style the number on a html list?如何在 html 列表中设置数字样式?
【发布时间】:2012-06-26 07:42:40
【问题描述】:

是否可以在有序列表中设置数字样式或增加其大小?

我计划仅使用 CSS 将有序列表 ol 变成类似 wikihow 的步骤。

这里有一个例子:http://jsfiddle.net/ejRzy/1/

【问题讨论】:

标签: css html-lists


【解决方案1】:

可以使用 CSS3 但不能 100% 跨浏览器(即 IE7)。使用伪 :before 元素和 counter-reset 和 counter-increment 您可以隐藏列表样式并创建自己的样式。

这里有一篇文章概述了如何:Styling ordered list numbers

here 是根据那篇文章构建的演示。

同样在可怕的链接失效的情况下 - 这是所需的主要 CSS 代码(这可以应用于任何有序列表)

ol {
    counter-reset:li; /* Initiate a counter */
    margin-left:0; /* Remove the default left margin */
    padding-left:0; /* Remove the default left padding */
}
ol > li {
    position:relative; /* Create a positioning context */
    margin:0 0 6px 2em; /* Give each list item a left margin to make room for the numbers */
    padding:4px 8px; /* Add some spacing around the content */
    list-style:none; /* Disable the normal item numbering */
    border-top:2px solid #666;
    background:#f6f6f6;
}
ol > li:before {
    content:counter(li); /* Use the counter as content */
    counter-increment:li; /* Increment the counter by 1 */
    /* Position and style the number */
    position:absolute;
    top:-2px;
    left:-2em;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    box-sizing:border-box;
    width:2em;
    /* Some space between the number and the content in browsers that support
       generated content but not positioning it (Camino 2 is one example) */
    margin-right:8px;
    padding:4px;
    border-top:2px solid #666;
    color:#fff;
    background:#666;
    font-weight:bold;
    font-family:"Helvetica Neue", Arial, sans-serif;
    text-align:center;
}
li ol,
li ul {margin-top:6px;}
ol ol li:last-child {margin-bottom:0;}​

此代码将生成一个自定义排序列表;尽管不是您要求的样式。我将把定制工作留给你 :) 干杯

【讨论】:

    【解决方案2】:

    有点.... 使用您想要的数字字体大小为您的有序列表设置样式,然后将所有列表项包装在跨度中,并赋予它们不同的样式。

    http://jsfiddle.net/keith_nicholas/MEHXj/

    【讨论】:

    • 这是一个不错的选择,正确的答案是虽然不可能
    • 当然,但是本着 stackoverflow 的精神,我们并没有止步于不可能,我们开始寻找解决问题的方法来实现我们想要的 :)
    • 虽然@rlemon 的答案是最好的,IMO,这个答案可能是最符合跨浏览器的,尽管它使用了更多的标记。为什么反对它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多