JM0
JM0

Reputation: 437

Is a self-closing <li> tag valid?

I recently came across some HTML that used self-closing <li> tags like so:

<ul>
    <li/>
    <p>Item 1</p>
    <li/>
    <p>Item 2</p>
</ul>

This renders correctly in Google Chrome, but the Mozilla developer documentation doesn't make any mention of this form and my editor yells at me when I type this. Is this valid HTML?

Upvotes: 0

Views: 333

Answers (1)

Quentin
Quentin

Reputation: 943480

No, it isn't. You can test this with a validator.

The / is an error so browsers will ignore it, treating it as a start tag.

The <p> elements are therefore children of the <li> elements (which they have to be since they aren't allowed to be children of <ul> elements).

The next <li> (or </ul>) implicitly ends the previous <li> because the end tag is optional for that element.

Upvotes: 1

Related Questions