逆序列表的自定义编号

时间:2014-04-09 19:06:08

标签: html css html-lists css-counter

如何为反转的有序列表创建自定义计数器样式:

C10. Item 10
C9. Item 9
C8. Item 8
C7. Item 7
C6. Item 6
C5. Item 5
C4. Item 4
C3. Item 3
C2. Item 2
C1. Item 1

我发现this link完美地描述了如何按升序实现自定义编号。如何修改以下内容以获得反向自定义编号并仅将其应用于特定列表?

<!DOCTYPE html>
<html>
<style>
ol.cp {
    counter-reset: item;
    margin-left: 0;
    padding-left: 0;
}
ol.cp li {
    display: block;
    margin-bottom: .5em;
    margin-left: 2em;
}
ol.cp:before {
    display: inline-block;
    content: "C"counter(item)". ";
    counter-increment: item;
    width: 3em;
    margin-left: -2em;
}
</style>
<body>
<h2>My Items</h2>
    <p>
        <ol reversed>
            <li>item 10</li>
            <li>item 9</li>
            <li>item 8</li>
            <li>item 7</li>
            <li>item 6</li>
            <li>item 5</li>
            <li>item 4</li>
            <li>item 3</li>
            <li>item 2</li>
            <li>item 1</li>
        </ol>
    </p>
    <p>
        <ol class="cp" reversed>
            <li>item 10</li>
            <li>item 9</li>
            <li>item 8</li>
            <li>item 7</li>
            <li>item 6</li>
            <li>item 5</li>
            <li>item 4</li>
            <li>item 3</li>
            <li>item 2</li>
            <li>item 1</li>
        </ol>
    </p>
</body>

</html>

以下代码的结果如下图所示。 enter image description here

4 个答案:

答案 0 :(得分:0)

我可以提出的唯一解决方案,使用JS,主要是一个强力解决方案。但是,如果你的所有名单都在20或50以下......你想要写多少这些名单。您可以匹配列表的长度并将计数器设置为该值,然后递减计数器。

10个项目列表的示例:

ol.cp {
    counter-reset: item;
    margin-left: 0;
    padding-left: 0;
}
ol.cp li {
    display: block;
    margin-bottom: .5em;
    margin-left: 2em;
}
ol.cp li:before {
    display: inline-block;
    content:"C"counter(item)". ";
    counter-increment: item -1;
    width: 3em;
    margin-left: -2em;
}

ol.cp li:first-child:nth-last-child(10) {
    counter-reset: item 11;
}

问题是您需要为每个要匹配的长度创建一个。这是1-10和样本 - http://jsfiddle.net/Ue7dG/

ol.cp li:first-child:nth-last-child(1) {
    counter-reset: item 2;
}
ol.cp li:first-child:nth-last-child(2) {
    counter-reset: item 3;
}
ol.cp li:first-child:nth-last-child(3) {
    counter-reset: item 4;
}
ol.cp li:first-child:nth-last-child(4) {
    counter-reset: item 5;
}
ol.cp li:first-child:nth-last-child(5) {
    counter-reset: item 6;
}
ol.cp li:first-child:nth-last-child(6) {
    counter-reset: item 7;
}
ol.cp li:first-child:nth-last-child(7) {
    counter-reset: item 8;
}
ol.cp li:first-child:nth-last-child(8) {
    counter-reset: item 9;
}
ol.cp li:first-child:nth-last-child(9) {
    counter-reset: item 10;
}
ol.cp li:first-child:nth-last-child(10) {
    counter-reset: item 11;
}

答案 1 :(得分:0)

要反转自定义编号的顺序,我们需要知道列表中的项目总数,并指示计数器从该编号开始然后递减。

在您的示例中,列表中有10个数字,我们将告诉计数器从11开始。

ol.cp {
    counter-reset: item 11;
    margin-left: 0;
    padding-left: 0;
}

然后将每个项目的计数器减1。

ol.cp:before {
    display: inline-block;
    content: "C"counter(item)". ";
    counter-increment: item -1;
    width: 3em;
    margin-left: -2em;
}

答案 2 :(得分:-1)

创建列表时,请颠倒顺序:

<ol reversed>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol> 

答案 3 :(得分:-1)

正如@durbnpoisn所提到的,您可以使用reversed属性。订单由HTML控制,而不是由CSS控制 另请注意,这是HTML5属性。

对于XHTML,您需要使用javascript。

修改 的 相反的是只更改计数器,而不是内容 对于内容操作,您必须使用javascript。

编辑#2 检查一下,但请注意,您必须提供列表的上限 改变这些行:

ol{
counter-reset: item 3; /* set the upper boundry for the list, where to start the countdown */
}
/* note that i've changed it to li:before */
ol li:before{
    counter-increment: item -1; /* negative counter */
}