How can I prefix ordered list item numbers with a static string using CSS?

后端 未结 2 520
情深已故
情深已故 2020-12-01 16:12

I want this HTML...

  1. Apples
  2. Oranges

...to ren

相关标签:
2条回答
  • 2020-12-01 16:46

    The only pure CSS way is with counters:

    ol {
        counter-reset: item;
        list-style-type: none;
    }
    
    ol li:before {
        content: 'Q' counter(item, decimal) '. ';
        counter-increment: item;
    }
    

    You cannot achieve this besides using CSS counters (which were designed specifically for such use cases!) or JavaScript.

    By the way, it's decimal, not numbered.

    0 讨论(0)
  • 2020-12-01 16:50

    There is a, fragile, non-counter method, but it's prone to breaking:

    ol {
        list-style-type: decimal;
        margin: 0 0 0 2em;
    }
    
    li {
        position: relative;
    }
    
    ol li:first-letter {
        color: #f90;
        float: left;
        position: relative;
        margin-left: -2em;
    }
    

    JS Fiddle demo.

    0 讨论(0)
提交回复
热议问题