Should I use <ul>s and <li>s inside my <nav>s?
Asked Answered
G

9

149

Now that we have a dedicated <nav> tag,

Is this:

<nav>
  <ul>
    <li><a href="#foo">foo</a></li>
    <li><a href="#bar">bar</a></li>
    <li><a href="#baz">baz</a></li>
  </ul>
</nav>

any better than the following?

<nav>
  <a href="#foo">foo</a>
  <a href="#bar">bar</a>
  <a href="#baz">baz</a>
</nav>

Assuming that I don't need an extra DOM level for some CSS positioning/padding, what is the preferred way, and why?

Gaud answered 4/4, 2011 at 22:5 Comment(1)
That's a good question... With tags that doesn't make sense to HTML 4, does any of the previous best practices apply?Floriaflorian
R
82

the nav element and the list provide different semantical information:

  • The nav element communicates that we're dealing with a major navigation block

  • The list communicates that the links inside this navigation block form a list of items

At http://w3c.github.io/html/sections.html#the-nav-element you can see that a nav element could also contain prose.

So yes, having a list inside a nav element does add meaning.

Raised answered 5/4, 2011 at 9:38 Comment(4)
Thanks a lot, this is what I needed! Also, robertc's comment about screen readers announcing "list of 3 items" below was very useful.Gaud
Seems like the UL tag doesn't help anything: css-tricks.com/navigation-in-lists-to-be-or-not-to-beSmirk
The link seems to be working for me. Make sure to read the follow up, which declares lists and no lists a draw. css-tricks.com/wrapup-of-navigation-in-listsCleora
In my opinion, a <nav> without <ul><li> seems it will have more dynamic children menus. What if you have multiple menu lists which are a different type and positioning in the <nav>? I would group those menu lists as <ul><li> in the <nav>. So if your menus have regular, I would go with <ul><li>.Marquez
E
10

The cleaner markup of nav > a is certainly tempting, but consider the question of submenus and dropdown menus (something not mentioned in the other answers). HTML allows you to nest one list inside another, which is an elegant (and dare I say it, semantic) way of structuring such a menu:

<nav>
  <ul>
    <li><a href="#foo">foo</a></li>
    <li><a href="#bar">bar</a>
        <ul>
            <li><a href="#qux">qux</a></li>
            <li><a href="#quux">quux</a></li>
        </ul>
    </li>
    <li><a href="#baz">baz</a></li>
  </ul>
</nav>

You can't nest a elements, so that rules out nav > a, unless you start wrapping stuff in divs:

<nav>
  <a href="#foo">foo</a>
  <a href="#bar">bar</a>
    <div>
      <a href="#qux">qux</a>
      <a href="#quux">quux</a>
    </div>
  <a href="#baz">baz</a>
</nav>

Some popular CSS frameworks (like Bulma and Semantic/Fomantic UI) do something like this for navbars with dropdowns. So it can be done, but it feels kinda clunky to me. qux and quux aren't really nested inside of bar like they are in the first example.

Erysipelas answered 1/4, 2021 at 5:53 Comment(0)
E
3

At this point, I'd keep the <ul><li> elements, reason being that not all browsers support HTML5 tags yet.

For example, I ran into an issue using the <header> tag - Chrome and FF worked like a charm, but Opera borked.

Until all browsers support HTML completely, I'd stick them in, but rely on the old ones for backwards compatibility.

Euplastic answered 4/4, 2011 at 22:9 Comment(6)
Use the HTML 5 Shim javascript file (remysharp.com/2009/01/07/html5-enabling-script) to mitigate any possible backwards compatibility blunders with browsers such as Opera and IE.Kinlaw
But then your site isn't friendly to non-obtrusive scripting :)Euplastic
You mean like until IE8 exits the market... so like until 2016... :)Ricker
@Šime - precisely :) And turning off Javascript is no longer an option in browsers ;)Euplastic
@Agent_9191 it will - I'm completely surprised today when I wanted to just check how many people are still on IE7 and guess what - in most countries there are more people on browsers like Opera or iPad safari than in IE7. I'm so happy I can drop support for IE7 now! And IE8 will go away sooner or later. It's the last stubborn browser we will have to face (IE9 isn't that bad to code against).Pasticcio
@ŠimeVidas 2016? IE8 is presently the majority share for IE with IE10 thankfully holding second place.Steeplechase
L
3

It's up to you really. If you usually used an unordered list to markup your navigation menu, then I'd say continue to do so within the <nav> element. The point of the <nav> element is to identify the navigation of the site to a computer reader for example, so whether you use a list or simply links is immaterial.

Luncheon answered 5/4, 2011 at 8:22 Comment(1)
+1 because of mentioning that for screen readers, navs and as are just as good as lists now.Pasticcio
B
2

For me, the unordered lists are extra markup that aren't really required. When I look at an HTML document, I want it to be as clean and easy to read as possible. It's already clear to the viewer that a list is being presented if proper indentation is used. Thus, adding the UL to these a tags is unnecessary and makes for reading the document more difficult.

Although you may gain some flexibility, I believe it's a better idea to not bloat the markup with unsemantic ul classes and to style the a elements in one fell swoop. And you have no excuse: use the :before and :after pseudo-selectors.

Edit: I have been made aware that some ARIA screen readers treat lists differently than simple anchor tags. If your website is geared towards disabled people, I might consider using the list-based approach.

Budweis answered 4/4, 2011 at 22:5 Comment(0)
K
2

No, they are equivalent. Remember, HTML 5 is backwards compatible with HTML 4 lists, so you can feel free to use them in the same regard. The trade off is less code for the 2nd version.

If you are concerned about backwards compatibility with respect to browsers, make sure to include this shim to provide functionality of tags such as <nav> and <article>.

Kinlaw answered 4/4, 2011 at 22:8 Comment(2)
A list of links provides additional semantics and accessibility (eg. screen readers will announce 'List of three items' when you enter the navigation).Journal
@Journal The screenreaders point is a very good one. You should have put it on an answer! I would have marked it as correct. +1 in any case.Gaud
M
1

If we're talking "by the book", then no; you don't need to use lists to mark up your navigation. The only real advantage they offer is to provide a better degree of flexibility when styling.

Mcneil answered 4/4, 2011 at 22:8 Comment(0)
G
1

I'd keep the <ul><li> tags, because the new tags (<nav>, <section>, <article> and so on) are just more semantic versions of <div>s.

For the same reason you wouldn't just have a load of links in a <div>, they should also be structured inside a <nav> tag.

Gers answered 5/4, 2011 at 10:46 Comment(3)
The reason I used <ul>s instead of lots of links in a <div> in the past was precisely that <ul>s were more semantic. But now <nav>s are more semantic. I'm confused.Gaud
The <ul>s are still more semantic than plain <a> links, but <nav> is more semantic than <div>. The <ul> vs <a> is separate to <nav> vs <div>Gers
Not caring about semantics is not the way to go when writing HTML.Charlesettacharleston
M
1

There is a really detailed post on CSS Tricks about this exact question. It is obviously a hotly debated issue; the post has over 200 comments.

Navigation in Lists: To Be or Not To Be (CSS Tricks, Jan 2013)

Magdalenemagdalenian answered 30/11, 2013 at 23:59 Comment(1)
Here's a follow up article they wrote css-tricks.com/wrapup-of-navigation-in-listsFairtrade

© 2022 - 2024 — McMap. All rights reserved.