The </li> before a nested list. Why not?

P

patrick j

Hi

I'm wondering about lists with nested lists as one does on a Saturday
afternoon.

Anyway below is an example of a list with a nested list which the iCab
browser's very useful HTML verification ability will not like:

<ul>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<ul>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
</ul
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
</ul>

iCab complains that the <ul> for the nested list should not be there.
Removing the </li> immediately prior to the nested <ul> sorts this out, so
iCab is happy with what is below:

<ul>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a>
<ul>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
</ul
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
</ul>

I notice that at the rather excellent Max Design tutorials on lists they
leave out the </li> immediately prior to a nested list.

You can see this here:

<http://css.maxdesign.com.au/listamatic2/vertical01.htm>

So it would suggest it's not just the iCab verification system that doesn't
like it.

But it doesn't seem to make sense to leave out that </li> before the nested
list.

I'd be grateful if someone could explain why it is so?

Thank you :)
 
E

Els

patrick said:
Anyway below is an example of a list with a nested list which the iCab
browser's very useful HTML verification ability will not like:

<ul>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
<ul>

Oops! Bad code :)
iCab complains that the <ul> for the nested list should not be there.

iCab is correct.
Removing the </li> immediately prior to the nested <ul> sorts this out, so
iCab is happy with what is below: [snip]
<li><a href="#">link</a>
<ul>
<li><a href="#">link</a></li> [snip]
</ul
<li><a href="#">link</a></li>

If iCab likes that, it's missing a spot..
I notice that at the rather excellent Max Design tutorials on lists they
leave out the </li> immediately prior to a nested list.

No, they don't leave it out - they place it in the right place.

Exactly. That's correct code :)
So it would suggest it's not just the iCab verification system that doesn't
like it.

True.
But it doesn't seem to make sense to leave out that </li> before the nested
list.

It does. The nested ul is not nested if it's not inside the <li>
element.
The correct code is:
<ul>
<li>....</li>
<li>....
<ul>
<li>...</li>
<li>...</li>
</ul>
</li>

etc.

See the closing </li> after the nested <ul>?
A nested list, is a child of a list item.
 
P

patrick j

See the closing </li> after the nested <ul>?
A nested list, is a child of a list item.

Thank you. I didn't notice the closing </li> after nested <ul> and that's
where I went wrong :)

It seems that the nested <ul> is a child of the <li> as you say. I had
originally thought it would be a child of the not-nested <ul>.

I learn something every day :)
 
B

Bergamot

patrick said:
<li><a href="#">link</a></li>
<ul>

The above is invalid syntax, as any HTML validator will tell you.
But it doesn't seem to make sense to leave out that </li> before the nested
list.

Yes, it does. As Els said, the nested list is part of the parent <li>,
not separate from it. That's what makes it nested, not merely the
placement of the child said:
I'd be grateful if someone could explain why it is so?

The reason you see so much of the invalid syntax version is because many
"WYSIWYG" tools don't generate correct syntax for nested lists. The only
reason it doesn't look broken on screen is because of browser error
correction.
 
B

BootNic

Els said:
news: (e-mail address removed)
patrick j wrote:
[snip]
Removing the </li> immediately prior to the nested <ul> sorts this
out, so iCab is happy with what is below: [snip]
But it doesn't seem to make sense to leave out that </li> before the
nested list.
[snip]

As a side note, the li start tag is required, the end tag: optional.

http://www.w3.org/TR/html401/struct/lists.html

--
BootNic Saturday, March 31, 2007 11:17 AM

When men are pure, laws are useless; when men are corrupt, laws are
broken.
*Benjamin Disraeli*
 
A

Andy Dingley

I'm wondering about lists with nested lists as one does on a Saturday
afternoon.

Don't wonder, read the DTD and _know_.

You can't (validly) put anything inside a <ul> (or <ol>) except
<li>. So _anything_ you put in there, text, <p> or another nested
<ul> needs to go inside <li>. Not "adjacent to" or "after" <li>, but
contained wholly inside it.

It's SGML, so closing elements are generally optional. As the parser
"knows" that the only thing allowed inside <ul> is <li>, then it can
assume several things about the document, even if the literal </li>
tags have (correctly and optionally) been omitted.

<ul><li></li><ul>...
is clearly invalid. <ul> just can't be a child of <ul>.

<ul><li></li><li><ul>...</ul></li>
is valid. Containment is satisfied and the things that are children in
the document are permitted to be children by the DTD

<ul><li><li><ul>...</ul><li>...
is also valid. Some </li> are (permissibly) omitted. An SGML parser
can automatically generate a document tree from this that's equivalent
to having parsed the following document:

<ul>
<li></li>
<li>
<ul>...</ul>
</li>
<li>...</li>
</ul>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top