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

前端 未结 6 1203
青春惊慌失措
青春惊慌失措 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:26

    Simply add:

    ol:not(:nth-of-type(1)){
        counter-increment: lis 10;
    }
    

    Demo Fiddle

    You cant use attr in counter-reset unfortunately, but you can add rules to alter the increment amount.

    Alternative 1

    If you are going to have multiple lists, a more resilient version would be:

    ol {
        list-style-type: none;
        /* this does not work like I expected */
        counter-reset: lis;
    
    }
    ol:not(:first-of-type){
         counter-increment: ol
    }
    li {
        counter-increment: lis
    }
    li:before {
        content: counter(lis)". ";
        color: red;
    }
    ol:not(:first-of-type) li:before {
        content: counter(ol) counter(lis)". ";
        color: red;
    }
    

    Alternative 2

    If the numerical prefix can be anything, the below will provision for this:

    HTML

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

    CSS

    ol {
        list-style-type: none;
        counter-reset: lis;
    }
    li {
        counter-increment: lis
    }
    li:before {
        content: attr(data-prefix) counter(lis)". ";
        color: red;
    }
    

提交回复
热议问题