How can I reset a CSS-counter to the start-attribute of the given list

前端 未结 6 1216
青春惊慌失措
青春惊慌失措 2020-11-30 06:50

I am using a self-styled, numbered list. How can I read the start-attribute and add it to the counter with CSS?

HTML

  1. Number
6条回答
  •  青春惊慌失措
    2020-11-30 07:19

    I see that this is an old question, but I'm putting this here because it may come to help someone yet.

    You cannot read an attribute in css counter properties. Instead, you could use inline css with counter-reset to define the starting number for a particular list.
    (Yes, I know it is not a best practice to use inline css, but it can and should be used for edge cases like this one)

    The first item increments the reset value by 1, so besides providing the counter name, you will need to subtract the number you want the list to start at by 1:

    HTML

    1. Number One
    2. Number Two
    3. Number Three
    1. Number Ten
    2. Number Eleven
    3. Number Twelve

    CSS

    ol {
        list-style-type: none;
        counter-reset: lis; /* Resets counter to zero unless overridden */
    }
    li {
        counter-increment: lis
    }
    li:before {
        content: counter(lis)". ";
        color: red;
    }
    

    FIDDLE (http://jsfiddle.net/hcWpp/308/)

提交回复
热议问题