Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 nav tag content

Tags:

html

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?

like image 582
Falmarri Avatar asked Sep 01 '11 22:09

Falmarri


People also ask

What goes inside a nav element?

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.

What is HTML5 nav tag?

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.

What should be in a NAV HTML?

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.

How do I add navigation to content in HTML?

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.


2 Answers

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>
like image 83
zzzzBov Avatar answered Sep 27 '22 23:09

zzzzBov


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.

like image 30
Caimen Avatar answered Sep 27 '22 21:09

Caimen