W3 says:
An
li
element’s end tag may be omitted if theli
element is immediately followed by anotherli
element or if there is no more content in the parent element.
But this:
<ol>
<li>one
<li>two
<li>three
<ol>
<li>three.one
<li>three.two
</ol>
<li>four
</ol>
Appears to render fine.
I don't know if four's li
is considered to "immediately follow" three's li
or not because there's an ol
inbetween. The spec isn't really clear -- technically the text "one" represents a TextNode so two's li
doesn't really immediately follow either.
Is there actually any scenario where a closing </li>
is necessary?
I've only got Chrome and Firefox installed, but they both render the above how I would expect.
As far as I'm aware, the only legal direct children of ul
and ol
are li
so there can't be any ambiguity, right?
Its totally your choice to give end tag at li . There have no strict rules to end tags . li ,br they can also perform without the end tag.
HTML li tag can be used inside ul or ol tags. Its closing tag is optional and it makes html more readable and less verbose. So it can be safely omitted.
Optional HTML Closing TagsSome closing tags are optional, however. The tags are optional because it's implied that a new tag would not be able to be started without closing it.
The ending tag is optional. However, it is recommended that it be included.
So, what W3C is saying is
An
li
element’s end tag is mandatory if theli
element is immediately followed by something else than anotherli
element or if there is more content in the parent element.
How is this possible? li
Elements can only occur within ol
s, ul
s and menu
s.
But ol
s und ul
s allow only li
s as their children.
What about menu
? It allows flow content too. So there you have it:
<menu>
<li>foo</li> <!-- mandatory -->
<a href="//example.com">bar</a>
</menu>
<menu>
<li>foo</li> <!-- mandatory -->
Hello, I am text content!
</menu>
When you see the examples it is pretty obvious that omitting the end tag would give the parser no chance to determine where the li
ends.
Edit: as @BoltClock points out below, script
and template
elements are now allowed too:
<ul> <!-- or ol or menu -->
<li>foo</li> <!-- mandatory -->
<script></script>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With