17

With HTML and CSS, can I achieve this?

1.
1.1
1.2
2.
2.1
2.2

using <li> and <ul> tags?

TylerH
  • 20,799
  • 66
  • 75
  • 101
78lro
  • 1,790
  • 7
  • 37
  • 63

8 Answers8

14

For CSS-compliant browsers you could use:

ul { counter-reset:item; }
ul > li { counter-increment:item; }
ul > li:before {content: counter(item); }

ul > li > ul { counter-reset:subitem; }
ul > li > ul > li { counter-increment:subitem; }
ul > li > ul > li:before { content: counter(item) "." counter(subitem); }
TylerH
  • 20,799
  • 66
  • 75
  • 101
dan richardson
  • 3,871
  • 4
  • 31
  • 38
8

There's no cross-browser way of doing it.

The best you can achieve is nested ordered lists:

<ol>
    <li>Item 1
        <ol>
            <li>Subitem 1</li>
        </ol>
    </li>
</ol>

and then style each nested list to have a different type:

ol {
    list-style-type: upper-roman;
}
ol ol {
    list-style-type: decimal;
}

Hope this helps!

Andrew Dunkman
  • 1,131
  • 2
  • 8
  • 18
1

http://www.w3.org/TR/CSS2/generate.html

But, yeah, this is for modern browsers only. Nested OLs is probably the way to go.

graphicdivine
  • 10,937
  • 7
  • 33
  • 59
1

Yes, this can be done with CSS Counters. However, it doesn't work on all browsers.

Rob
  • 26,989
  • 16
  • 82
  • 98
elmuerte
  • 720
  • 5
  • 21
1

Consider using DL/DT/DD instead of OL/UL and hard-code the values in the DT.

richardtallent
  • 34,724
  • 14
  • 83
  • 123
1

CSS 2.1 provides user-definable counters, which can be used to count elements. Combined with the :before and :after pseudo-classes, you can output the counter value to create automatically numbered headings.

Unfortunately, at least Internet Explorer does not support any of this, even in the latest version. But at least Firefox does support it very well, so if you just want to add it as a enhancement which does not make your site unusable if it is not supported, you can still use it.

Simon Lehmann
  • 10,737
  • 4
  • 41
  • 53
0

One line of javascript will do this. Used in a table of contents:

<td class="session">
<script>
{document.getElementsByClassName("session")[num].innerHTML = "1." + (++num);}
</script>
</td>

num is a global variable in the document initialized to zero at an appropriate place upstream. The above produces 1.1 at the first instance. Change to num++ and get 1.0.

Mick536
  • 19
  • 1
-5

html code :

<ul>
  <li>1  </li>          
  <li>1.1</li>    
  <li>1.2</li>      
  <li>2  </li>                  
  <li>2.1</li>  
  <li>2.2</li>  
</ul>

css code:

li  {   list-style:none; }