Nested ordered lists with varied initial counter positions

In order to create an html ordered list, I am using a Nested counter.

This is the code snippet that I am using:

http://jsfiddle.net/Katalhama/YpgfF/

The expected output should be as follows:

0. zero
  0.1 zero.one
1. one
  1.1. one.one
  1.2. one.two
  1.3. one.three
2. two
  2.1 two.one
  2.2 two.two
    2.2.1 two.two.one
    2.2.2 two.two.two
3. three

However, the actual output that I am getting is different:

0. zero
  0.0 zero.one
1. one
  1.0 one.one
  1.1 one.two
  1.2 one.three
2. two
  2.0 two.one
  2.1 two.two
    2.1.0 two.two.one
    2.2.1 two.two.two
3. three

I would like the main list index to start at 0, while the sublists index starts at 1. I have considered using two counters but my knowledge of advanced CSS is still limited and I am unsure how to implement them correctly.

Thank you for any assistance!

Answer №1

Your code presents two significant issues that need attention. The first problem pertains to invalid HTML code structure as the ol elements should only contain direct children of li. However, your code includes ol elements as direct children of another ol. To rectify this, you should enclose the nested ol elements within li tags. The second issue relates to achieving desired functionality by adjusting the counter-reset property for distinct ol instances:

HTML:

 <!-- Your revised HTML code goes here -->

CSS:

#list { 
  counter-reset: item -1;
}
ol {
  counter-reset: item; 
  padding-left: 10px;  
}
LI { display: block }
LI:before { 
  content: counters(item, ".") " "; 
  counter-increment: item;
}

Demo 1

To address the issue without using an id attribute for the outermost ol, consider implementing the following modifications:

 <!-- Revised CSS for alternative implementation -->

Demo 2

Another strategic approach to resolve the problems in your code involves:

 <!-- Modified CSS for a different solution -->

Demo 3

Answer №2

For accurate numbers in the JS Fiddle example, adjust the counter-reset to start at item 0;

CSS

OL {
    counter-reset: item 0;
    padding-left: 10p;
}

LI { 
    display: block
}

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

Also, make sure your OL elements are contained within the LI elements, as you cannot have an OL inside another OL.

HTML:

<body>
    <ol>
        <li>one</li>
        <li>two
            <ol>
                <li>two.one</li>
                <li>two.two</li>
                <li>two.three</li>
            </ol>
        </li>
        <li>three
            <ol>
                <li>three.one</li>
                <li>three.two
                    <ol>
                        <li>three.two.one</li>
                        <li>three.two.two</li>
                    </ol>
                </li>
            </ol>
        </li>
        <li>four</li>
    </ol>
</body>

Answer №3

Hey there, it looks like your HTML code (in the fiddle) is not quite right. As indicated in the title, your <ol> elements should be properly nested within each other, meaning one should be inside another as shown below:

<ol>
  <li>Two
    <ol>
      <li>Two.one</li>
    </ol>
  </li>
</ol>

Currently, your <ol> elements are directly connected to each other. According to HTML standards, an ordered list (<ol>) must only have list items (<li>) as children or possibly script tags. Your current structure looks something like this:

<ol>
   <li>Two</li>
   <ol>
      <li>Two.one</li>
   </ol>
</ol>

Once you update it to the first example mentioned above, you can then apply different CSS styles for nested list items, such as the following:

ol { counter-reset: item -1 }
ol li ol  { counter-reset: item 0 }

You can see a demonstration of this setup here: http://jsfiddle.net/kevinvanlierde/YpgfF/1/

Answer №4

After modifying the Tyblitz HTML structure, the results were displayed correctly.

<body>
 <ol>
  <li>zero</li>
  <li>one
      <ol>
       <li>one.one</li>
       <li>one.two</li>
       <li>one.three</li>
      </ol>
  </li>
  <li>two
      <ol>
       <li>two.one</li>
       <li>two.two
            <ol>
             <li>two.two.one</li>
             <li>two.two.two</li>
            </ol>
        </li>
       </ol>
     </li>
  <li>three</li>
  </ol>
 </body>

CSS

OL { counter-reset: item -1; padding-left: 10px;  }
ol li ol { counter-reset: item 0; }
LI { display: block }
LI:before { content: counters(item, ".") " "; counter-increment: item }

JSFiddel

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can recursive data be displayed in a template?

I am working with a model in Django that has a ForeignKey pointing to itself, and I need to display all the data from the database using lists and sublists: Below is my model definition: class Place(models.Model) name = models.CharField(max_length=1 ...

Please refrain from altering the color of my flag on hover

I am working with a table that has rows marked with flags. Below is the CSS I have used: <table class="hovered no-border" data-url="@Url.Action("General", "Transport")"> <tbody> <tr> <td><input type="che ...

Adjustment of Facebook button positioning in floating bar on Firefox

My floating bar currently has 5 social buttons, but I've noticed that the Facebook button is shifting about 20 pixels to the left. Could this be an issue with the CSS elements? After inspecting the elements, I found that when I remove position: absol ...

Excessive text overflowing in a chat interface can be resolved through CSS styling

After conducting a thorough search, it appears that very few individuals are taking this important element into consideration. I currently have a chat interface set up with a div element containing a ul, which then houses li elements that contain p element ...

`Animate your divs with slide in and slide out effects using

Currently, I am in the process of replicating a website and facing some challenges. This site is my inspiration for the project. I have managed to create a sliding effect using CSS, making the div slide in and out from the same direction. However, I want ...

Dynamic visual content (map)

I'm currently working on creating an interactive image for my website where clicking on points A, B, C, ... N will reveal bubbles with images and text inside them. Would anyone be able to provide guidance on how to achieve this? Or direct me to resou ...

Tab order in Angular Forms can be adjusted

While constructing a two-column form for simplicity, I utilized two separate divs with flexbox. However, the tabbing behavior is not ideal as it moves down the form rather than moving across when using the tab key to navigate between inputs. I am seeking a ...

Setting the `setCustomValidity()` method for dynamically inserted HTML elements Explanation on how to use

I am dynamically adding elements to my view and setting their attributes using the jQuery attr function as demonstrated in the code snippet below: var input = $("<input type='text' name='"+inputFieldName+"' '>"); input.attr( ...

Issues with Toggling Visibility in HTML, CSS, and Javascript

Currently, I am working on a website and following a tutorial called "Creating Slideshow using HTML, CSS, and Javascript" by W3Schools. In the project, I want to hide the thumbnail images located at the bottom and the navigation arrows initially and have t ...

Scrolling horizontally with Bootstrap 4

I'm having trouble with this issue: I need my "Factuur" (Dutch for invoice) to always be at a width of 800px, but on mobile devices, the rows are collapsing into each other. I want the invoice to remain static and be scrollable. Below is my CSS code: ...

What is the best way to split an array into four columns and allocate 10 <li> items from the array to each column?

If I have a dynamic array with 40 elements that changes on every render, how can I loop through the array and return 4 groups of elements, each containing 10 items without any repetition? I want to style these objects in the array using a parent flex con ...

Interaction between index file and module instance

As I develop a computer system, I have divided it into various smaller components. My experience in software development has taught me the value of keeping systems compact and focused. To achieve this, I am creating modules that perform specific function ...

Search for the specified pattern in BBEdit and delete the entire table that matches

While I am not an expert, I recently acquired BBEdit for a specific project. My current task involves working on an HTML file that contains numerous entries I need to eliminate. Specifically, I want to remove all tables that contain the text "NOT CONVERTE ...

Guide on creating a menu that remains open continuously through mouse hovering until the user chooses an option from the menu

I have a unique scenario where I am working with two images. When the mouse hovers over each image, two corresponding menu bars appear. However, the issue is that when the mouse moves away from the images, the menu disappears. Any suggestions on how to im ...

Is it possible to create editable Word documents programmatically in ASP.NET?

The goal is to create proposal documents that can be easily modified in Word before sending them to customers. Most of the proposal content will come from existing HTML website material (CMS) and some custom content for specific situations. Conditional lo ...

How can I expand and collapse all the Bootstrap panels simultaneously?

Is it possible to make all three panels collapse and expand simultaneously with just one button? <button data-toggle="collapse" data-target="#collapseOne-1,#collapseTwo-1,#collapseThree-1" ...></button> <div id="#collapseOne-1" class="coll ...

Alignment of input fields and labels in Bootstrap by utilizing the Grid System

Currently, I am in the process of developing a form that includes multiple input fields. To organize these fields, I have utilized the Bootstrap 4 Grid system. However, I am encountering challenges when it comes to aligning certain input fields. Snippet ...

Having trouble with a fixed element that won't scroll?

Code Example: <div id="tmenu" style="direction:rtl;"> <img src="assets/imgs/menu/all.jpg"/> <img src="assets/imgs/menu/sweets.jpg"/> <img src="assets/imgs/menu/main meals.jpg"/> <img src="assets/imgs/menu/ma5bozat.jpg"/& ...

Having trouble with the menu toggle button on Bootstrap 4?

When using Bootstrap 4, the breadcrumb button may not function properly when the header becomes responsive. I have ensured that Bootstrap 4 CSS and JS are included in the project. Please assist me in resolving this issue. Code: .navbar { height:100 ...

Using jQuery to animate two images simultaneously in opposite directions

Trying to make two images animate using a single function. The issue is that one image needs to animate the left attribute while the other needs to animate the right. Here's what I have so far: $(document).ready(function(){ v ...