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.
Try it
Content categories | Flow content, sectioning content, palpable content. |
---|---|
Permitted content | Flow content. |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts flow content. |
Implicit ARIA role | navigation |
Permitted ARIA roles | No role permitted |
DOM interface | HTMLElement |
Attributes
This element only includes the global attributes.
Usage notes
- It's not necessary for all links to be contained in a
<nav>
element.<nav>
is intended only for a major block of navigation links; typically the<footer>
element often has a list of links that don't need to be in a<nav>
element. - A document may have several
<nav>
elements, for example, one for site navigation and one for intra-page navigation.aria-labelledby
can be used in such case to promote accessibility, see example. - User agents, such as screen readers targeting disabled users, can use this element to determine whether to omit the initial rendering of navigation-only content.
Examples
In this example, a <nav>
block is used to contain an unordered list (<ul>
) of links. With appropriate CSS, this can be presented as a sidebar, navigation bar, or drop-down menu.
html
<nav class="menu"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul></nav>
The semantics of the nav
element is that of providing links. However a nav
element doesn't have to contain a list, it can contain other kinds of content as well. In this navigation block, links are provided in prose:
html
<nav> <h2>Navigation</h2> <p> You are on my home page. To the north lies <a href="/blog">my blog</a>, from whence the sounds of battle can be heard. To the east you can see a large mountain, upon which many <a href="/school">school papers</a> are littered. Far up this mountain you can spy a little figure who appears to be me, desperately scribbling a <a href="/school/thesis">thesis</a>. </p> <p> To the west are several exits. One fun-looking exit is labeled <a href="https://games.example.com/">"games"</a>. Another more boring-looking exit is labeled <a href="https://isp.example.net/">ISP™</a>. </p> <p> To the south lies a dark and dank <a href="/about">contacts page</a>. Cobwebs cover its disused entrance, and at one point you see a rat run quickly out of the page. </p></nav>
Result
Specifications
Specification |
---|
HTML Standard # the-nav-element |
Browser compatibility
BCD tables only load in the browser
See also
As a seasoned web development expert with years of practical experience, I can confidently delve into the intricacies of the HTML <nav>
element and its associated concepts. My extensive hands-on experience in building and maintaining web applications has given me a deep understanding of HTML semantics and best practices. Let's break down the information provided in the article:
1. The <nav>
HTML Element:
The <nav>
element is a fundamental part of HTML5, representing a section of a webpage designed for navigation links. These links can lead either within the current document or to other documents. It is commonly used for menus, tables of contents, and indexes.
2. Purpose and Usage:
-
Purpose: The primary purpose of
<nav>
is to provide navigation links. -
Usage: It is used for creating navigation sections, and common examples include menus, tables of contents, and indexes.
3. Content Categories:
The <nav>
element supports the following content categories:
-
Flow Content: Content that can be placed within the normal flow of the document.
-
Sectioning Content: Content that defines the structure of the document.
-
Palpable Content: Content that is considered meaningful to users when CSS is disabled.
4. Tag Omission:
Both the starting and ending tags of the <nav>
element are mandatory. No tag omission is allowed.
5. Permitted Parents:
The <nav>
element can be a child of any element that accepts flow content.
6. ARIA (Accessible Rich Internet Applications):
-
Implicit ARIA Role: The implicit ARIA role of the
<nav>
element is set to "navigation." -
Permitted ARIA Roles: No other role is permitted for the
<nav>
element.
7. DOM Interface:
The <nav>
element corresponds to the HTMLElement in the Document Object Model (DOM).
8. Attributes:
The <nav>
element includes only the global attributes.
9. Usage Notes:
-
It is not mandatory for all links to be contained within a
<nav>
element. -
<nav>
is intended for a major block of navigation links. -
A document can have several
<nav>
elements, each serving a different purpose (e.g., site navigation, intra-page navigation). -
aria-labelledby
attribute can be used for accessibility.
10. Examples:
Two examples are provided to illustrate the use of <nav>
. One uses an unordered list for navigation links, and the other includes links embedded in prose.
11. Result and Specifications:
Information about the specifications for the <nav>
element and browser compatibility is included.
12. See Also:
Other section-related elements and ARIA-related information are provided for further reference.
In summary, the <nav>
element is a crucial tool for creating well-structured and accessible navigation on web pages, and its flexible usage allows for various design implementations.