逆序列表的自定义编号

问题描述 投票:0回答:4

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

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>

以上代码的结果如下图所示。“在此处输入图像描述”

html css html-lists css-counter
4个回答
1
投票

我不使用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;
}

0
投票

如@durbnpoisn所述,您可以使用reversed属性。顺序由HTML控制,而不由CSS控制。另外,请注意,这是HTML5属性。

对于XHTML,您将需要javascript。

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

edit#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 */
}

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;
}

-1
投票

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

<ol reversed>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol> 
© www.soinside.com 2019 - 2024. All rights reserved.