CSS 如何设置 html 列表中的数字样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11182775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 04:17:21  来源:igfitidea点击:

How to style the number on a html list?

csshtml-lists

提问by Filype

Is it possible to style or increase the size of the numbers onlyon a ordered list?

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

I am planning on turning a ordered list olinto something like wikihowsteps using CSS only.

我计划仅使用 CSS将有序列表ol转换为类似于wikihow步骤的内容。

Here is a example to play with: http://jsfiddle.net/ejRzy/1/

这是一个可以使用的示例:http: //jsfiddle.net/ejRzy/1/

回答by rlemon

It can be done using CSS3 but not 100% cross browser (namely IE7). using the pseudo :before element and counter-reset and counter-increment you can hide the list-style and create your own.

它可以使用 CSS3 完成,但不能 100% 跨浏览器(即 IE7)。使用伪 :before 元素和计数器重置和计数器增量,您可以隐藏列表样式并创建自己的列表样式。

here is an article outlining how: Styling ordered list numbers

这是一篇概述方法的文章:样式化有序列表编号

and hereis a demo built from that article.

这里是那篇文章建立了一个演示。

Also in case of the dreaded link rot - here is the main CSS code required (this can be applied to any ordered list)

同样在可怕的链接腐烂的情况下 - 这是所需的主要 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;}?

This code will produce a custom ordered list; albeit not the style you asked for. I will leave the customization work upto you :) cheers

此代码将生成自定义有序列表;虽然不是你要求的风格。我会把定制工作留给你:) 欢呼

回答by Keith Nicholas

kind of.... style your ordered list with the font size you want for the numbers, then wrap all your list items in spans, and give them a different style.

有点......用你想要的数字字体大小来设计你的有序列表,然后用跨度包装你的所有列表项,并给它们一个不同的样式。

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

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