有序列表CSS样式包括父代号

时间:2012-12-12 14:18:09

标签: css html-lists nested

我们正在寻找使用CSS来创建如下所示的有序列表:

A.
A.1
A.2
B.
C.
C.1
C.2
C.2.1
C.2.2

你会如何在孩子中包含这样的父索引?

2 个答案:

答案 0 :(得分:5)

您应该使用CSS counters W3C specs as @Zeta )指出,但这是一种处理多个嵌套和拉丁计数器的方法。

ol{
    list-style: none;
}
ol.roman{
    counter-reset: roman;
}
ol.roman > li:before{
    counter-increment: roman;
    content: counter(roman, upper-latin)".";
    padding-right:5px;
}
ol.roman li ol{
    counter-reset: inner;
}
ol.roman ol li:before{
    counter-increment: inner;
    content: counter(roman, upper-latin)"."counters(inner,'.');
    padding-right:5px;
}

http://jsfiddle.net/gaby/nZQSF/

演示

答案 1 :(得分:3)

您需要使用CSS counters

示例(from MDN

CSS:

ol {
  counter-reset: section;                /* Creates a new instance of the
                                            section counter with each ol
                                            element */
  list-style-type: none;
}
li:before {
  counter-increment: section;            /* Increments only this instance
                                            of the section counter */
  content: counters(section, ".") " ";   /* Adds the value of all instances
                                            of the section counter separated
                                            by a ".". */
}

HTML:

<ol>
 <li>Entry
 <li>Entry
 <li>Entry with subentries
  <ol>
    <li>Entry
    <li>Entry
    <li>Entry
    <li>Entry
  </ol>
 <li>Entry
 <li>Entry
 <li>Entry with subentries
  <ol>
    <li>Entry
    <li>Entry
    <li>Entry
    <li>Entry
  </ol>
</ol>

JSFiddle