The <nav> element helps organize links for website navigation. It tells browsers and users where the main menu or navigation links are.
Nav element in HTML
Introduction
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
<nav> element.HTML
<nav> <a href="home.html">Home</a> <a href="about.html">About</a> <a href="contact.html">Contact</a> </nav>
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>
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.
Practice
1. What is the main purpose of the
<nav> element in HTML?easy
Solution
Step 1: Understand the role of <nav>
The <nav> element is specifically designed to group navigation links.Step 2: Compare with other elements
Other elements like <footer> or <section> serve different purposes, not navigation grouping.Final Answer:
To group the main navigation links on a webpage -> Option CQuick Check:
Navigation grouping = <nav> [OK]
Hint: Remember: <nav> means navigation links grouping [OK]
Common Mistakes:
- Confusing <nav> with <footer>
- Using <nav> for non-navigation content
- Thinking <nav> is for styling only
2. Which of the following is the correct syntax to create a navigation section using the
<nav> element?easy
Solution
Step 1: Check the element name
The correct HTML element for navigation is <nav>, not <navigation>.Step 2: Verify proper structure for links
Navigation links are best inside a list (<ul> with <li> and <a> tags) for semantics and accessibility.Final Answer:
<nav><ul><li><a href='#'>Home</a></li></ul></nav> -> Option AQuick Check:
Correct tag + list of links = <nav><ul><li><a href='#'>Home</a></li></ul></nav> [OK]
Hint: Use <nav> with <ul> and <li> for menus [OK]
Common Mistakes:
- Using <navigation> instead of <nav>
- Putting plain text instead of links inside <nav>
- Not using list elements for navigation links
3. What will be the visual result of this HTML code in a browser?
<nav>
<ul>
<li><a href='#home'>Home</a></li>
<li><a href='#about'>About</a></li>
</ul>
</nav>medium
Solution
Step 1: Understand default <ul> and <li> display
By default, <ul> creates a vertical list with bullet points, and <li> items stack vertically.Step 2: Check the <a> tags inside list items
The <a> tags create clickable links labeled 'Home' and 'About'.Final Answer:
A vertical list of clickable links labeled 'Home' and 'About' -> Option DQuick Check:
Default list + links = vertical clickable list [OK]
Hint: Lists inside <nav> show vertical clickable menus by default [OK]
Common Mistakes:
- Assuming <nav> changes list layout automatically
- Thinking <nav> causes errors with <ul>
- Expecting horizontal menu without CSS
4. Identify the error in this navigation code:
<nav>
<ul>
<li>Home</li>
<li><a href='#about'>About</a></li>
</ul>
</nav>medium
Solution
Step 1: Check each list item content
The first <li> contains plain text 'Home' without a clickable link.Step 2: Confirm proper link usage
Navigation items should be links (<a> tags) for accessibility and functionality.Final Answer:
The first list item 'Home' is missing a link (<a> tag) -> Option AQuick Check:
All nav items need <a> tags [OK]
Hint: Every nav item should be a link inside <li> [OK]
Common Mistakes:
- Leaving text without <a> inside <li>
- Thinking <nav> disallows <ul> or <li>
- Ignoring missing href attributes
5. You want to create a navigation menu inside a website header that is accessible and clearly identified by screen readers. Which is the best way to use the
<nav> element?hard
Solution
Step 1: Use <nav> inside <header> for main navigation
Placing <nav> inside <header> groups navigation logically.Step 2: Add
The aria-label 'Main navigation' helps screen readers identify this menu clearly.aria-labelfor accessibilityStep 3: Use list with links inside <nav>
Using <ul> and <li> with <a> tags creates a semantic, accessible menu.Final Answer:
Use <nav> with aria-label and list of links inside header -> Option BQuick Check:
Accessible nav with aria-label = <header> <nav aria-label='Main navigation'> <ul> <li><a href='/home'>Home</a></li> <li><a href='/contact'>Contact</a></li> </ul> </nav> </header> [OK]
Hint: Add aria-label to <nav> for screen reader clarity [OK]
Common Mistakes:
- Not using <a> tags for navigation items
- Omitting aria-label for accessibility
- Using plain text instead of links inside <nav>
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
