There seems to be conflicting examples in documents relating to the <nav>
tag in html5. Most examples I've seen use this:
<nav>
<ul>
<li><a href='#'>Link</a></li>
<li><a href='#'>Link</a></li>
<li><a href='#'>Link</a></li>
</ul>
</nav>
But I'm wondering if that's only because people are used to using divs. There are examples that I've seen that simply do this
<nav>
<a href='#'>Link</a>
<a href='#'>Link</a>
<a href='#'>Link</a>
</nav>
The second way seems cleaner and more semantic to me. Is there an "official" correct version? Is there a good reason to still use a <ul>
inside the nav tag rather than just use anchor elements directly?
The nav element represents a section of a page that links to other pages or to parts within the page: a section with navigation links. Not all groups of links on a page need to be in a nav element — the element is primarily intended for sections that consist of major navigation blocks.
The <nav> HTML element represents a section of a page whose purpose is to provide navigation links, either within the current document or to other documents. Common examples of navigation sections are menus, tables of contents, and indexes.
Common examples of the nav elements are menus, tables, contents, and indexes. The nav tag is reserved for primary navigation areas, like the main menu across the top of the page or section. A document may have several nav elements, for example, site navigation and one for intra-page navigation.
Use <h4> tag to add the heading for the navigation bar. Use <ul> tag to define unordered (unnumbered) lists. Use <li> tags within the <ul> tag to add list items to the unordered list. Use <a> tag to add a hyperlink to any content on the web page.
Both examples are semantic.
In the first example, the list of anchors is explicitly an unordered list. In the second example, the list of links is just a collection of anchor elements.
If you simply want a one-dimensional collection of links, I recommend sticking with
<nav>
<a href='#'>Link</a>
<a href='#'>Link</a>
<a href='#'>Link</a>
</nav>
however, using ul
elements allows for explicit hierarchical menus (such as drop-downs or tree-lists):
<nav>
<ul>
<li>
<a href="#">Link</a>
</li>
<li>
<a href="#">Link</a>
<ul>
<li>
<a href="#">Sub link</a>
</li>
<li>
<a href="#">Sub link</a>
</li>
<li>
<a href="#">Sub link</a>
</li>
</ul>
</li>
<li>
<a href="#">Link</a>
</li>
<li>
<a href="#">Link</a>
<ul>
<li>
<a href="#">Sub link</a>
</li>
<li>
<a href="#">Sub link</a>
</li>
</ul>
</li>
</ul>
</nav>
The only real difference might be how search engines might look at the links. When in an unordered list Google might for example understand that all the items in that list are related. In the second example Google might just as well assume none of the links are related. Google can use that information for indexing and display your information more accurately.
They may display the same, but really a lot of markup is about how information should be presented if there was no style attached to the page at all or if a bot is searching your website for relevant information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With