Numbering a nested list in a single ol list
If you can't change your HTML markup, you can simulate nested numbering on a single list with CSS counters.
You need to remove the default list-style-type
and add the counter with pseudo elements..sub-item
elements can have their own counter (sub-counter
in the following example) that doesn't affect the one set on all the <li>
elements (main-counter
in the following example) :
ol {
counter-reset: main-counter, sub-counter;
list-style-type:none;
padding:0;
}
li {
counter-increment: main-counter;
counter-reset: sub-counter;
}
li::before {
content: counter(main-counter) ". ";
}
li.sub-item {
counter-increment: sub-counter;
padding-left:1em;
counter-reset: none;
}
li.sub-item::before {
content: counter(sub-counter, lower-alpha) ". ";
}
<ol>
<li>One</li>
<li>Two</li>
<li>THree</li>
<li class="sub-item">Sub three 1</li>
<li class="sub-item">Sub three 2</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
<li>Seven</li>
<li class="sub-item">Sub three 1</li>
<li class="sub-item">Sub three 2</li>
<li>Eight</li>
</ol>
HTML Nested List
It looks like your problem is improper nesting. If you want to put a list in a list, it needs to be nested within a nested list item (i.e. ul > li > ul
), you should not have an unorder list directly in the root of another unordered list (i.e. ul > ul
). This is also true for ordered lists (ol
), and the mixture of ordered and unordered lists (e.g. ol>ul
, ul>ol
, ol>ul>ul>ol>ul
, etc.) For more information on nesting, see the Stack Overflow question “Proper way to make HTML nested list?”.
Your code should look more like this:
<ul>
<li>Out1</li>
<li>Out2
<ul>
<li>In1</li>
<li>In2
<ul>
<li>De1</li>
<li>De2</li>
</ul>
</li>
</ul>
</li>
<li>Out3
<ul>
<li>In3</li>
</ul>
</li>
</ul>
Additionally, your code displayed fine in my browser (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
), but it was malformed.
Related Topics
Selecting an Option in a Dropdown List Using Request Parameters
Using Mailto to Send Email With an Attachment
How to Position Elements Next to Each Other in a Div
How to Place Div in Top Right Hand Corner of Page
Table With Fixed Header and Fixed Column on Pure CSS
How to Prevent Flex-Items from Overflowing Flex Parent With No Wrap
How to Force Position Absolute With 100% Width to Fit into Parent Div With Padding
Open an Exe File Through a Link in a HTML File
Angular - Show Empty Row by Default in Drop Down List and Disable Button
Nested Ngfor Looping Over an Array of Objects
Angular, Image Not Found (Get 404)
Html/Css - Make Alert Disappear After a Few Seconds
Setting the Width of Inline Elements
Floated Elements of Variable Height Push Siblings Down
Why Aren't My Absolutely/Fixed-Positioned Elements Located Where I Expect
Rendering Problems Using Flexbox in Firefox and Chrome 48
How to Limit the Visible Options in an HTML <Select> Dropdown