Search code examples
htmlcsstableofcontents

How can I make subsections in a Contents list?


I'm trying to make a Contents list on my article page but my validator keeps telling me <ol> and <ul> cannot be child elements of another <ol> or <ul>.

Is there any way I can enable this in the contents without my coding having errors?

My current markup is like this:

<!DOCTYPE html>
<head>
    <title></title>
    <meta charset="UTF-8">
</head>
<body>
    <h2>Contents</h2>
    <div>
        <ol>1
            <ol>1.1
            </ol>
            <ol>1.2
            </ol>
            <ol>1.1.1
            </ol>
        </ol>
    </div>
</body>
</html>

Thanks, Mike


Solution

  • Each ol element can only contain li tags. If you want to nest lists you have to use this structure:

    <!DOCTYPE html>
    <head>
    <title></title>
    <meta charset="UTF-8">
    </head>
    <body>
        <h2>Contents</h2>
        <div>
            <ol>
                <li>
                    <span>1</span>      
                    <ol>
                        <li>1.1</li>
                        <li>1.2</li>
                        <li>
                            <ol>
                                <li>1.2.1</li>
                                <li>1.2.2</li>
                            </ol>
                        </li>
                        <li>1.3</li>
                    </ol>
                <li>
                <li>
                    <span>2</span>      
                    <ol>
                        <li>2.1</li>
                        <li>2.2</li>
                        <li>2.3</li>
                    </ol>
                </li>
            </ol>
        </div>
    </body>
    </html>