0
0
HTMLmarkup~5 mins

Nav element in HTML

Choose your learning style9 modes available
Introduction

The <nav> element helps organize links for website navigation. It tells browsers and users where the main menu or navigation links are.

When you want to group the main menu links of a website.
To separate navigation links from other page content for clarity.
When creating a header or sidebar with links to different pages.
To improve accessibility by helping screen readers find navigation easily.
When building a footer with important site links.
Syntax
HTML
<nav>
  <!-- navigation links here -->
</nav>
Use <nav> only for major navigation blocks, not for all links.
Place links inside <nav> to group them logically.
Examples
A simple navigation bar with three links inside the <nav> element.
HTML
<nav>
  <a href="home.html">Home</a>
  <a href="about.html">About</a>
  <a href="contact.html">Contact</a>
</nav>
Navigation inside a header with an accessible label and a list of links.
HTML
<header>
  <nav aria-label="Main navigation">
    <ul>
      <li><a href="#">Services</a></li>
      <li><a href="#">Portfolio</a></li>
      <li><a href="#">Blog</a></li>
    </ul>
  </nav>
</header>
Sample Program

This example shows a navigation bar inside a header. The links have good color contrast and keyboard focus styles for accessibility.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Nav Element Example</title>
  <style>
    nav {
      background-color: #004080;
      padding: 1rem;
    }
    nav a {
      color: white;
      margin-right: 1.5rem;
      text-decoration: none;
      font-weight: bold;
    }
    nav a:hover, nav a:focus {
      text-decoration: underline;
      outline: none;
    }
  </style>
</head>
<body>
  <header>
    <nav aria-label="Primary navigation">
      <a href="#home">Home</a>
      <a href="#services">Services</a>
      <a href="#contact">Contact</a>
    </nav>
  </header>
  <main>
    <h1>Welcome to Our Website</h1>
    <p>Use the navigation links above to explore.</p>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Always add aria-label to <nav> if there are multiple navigation sections to help screen readers.

Use semantic HTML like <nav> to improve SEO and accessibility.

Keep navigation links clear and easy to find for all users.

Summary

The <nav> element groups main navigation links on a page.

It improves accessibility by helping screen readers identify navigation areas.

Use it inside headers, footers, or sidebars to organize menus clearly.