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

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

    Note: Use CSS counters to create nested numbering in a modern browser. See the accepted answer. The following is for historical interest only.


    If the browser supports content and counter,

    .foo {
      counter-reset: foo;
    }
    .foo li {
      list-style-type: none;
    }
    .foo li::before {
      counter-increment: foo;
      content: "1." counter(foo) " ";
    }
    <ol class="foo">
      <li>uno</li>
      <li>dos</li>
      <li>tres</li>
      <li>cuatro</li>
    </ol>

    0 讨论(0)
  • 2020-11-22 10:45
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <meta name="author" content="Sandro Alvares - KingRider">
        </head>
        <body>
            <style type="text/css">
                li.title { 
                    font-size: 20px; 
                    font-weight: lighter; 
                    padding: 15px; 
                    counter-increment: ordem; 
                }
                .foo { 
                    counter-reset: foo; 
                    padding-left: 15px; 
                }
                .foo li { 
                    list-style-type: none; 
                }
                .foo li:before { 
                    counter-increment: foo; 
                    content: counter(ordem) "." counter(foo) " "; 
                }
            </style>
            <ol>
                <li class="title">TITLE ONE</li>
                <ol class="foo">
                    <li>text 1 one</li>
                    <li>text 1 two</li>
                    <li>text 1 three</li>
                    <li>text 1 four</li>
                </ol>
                <li class="title">TITLE TWO</li>
                <ol class="foo">
                    <li>text 2 one</li>
                    <li>text 2 two</li>
                    <li>text 2 three</li>
                    <li>text 2 four</li>
                </ol>
                <li class="title">TITLE THREE</li>
                <ol class="foo">
                    <li>text 3 one</li>
                    <li>text 3 two</li>
                    <li>text 3 three</li>
                    <li>text 3 four</li>
                </ol>
            </ol>
        </body>
    </html>
    

    Result: http://i.stack.imgur.com/78bN8.jpg

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

    I have some problem when there are two lists and second one is inside DIV Second list should start at 1. not 2.1

    <ol>
        <li>lorem</li>
        <li>lorem ipsum</li>
    </ol>
    
    <div>
        <ol>
            <li>lorem (should be 1.)</li>
            <li>lorem ipsum ( should be 2.)</li>
        </ol>
    </div>
    

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

    EDIT: I solved the problem by this http://jsfiddle.net/hy5f6161/

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

    this is proper code if you want to first child li resize of other css.

    <style>
        li.title { 
            font-size: 20px; 
    
            counter-increment: ordem; 
            color:#0080B0;
        }
        .my_ol_class { 
            counter-reset: my_ol_class; 
            padding-left: 30px !important; 
        }
        .my_ol_class li { 
              display: block;
            position: relative;
    
        }
        .my_ol_class li:before { 
            counter-increment: my_ol_class; 
            content: counter(ordem) "." counter(my_ol_class) " "; 
            position: absolute;
            margin-right: 100%;
            right: 10px; /* space between number and text */
        }
        li.title ol li{
             font-size: 15px;
             color:#5E5E5E;
        }
    </style>
    

    in html file.

            <ol>
                <li class="title"> <p class="page-header list_title">Acceptance of Terms. </p>
                    <ol class="my_ol_class">
                        <li> 
                            <p>
                                my text 1.
                            </p>
                        </li>
                        <li>
                            <p>
                                my text 2.
                            </p>
                        </li>
                    </ol>
                </li>
            </ol>
    
    0 讨论(0)
  • 2020-11-22 11:06

    In the near future you may be able to use the ::marker psuedo-element to achieve the same result as other solutions in just one line of code.

    Remember to check the Browser Compatibility Table as this is still an experimental technology. At the moment of writing only Firefox and Firefox for Android, starting from version 68, support this.

    Here is a snippet that will render correctly if tried in a compatible browser:

    ::marker { content: counters(list-item,'.') ':' }
    li { padding-left: 0.5em }
    <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>

    You may also want to check out this great article by smashingmagazine on the topic.

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