How to Number Nested Ordered Lists in HTML
In HTML, nested ordered lists are typically displayed with nested numbering. By default, each nested level starts from 1 again. However, it may be desirable to number nested elements sequentially.
CSS Approach (Works in modern browsers)
To achieve sequential numbering, you can utilize the CSS list-style-type and counter-increment properties:
html>/**/body ol { 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; }
This method interprets the contents of ol tags as simple lists, allowing for sequential numbering. In the browser, it will display the nested ordered list as follows:
<ol> <li>first</li> <li> second <ol> <li>2.1. second nested first element</li> <li>2.2. second nested second element</li> <li>2.3. second nested third element</li> </ol> </li> <li>third</li> <li>fourth</li> </ol>
jQuery Approach (IE6/7 Compatibility)
For browsers that do not support the CSS approach (IE6/7), you can use jQuery to add the desired numbering:
<script> $(document).ready(function() { if ($('ol:first').css('list-style-type') != 'none') { $('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 + ''); }); }); } }); </script>
This JavaScript code targets the unsupported browsers and modifies the unordered list structure to achieve the desired numbering. In these browsers, it will display the nested ordered list as follows:
<ol> <li>first</li> <li> <p>second</p> <p>2.1. second nested first element</p> <p>2.2. second nested second element</p> <p>2.3. second nested third element</p> </li> <li>third</li> <li>fourth</li> </ol>
By combining both approaches, you can ensure that nested ordered lists are numbered sequentially in all major browsers.
The above is the detailed content of How to Number Nested Ordered Lists Sequentially in HTML?. For more information, please follow other related articles on the PHP Chinese website!

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
