Table of contents
1.
Introduction
2.
Definition and Usage
3.
Syntax
4.
Attributes
5.
How to Use the <nav> Tag
6.
Example 1: Basic Implementation of <nav>
7.
Example 2: Styling the <nav> Tag Using CSS
8.
Supported Browsers
9.
Frequently Asked Questions
9.1.
What is the purpose of the <nav> tag in HTML? 
9.2.
Can we use lists inside the <nav> tag? 
9.3.
Is the <nav> tag necessary for all webpages? 
10.
Conclusion
Last Updated: Dec 28, 2024
Easy

<Nav> Tag in HTML

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The <nav> tag in HTML is an essential element used for creating navigation menus on a website. It helps in structuring the links for navigation such as those leading to different sections of a webpage or other pages of a website. 

Nav Tag in HTML

In this article, you will learn what the <nav> tag is, how to use it, and how to style it using CSS. We will also discuss examples and browser compatibility to help you better understand its implementation.

Definition and Usage

The <nav> tag is an HTML5 semantic element that represents a section of a webpage containing navigation links. It is typically used to wrap the main navigation menu of a site, which often includes links to the most important pages or sections.

With the help of the <nav> tag, developers can clearly indicate to web browsers & assistive technologies that the enclosed links are part of the site's main navigation. This improves accessibility & helps search engines better understand the structure of the website.

For example: 

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


In this example, the <nav> tag wraps an unordered list (<ul>) containing navigation links. Each link is placed within a list item (<li>) & linked using the anchor (<a>) tag with the appropriate href attribute.

Syntax

Below is the basic syntax of the <nav> tag:

<nav>
  <!-- Navigation links go here -->
  <a href="#section1">Section 1</a>
  <a href="#section2">Section 2</a>
</nav>

 

  • The <nav> tag encloses anchor (<a>) tags.
     
  • Each <a> tag represents a navigation link.
     
  • You can include lists or other elements for better structure within the <nav> tag.
     

Attributes

The <nav> tag supports the global attributes that can be applied to most HTML elements. However, there are no specific attributes exclusively for the <nav> tag.


Let’s take a look at commonly used global attributes with the <nav> tag:

1. class: Specifies one or more class names for the <nav> element, which can be used to apply CSS styles or target the element with JavaScript.
 

2. id: Specifies a unique identifier for the <nav> element, which can be used to target the element with CSS or JavaScript.
 

3. style: Allows inline CSS styles to be applied directly to the <nav> element.
 

4. aria-label: Provides a descriptive label for the <nav> element, which can be useful for accessibility purposes when the navigation's purpose is not clear from the content alone.


For example: 

<nav class="main-nav" id="primary-navigation" aria-label="Main Menu">
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
    <li><a href="/services">Services</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>


In this example, the <nav> tag has a class of "main-nav", an id of "primary-navigation", and an aria-label attribute describing the navigation's purpose.

How to Use the <nav> Tag

The <nav> tag is a semantic HTML5 element that groups navigation links. It improves the accessibility and readability of your code. When a search engine or a screen reader encounters the <nav> tag, it understands that the content inside it is for navigation purposes. This helps enhance the user experience and SEO performance of your website.

Key Points to Remember:

  • It should only include major navigational links, not all links on the webpage.
     
  • Commonly used for menus, table-of-contents links, or other navigational aids.

Example 1: Basic Implementation of <nav>

Let’s create a simple navigation menu:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Basic Nav Example</title>
</head>
<body>
  <header>
    <h1>Website Header</h1>
  </header>
  <nav>
    <a href="#home">Home</a> |
    <a href="#about">About</a> |
    <a href="#services">Services</a> |
    <a href="#contact">Contact</a>
  </nav>

  <section id="home">
    <h2>Home Section</h2>
    <p>Welcome to the home page.</p>
  </section>

  <section id="about">
    <h2>About Section</h2>
    <p>Learn more about us here.</p>
  </section>
  <section id="services">
    <h2>Services Section</h2>
    <p>Details about services offered.</p>
  </section>


  <section id="contact">
    <h2>Contact Section</h2>
    <p>Get in touch with us.</p>
  </section>
</body>
</html>


Output

Output

 This code creates a horizontal navigation menu with links leading to different sections of the page: Home, About, Services, and Contact. Clicking a link takes you to the respective section.

Example 2: Styling the <nav> Tag Using CSS

To make the navigation menu visually appealing, we can style it with CSS:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Styled Nav Example</title>
  <style>
    nav {
      background-color: #333;
      padding: 10px;
    }

    nav a {
      color: white;
      text-decoration: none;
      margin: 0 15px;
      font-size: 18px;
    }


    nav a:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>
  <header>
    <h1>Website Header</h1>
  </header>

  <nav>
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#services">Services</a>
    <a href="#contact">Contact</a>
  </nav>

  <section id="home">
    <h2>Home Section</h2>
    <p>Welcome to the home page.</p>
  </section>

  <section id="about">
    <h2>About Section</h2>
    <p>Learn more about us here.</p>
  </section>

  <section id="services">
    <h2>Services Section</h2>
    <p>Details about services offered.</p>
  </section>

  <section id="contact">
    <h2>Contact Section</h2>
    <p>Get in touch with us.</p>
  </section>
</body>
</html>


Output

 

Output
  • The nav background is styled with a dark color (#333) and some padding.
     
  • Links (<a> tags) are styled with white color and a hover effect for a professional look.
     
  • The navigation now appears as a clean, styled menu that enhances the user experience.

Supported Browsers

The <nav> tag is supported by all major modern browsers. Here is the compatibility list:

BrowserSupport
Google ChromeYes
Mozilla FirefoxYes
Microsoft EdgeYes
SafariYes
OperaYes


This universal support ensures that your navigation menus will work smoothly across devices and platforms.

Frequently Asked Questions

What is the purpose of the <nav> tag in HTML? 

The <nav> tag is used to define navigation links in a webpage, making it easier for users to navigate between sections or pages.

Can we use lists inside the <nav> tag? 

Yes, you can use ordered (<ol>) or unordered (<ul>) lists inside the <nav> tag to organize navigation links better.

Is the <nav> tag necessary for all webpages? 

While not mandatory, using the <nav> tag is a good practice for structuring navigation links in a semantic and accessible manner.

Conclusion

The <nav> tag is a useful tag for creating structured and accessible navigation menus in HTML. By understanding its syntax and implementing it with examples, you can create professional-looking navigation bars that enhance user experience. Using CSS, you can further style the <nav> tag to align with the design of your website. With its wide browser support and ease of use, the <nav> tag is indispensable for modern web development.

You can also check out our other blogs on Code360.

Live masterclass