Can I use a div inside a list item?

HtmlValidationXhtmlHtml Lists

Html Problem Overview


Why is the following code valid when I am using a <div> inside a <li>?

<ul>
    <li class="aschild">
        <div class="nav">Test</div>
    </li>
</ul>

Html Solutions


Solution 1 - Html

Yes you can use a div inside a li and it will validate.

<!ELEMENT li %Flow;>
<!ENTITY % Flow "(#PCDATA | %block; | form | %inline; | %misc;)*">
<!ENTITY % block     "p | %heading; | div | %lists; | %blocktext; | fieldset | table">

Solution 2 - Html

Inside a <li> you can have anything you could naturally put inside a <div>. They are no different in this sense.

It should be valid in HTML4, XHTML and HTML5 as well.

This is NOT valid though (so the sources you found about "no divs in lists" could refer to this situation):

<ul>
    <li></li>
    <div></div>
    <li></li>
</ul>

So: Lists (ul, ol) can only have lis as their children. But lis can have anything as their children.

Solution 3 - Html

Because <li> is a block element, not an inline element like <span> or <a>.

Solution 4 - Html

An <li> is a block element, and will work perfectly fine with other block elements inside.

Solution 5 - Html

Yes, you can. As much as you want.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManish BasdeoView Question on Stackoverflow
Solution 1 - HtmlJawadView Answer on Stackoverflow
Solution 2 - HtmlkapaView Answer on Stackoverflow
Solution 3 - HtmltwsaefView Answer on Stackoverflow
Solution 4 - HtmlAdamView Answer on Stackoverflow
Solution 5 - HtmlJose FaetiView Answer on Stackoverflow