Everything you need as a full stack web developer

How to Create Multi-Level Navigation with `<nav>` and Lists

- Posted in HTML by

TL;DR Use the <nav> element and lists to create a multi-level navigation menu, providing semantic meaning and improving accessibility for users with disabilities. Start with a basic structure and add sub-menu items using nested unordered lists. Style the menu with CSS to make it visually appealing and enhance user experience.

How to Create Multi-Level Navigation with <nav> and Lists

As a fullstack developer, creating a well-structured navigation system is crucial for any web application or website. A good navigation menu not only enhances user experience but also helps search engines understand the hierarchy of your content. In this article, we'll explore how to create multi-level navigation using HTML's <nav> element and lists.

Why Use <nav> Element?

HTML5 introduced the <nav> element specifically for defining a section that contains navigation links. Using <nav> provides several benefits:

  1. Semantic meaning: It clearly indicates to screen readers, search engines, and other web crawlers that this section contains navigational content.
  2. Improved accessibility: By using <nav>, you're making your website more accessible for users with disabilities who rely on assistive technologies like screen readers.

Basic Navigation Structure

Before we dive into creating multi-level navigation, let's start with a basic example:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

In this example, we have a <nav> element containing an unordered list (<ul>) with three list items (<li>) each containing an anchor tag (<a>) for navigation.

Creating Multi-Level Navigation

Now that we have the basic structure in place, let's create a multi-level navigation menu. We'll add sub-menu items to our existing menu:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About Us</a>
      <ul>
        <li><a href="#">Our Team</a></li>
        <li><a href="#">Mission Statement</a></li>
      </ul>
    </li>
    <li><a href="#">Contact</a>
      <ul>
        <li><a href="#">Email Us</a></li>
        <li><a href="#">Phone Numbers</a></li>
        <li><a href="#">Address</a></li>
      </ul>
    </li>
  </ul>
</nav>

Here, we've added two sub-menus to our existing menu:

  • Under "About Us", we have a new unordered list containing two sub-menu items: "Our Team" and "Mission Statement".
  • Under "Contact", we have another unordered list with three sub-menu items: "Email Us", "Phone Numbers", and "Address".

Styling Your Navigation Menu

Now that our HTML structure is in place, let's add some basic styling to make it look more visually appealing. We'll use CSS to style our navigation menu:

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

nav li {
  position: relative;
}

nav a {
  text-decoration: none;
  color: #333;
}

nav a:hover {
  color: #666;
}

nav ul ul {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #f9f9f9;
  padding: 10px;
}

nav li:hover > ul {
  display: block;
}

This CSS code removes the default list styling, adds some basic colors and hover effects, and styles our sub-menus to appear on hover.

Conclusion

Creating a multi-level navigation menu using <nav> and lists is a fundamental skill for any web developer. By following these simple steps and adding some basic styling, you can create a well-structured and visually appealing navigation system that enhances user experience and search engine optimization. Remember to always keep accessibility in mind when building your website's navigation.

Recommended Books

Fullstack.ist offers meaningful insight into a broad range of topics. Fullstack.ist offers meaningful insight into a broad range of topics.
Backend Developer 102 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108