Can ordered list produce result that looks like 1.1, 1.2, 1.3 (instead of just 1, 2, 3, …) with css?

后端 未结 11 1066
广开言路
广开言路 2020-11-22 10:13

Can an ordered list produce results that looks like 1.1, 1.2, 1.3 (instead of just 1, 2, 3, ...) with CSS? So far, using list-style-type:decimal has produced on

11条回答
  •  醉话见心
    2020-11-22 10:42

    None of solutions on this page works correctly and universally for all levels and long (wrapped) paragraphs. It’s really tricky to achieve a consistent indentation due to variable size of marker (1., 1.2, 1.10, 1.10.5, …); it can’t be just “faked,” not even with a precomputed margin/padding for each possible indentation level.

    I finally figured out a solution that actually works and doesn’t need any JavaScript.

    It’s tested on Firefox 32, Chromium 37, IE 9 and Android Browser. Doesn't work on IE 7 and previous.

    CSS:

    ol {
      list-style-type: none;
      counter-reset: item;
      margin: 0;
      padding: 0;
    }
    
    ol > li {
      display: table;
      counter-increment: item;
      margin-bottom: 0.6em;
    }
    
    ol > li:before {
      content: counters(item, ".") ". ";
      display: table-cell;
      padding-right: 0.6em;    
    }
    
    li ol > li {
      margin: 0;
    }
    
    li ol > li:before {
      content: counters(item, ".") " ";
    }
    

    Example:

    Try it on JSFiddle, fork it on Gist.

提交回复
热议问题