tags:

views:

256

answers:

7

Is it ok to use <li> tags without parent tags?

i.e.

<li> some copy

or must it be..

<ul> (substitute your favorite list type)
    <li> some copy
</ul>

Links on the subject:

+19  A: 

If by OK you mean "correct, follows standards and will validate" then no. Per the spec, only OL and UL can contain LI. (MENU and DIR are deprecated)

If by OK you mean "will render" then yes.

Also, to be "OK" by the first definition, you must also close your LI tags. Every tag must either be self-closing (/>) or have a corresponding closing tag. Here's a quick and simple explanation of correct XHTML (and honestly, good HTML should conform to these as well. No reason not to.)

Rex M
I fully agree, except that I wouldn't be totally confident that it would render fine (at least in all browsers) - I suspect there are ones with stricter compliance modes that wouldn't like it. Have you tested it?
Noldorin
It'a always a crap shoot in quirks mode.
Chris Ballance
@Noldorin it probably would not render fine, I just said it would render. All the major browsers (IE, FF, Safari, Opera) will do their best to render markup even if it is not compliant with the doctype, no matter how strict. No browser simply halts when it encounters a validation error.
Rex M
I bet iCab complains about it, though.
Paul Tomblin
@Rex: You're right. I was interpreting "render" as "render well", when what you really meant is "display something, whatever it is", which I'm sure is true.Indeed, browsers tend to do the best with whatever tag soup they get, but in the worst case just ignore the entire element.
Noldorin
@Rex: 'Every tag must either be self-closing (/>) or have a corresponding closing tag.' - For XHTML, yes. For HTML 4.01, no. Empty tags (such as <BR> or <HR>) are not self-closing (/>) and the LI and a few other closing tags are optional as per the spec: http://www.w3.org/TR/html401/struct/lists.html#h-10.2
Grant Wagner
@Grant Properly closed tags are still valid HTML 4.01 - just because it is not *required* doesn't mean there's any good reason not to do it anyway (as stated in the answer).
Rex M
+3  A: 

No, using a li element without a parent ul or ol element would be invalid.

Gumbo
+1 for remembering to mention <ol> also
Chris Ballance
there are even more.. http://www.w3schools.com/HTML/html_lists.asp
madcolor
+6  A: 

Proper HTML (not to mention strict XHTML) should be:

<ul>
    <li>whatever</li>
</ul>
Yuval A
+3  A: 

No, that would definitely be invalid (X)HTML. I mean, you could, and the browser might render it correctly if you're lucky (IE in particular, as it tends to be especially lenient), but there's far from any guarantee. You should always enclose <li> tags with either <ul> (unordered list) or <ol> (ordered list). Why wouldn't you want to anyway?

Noldorin
A: 

li = list item, if it's without parents where's the list?

http://www.w3schools.com/TAGS/tag_li.asp

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

zalew
+1  A: 

No, while it might work in some browsers, it is invalid HTML.

Chris Ballance
+1  A: 

I checked the validation of the code below at Markup Validation Service of W3C :

<li>item 1</li>
<li>item 2</li>

and the result is :

document type does not allow element "LI" here; missing one of "UL", "OL", "DIR", "MENU" start-tag

Canavar