Ekin
Ekin

Reputation: 69

Auto generate numbering list in HTML using CSS

I've been working on TNC for my client. This content will have a lot of <li></li> tags. So in order to create the automated numbering, I've been using below code to do the magic.

CSS :

ol {counter-reset: section;list-style-type: none;padding-left:0}
ol li {counter-increment: section;font-weight:700}
ol li:before {content: counters(section, ".") ". "}
ol li ol {padding-left:15px}
ol li ol li {margin:10px 0}
ul {list-style-type:lower-alpha}
ul li:before {content: ""}

HTML :

<ol>
    <li>
    <span class="underline">Title</span>
        <ol>
            <li>
                Content
            </li>
            <li>
                Content
            </li>
            <li>
                <span class="underline">Sub title</span>
                <ul>
                    <li>
                        Sub Content
                    </li>
                    <li>
                        Sub Content
                    </li>
                    <li>
                        Sub Content
                    </li>
                </ul>
            </li>
            <li>
                Content
            </li>
        </ol>
    </li>
</ol>

OUTPUT :

Check this image output

jsFiddle

The problem is the counter seems to be broken after lower-alpha style list. I would like to avoid using javascript in this process as this page will be called in other page using AJAX.

Upvotes: 3

Views: 6020

Answers (1)

xkeshav
xkeshav

Reputation: 54072

TRY

ol {counter-reset: section;  list-style-type:none;padding-left:0}
ol li:before {counter-increment: section;font-weight:700;content: counters(section, ".") ". "}
ul {counter-reset: section;  list-style-type:lower-alpha}
ul li:before {counter-increment: section; content: ""}

working DEMO

update

add counter-reset on ul for the next (inner) listings

 ul {counter-reset: section;list-style-type:lower-alpha}
 ul  li {counter-increment: section;}
 ul li:before {content: ""}

Working Fiddle

Read more on Using CSS counters

Upvotes: 5

Related Questions