Should I use <ul>s and <li>s inside my <nav>s?
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?
&开发者_Go百科lt;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?
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.
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 div
s:
<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.
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.
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.
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.
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>
.
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.
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.
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)
精彩评论