HTML Nav Tag: How to Create Navigation Menus

html nav tag

The nav tag defines a navigation block in HTML. It holds main links to other parts of your site. Search engines detect it as primary navigation.

The <nav> tag used for menus and key link groups only. HTML5 introduced it to add clear structure.

Basic Syntax of HTML <nav> Tag

The syntax looks like this:

<nav>
  <!-- Your links here -->
</nav>

You must start with <nav> and end with </nav>. Place <a> elements inside the container. Wrap them in <ul> and <li> tags to build menus. Take this example:

<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>

This structure avoids markup errors.

Use <nav> for main site navigation. Place it around key internal links. Text-to-speech tools use it to jump to links, while search engines see it as your site map.

Never wrap the footer or unrelated links in <nav>. You can use more than one <nav> if your site has separate sections.

Here are the important points when you use the nav tag in HTML:

  • Add an aria-label if you have more than one <nav>. This describes the purpose of each navigation area and uses link text.
  • Place headings near <nav> to give context.
  • Add skip links to jump past navigation.
  • Test the <nav> with a screen reader to confirm it works.

Styles of HTML <nav> Tag with CSS

The <nav> tag has no built-in style, and you need to add CSS to create a layout. Use Flexbox or Grid for alignment.

Here’s how to center links in a horizontal bar:

nav ul {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
}

nav li {
  margin-right: 20px;
}

You can highlight the active link with a class. Make sure your styles adapt to all screen sizes.

Examples of HTML <nav> Tag

Simple Site Navigation:

<nav>
  <a href="/">Home</a>
  <a href="/about">About</a>
  <a href="/contact">Contact</a>
</nav>

This example shows a basic navigation bar with links to main pages.

Navigation with List Items:

<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/blog">Blog</a></li>
    <li><a href="/services">Services</a></li>
  </ul>
</nav>

This structure uses lists to organize links.

Navigation with Logo:

<nav>
  <a href="/"><img src="logo.png" alt="Site Logo"></a>
  <a href="/products">Products</a>
  <a href="/cart">Cart</a>
</nav>

The logo acts as a link to the homepage. It combines brand and navigation in one clear section.

Nested Navigation for Sections:

<nav>
  <a href="/">Home</a>
  <div>
    <a href="/products">Products</a>
    <a href="/products/new">New Arrivals</a>
  </div>
</nav>

This shows nested links under a main section. It organizes related pages clearly so users see their options.

Navigation with ARIA Label:

<nav aria-label="Main Navigation">
  <a href="/">Home</a>
  <a href="/profile">Profile</a>
  <a href="/settings">Settings</a>
</nav>

This example uses an ARIA label to help text-to-speech tools. It makes the navigation clear and improves usability for everyone.

Wrapping Up

In this article you learned what the <nav> tag does and how it marks your main site navigation. You also saw how to use syntax and structure.

Here is a quick recap:

  • The <nav> tag is the key to site links and defines the main navigation.
  • It signals primary navigation to search engines for better SEO.
  • Use clear internal links and avoid unrelated or footer links inside <nav>.
  • Add CSS to style and arrange links in a responsive layout.
  • Include ARIA labels if you use more than one
    to help text-to-speech tools
  • Test your navigation on different devices and with screen readers to ensure access for everyone.

What is the purpose of the HTML tag?

The <nav> tag holds main links for a page. Use it for menus, table of contents, or site sections. It helps browsers and screen readers find key links.

Is the HTML tag part of HTML5?

Yes. HTML5 added the <nav> tag as a semantic element. It shows the section is for navigation and supports screen readers and search engines.

Where should you use the HTML tag?

Use <nav> for main menus, sidebars, or in-page links. Do not use it for small link sets like social icons or tag lists.

Do you need role="navigation" on the tag?

No. <nav> already means navigation. The role is built-in, so you do not need to add it again.

Where does the tag go inside a webpage?

Place it inside <body>, often in <header>, <aside>, or near content. Always open and close it properly to keep valid structure.

How do you write navigation with the tag?

Use <nav> to wrap links or a list of links. You can write them directly or place them in a <ul> and <li> structure.
<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>
If you use more than one <nav> on a page, add an aria-label to describe the section.

Similar Reads

HTML Data Attributes with Examples

data-* attributes store extra values on elements without an extra DOM nodes or backend requests. What are data-* attributes in…

HTML Heading Elements: Understanding h1 to h6 with Examples

The heading elements define a content structure in HTML. Use <h1> to <h6> to show levels. <h1> means the main…

HTML Link Attributes Guide for Beginners

HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…

HTML Document Structure: How to Create DOCTYPE in HTML

The DOCTYPE ensures that your HTML is readable by browsers and ensures that pages follow modern standards. Understand the DOCTYPE…

HTML p Tag: How to Create a Paragraph in HTML

Paragraphs help group-related text. They build clear, readable pages. You use them to split ideas or topics. The HTML p…

HTML Compatibility: Old vs New Browsers

HTML code does not always show the same result on every browser. Old browsers follow outdated rules while modern browsers…

HTML abbr Tag: How Abbreviations Work with Examples

The abbr tag refers to an abbreviation in HTML. It helps you save space and improve accessibility. The screen readers…

HTML id Attribute: How to Assign Unique Identifiers to Elements

The id attribute adds a unique name to an HTML element. It helps with CSS and JavaScript in the page.…

Comments in HTML: How They Work with Examples

Comments in HTML help you explain your code. You can add notes without showing anything in the browser. Understand How…

HTML vs XHTML: Key Differences Every Developer Must Know

HTML and XHTML describe web pages. You must know the differences to write code that works across browsers. This article…

Previous Article

HTML strong Tag: How to Give Text Strong Importance

Next Article

HTML Aside Tag: How to Create a Side Content

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.