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

后端 未结 11 904
广开言路
广开言路 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

    You can use counters to do so:

    The following style sheet numbers nested list items as "1", "1.1", "1.1.1", etc.

    OL { counter-reset: item }
    LI { display: block }
    LI:before { content: counters(item, ".") " "; counter-increment: item }
    

    Example

    ol { counter-reset: item }
    li{ display: block }
    li:before { content: counters(item, ".") " "; counter-increment: item }
    <ol>
      <li>li element
        <ol>
          <li>sub li element</li>
          <li>sub li element</li>
          <li>sub li element</li>
        </ol>
      </li>
      <li>li element</li>
      <li>li element
        <ol>
          <li>sub li element</li>
          <li>sub li element</li>
          <li>sub li element</li>
        </ol>
      </li>
    </ol>

    See Nested counters and scope for more information.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 10:43

    The chosen answer is a great start, but it essentially forces list-style-position: inside; styling on the list items, making wrapped text hard to read. Here's a simple workaround that also gives control over the margin between the number and text, and right-aligns the number as per the default behaviour.

    ol {
        counter-reset: item;
    }
    ol li {
        display: block;
        position: relative;
    }
    ol li:before {
        content: counters(item, ".")".";
        counter-increment: item;
        position: absolute;
        margin-right: 100%;
        right: 10px; /* space between number and text */
    }
    

    JSFiddle: http://jsfiddle.net/3J4Bu/

    0 讨论(0)
  • 2020-11-22 10:43

    The solutions posted here did not work well for me, so I did a mixture of the ones of this question and the following question: Is it possible to create multi-level ordered list in HTML?

    /* Numbered lists like 1, 1.1, 2.2.1... */
    ol li {display:block;} /* hide original list counter */
    ol > li:first-child {counter-reset: item;} /* reset counter */
    ol > li {counter-increment: item; position: relative;} /* increment counter */
    ol > li:before {content:counters(item, ".") ". "; position: absolute; margin-right: 100%; right: 10px;} /* print counter */
    

    Result:

    screenshot

    Note: the screenshot, if you wish to see the source code or whatever is from this post: http://estiloasertivo.blogspot.com.es/2014/08/introduccion-running-lean-y-lean.html

    0 讨论(0)
  • 2020-11-22 10:43

    I needed to add this to the solution posted in 12 as I was using a list with a mixture of ordered list and unordered lists components. content: no-close-quote seems like an odd thing to add I know, but it works...

    ol ul li:before {
        content: no-close-quote;
        counter-increment: none;
        display: list-item;
        margin-right: 100%;
        position: absolute;
        right: 10px;
    }
    
    0 讨论(0)
  • 2020-11-22 10:44

    The following worked for me:

    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, ".") ") ";
    }
    

    Look at: http://jsfiddle.net/rLebz84u/2/

    or this one http://jsfiddle.net/rLebz84u/3/ with more and justified text

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