Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number nested ordered lists in HTML

I have a nested ordered list.

<ol>   <li>first</li>   <li>second   <ol>     <li>second nested first element</li>     <li>second nested secondelement</li>     <li>second nested thirdelement</li>   </ol>   </li>   <li>third</li>   <li>fourth</li> </ol> 

Currently the nested elements start back from 1 again, e.g.

  1. first
  2. second
    1. second nested first element
    2. second nested second element
    3. second nested third element
  3. third
  4. fourth

What I want is for the second element to be numbered like this:

  1. first
  2. second

    2.1. second nested first element

    2.2. second nested second element

    2.3. second nested third element

  3. third
  4. fourth

Is there a way of doing this?

like image 489
John Avatar asked Apr 28 '10 13:04

John


People also ask

How do you make an ordered list with numbers in HTML?

To create ordered list in HTML, use the <ol> tag. Ordered list starts with the <ol> tag. The list item starts with the <li> tag and will be marked as numbers, lowercase letters uppercase letters, roman letters, etc. The default numbers for list items.

How do you create a numbered list in HTML?

In HTML, we can create an ordered list using the <ol> tag. The ol in the tag stands for an ordered list. Inside each of the ordered list elements <ol> and <ol /> , we have to define the list items. We can define the list items using the <li> tag.


2 Answers

Here's an example which works in all browsers. The pure CSS approach works in the real browsers (i.e. everything but IE6/7) and the jQuery code is to cover the unsupported. It's in flavor of an SSCCE, you can just copy'n'paste'n'run it without changes.

<!doctype html> <html lang="en">     <head>         <title>SO question 2729927</title>         <script src="http://code.jquery.com/jquery-latest.min.js"></script>         <script>             $(document).ready(function() {                 if ($('ol:first').css('list-style-type') != 'none') { /* For IE6/7 only. */                     $('ol ol').each(function(i, ol) {                         ol = $(ol);                         var level1 = ol.closest('li').index() + 1;                         ol.children('li').each(function(i, li) {                             li = $(li);                             var level2 = level1 + '.' + (li.index() + 1);                             li.prepend('<span>' + level2 + '</span>');                         });                     });                 }             });         </script>         <style>             html>/**/body ol { /* Won't be interpreted by IE6/7. */                 list-style-type: none;                 counter-reset: level1;             }             ol li:before {                 content: counter(level1) ". ";                 counter-increment: level1;             }             ol li ol {                 list-style-type: none;                 counter-reset: level2;             }             ol li ol li:before {                 content: counter(level1) "." counter(level2) " ";                 counter-increment: level2;             }             ol li span { /* For IE6/7. */                 margin: 0 5px 0 -25px;             }         </style>     </head>     <body>         <ol>             <li>first</li>             <li>second                 <ol>                     <li>second nested first element</li>                     <li>second nested second element</li>                     <li>second nested third element</li>                 </ol>             </li>             <li>third</li>             <li>fourth</li>         </ol>     </body> </html> 
like image 65
BalusC Avatar answered Oct 04 '22 06:10

BalusC


I know it is late to reply, but I just found an example of doing that using CSS. Add this to you CSS section (or file):

ol.nested {     counter-reset: item } li.nested {     display: block } li.nested:before {     content: counters(item, ".") ". ";     counter-increment: item } 

Here is an example of how your list code would look like:

<ol class="nested"> <li class="nested">item 1</li> <li class="nested">item 2     <ol class="nested">         <li class="nested">subitem 1</li>         <li class="nested">subitem 2</li>     </ol></li> <li class="nested">item 3</li> </ol> 

HTH

like image 35
Alexandre Avatar answered Oct 04 '22 04:10

Alexandre