Navigation <li> vs <a>

Hi

Are there any reasons why shouldn’t I use <a> tags instead of <ul> <li> tags in building a menu?

For example

<div class=“menu”>
<a href=“#”>MENUITEM1</a>
<a href=“#”>MENUITEM2</a>
</div>

Instead of…

<ul id=“menu”>
<li><a href=“#”>MENUITEM1</a></li>
<li><a href=“#”>MENUITEM2</a></li>
</ul>

It’s more semantically correct to use a list (plus your not stuffing a generic DIV where it’s not needed). A menu is by definition a list of links to various sections of a website, therefore it should be provided as such in a list. If you’re going to code a website, you may as well use the right tool for the right job. :slight_smile:

There is no real reason why you shouldn’t but like was mentioned it’s considered slightly more semantic to use LI. Although it’s not really any more an “unordered list” than a paragraph is a list of words (in some cases) it a kind of limitation of current navigation-specific elements. Now, if the links required OL then yes, it is more apt - I am not disagreeing just putting forward a different perspective. :slight_smile:

using <li> could help “a bit” in SEO.
I am not sure about it, i read it somewhere.

@Koora

Read this:
http://www.webdesignfromscratch.com/html-tag-reference/html-unordered-list-ul-tag.php

But isn’t the information provided on that site wrong?

A list can contain just one element, no?

Tracklist:

  • song Z

That’s a list with one item. As laughable as a one-item list may look like, it is semantically correct as far as I understand it.

Not sure where you might have read that but it is drivel (shear nonsense) there would be NO SEO advantage to the list.

That being said someone here a long time ago asked me a question (in relationship to a menu)

What is a menu?

The answer is as plain as the nose on your face!

A menu is a list of links to pages on a site and that’s where is becomes semantically correct to use lists for menus, nuff said

Someone using a browser where your CSS isn’t applied would be unable to tell where one link ends and the other starts as the two run into one another. Visually there might be a slight gap in the underline but for anyone listening there is nothing to divide up the links. At the very least if you are going to have multiple <a> within the one element you need at least one character of actual text in between such as |

Marking up your list as a list is not only more semantic, it also takes care of dividing up the links so that everyone can tell where they start and finish.

@kohoutek - You bring up a good question, “can a list contain just one item (element)”. I’d argue that a list must contain more than one item, unless it isn’t a list.

Ridiculous.

I would use a <ul>/<li> combo for the extra CSS hook.

Coop, I won’t debate coding semantics with you, because I will lose :slight_smile: but I will posit that in spoken/written language semantics, a list must contain more than one item to be a list.

[highlight=“Proper English”]Cooper’s grocery list includes: butter, cheese, milk, and bread.



However, in HTML, a list can contain a single item to be functionally, and semantically, correct.

[highlight="HTML 4.01 Strict"]<ul>
<li>an item</li>
</ul>

It validates, it works on the page … I don’t see the downside.

Tell me I’m wrong if you must :slight_smile: but, please, explain why.

Validation and being semantically correct are two different things. The html validator looks for well formed pages.

I’m basing my belief off the spoken/written language - since that’s what in actuality defines the existence of a list.

W3C says - All lists must contain one or more list element.
That is the most contradicting statement i’ve ever heard.

The w3c also wraps a <p> element around an input control.

I’ve read the spec, and disagree with a few things :lol:

<rant>Don’t get me started with html5</rant> hehe

Yes, it does look ridiculous but if I take a music album comprising two discs; the first disc has 12 songs, the second disc has only one song. Were I to write that in HTML, I would write two lists, the second one containing all but a single item.

If no element can semantically define the intended content, then a neutral element should be used. Since block level elements are sitting before/after, a neutral block level element would encapsulate the one grocery item/album song.

It is absolutely contradictory. But it also means I can create a one-item list, if need be, that won’t make my code or anyone’s browser throw a hairball. :slight_smile:

You must listen to some of the [URL=“http://en.wikipedia.org/wiki/King_Crimson”]same bands [URL=“http://en.wikipedia.org/wiki/Liquid_Tension_Experiment”]I do:slight_smile:

If an album were to have one song, it wouldn’t be considered a list though. I see what you’re saying, but really think about it.

“A list of one song”

Just because W3C spec. says something doesn’t mean it’s semantically correct.
It’s like stating that a machine made by man is perfect, without hiccup :slight_smile:

Yep.

There is no such thing as an one-item list though :confused:

Just because it looks good in a browser, doesn’t mean its semantically correct.

I never said that I think it’s correct because the W3C says so.

When I write a grocery list, even if I just have one item to shop today, it’s still my grocery list. The nature of the item doesn’t change. At least I don’t think it does. It seems logical to me (which means absolutely nothing). :slight_smile:

Sorry, I was speaking out of context. When you or I write a list, we write more than one item, because if we go to the store to get just one item, we don’t need to write anything, correct?

Probably, yes but I am speaking in HTML terms. How do I markup my one-item grocery list in HTML?

Going with my previous example with the 2-disc set, what does my markup look like for the second disc containing one meager song?