Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a closing </li> not optional?

Tags:

html

W3 says:

An li element’s end tag may be omitted if the li element is immediately followed by another li 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?

like image 546
mpen Avatar asked Aug 04 '14 03:08

mpen


People also ask

Is closing tag for LI tag is optional?

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.

Is a closing Li required?

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.

Are closing tags optional in HTML?

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.

Is closing tag optional?

The ending tag is optional. However, it is recommended that it be included.


1 Answers

So, what W3C is saying is

An li element’s end tag is mandatory if the li element is immediately followed by something else than another li element or if there is more content in the parent element.

How is this possible? li Elements can only occur within ols, uls and menus. But ols und uls allow only lis 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>
like image 182
user123444555621 Avatar answered Oct 22 '22 16:10

user123444555621