How to Create a Dropdown Navigation Bar with CSS

As a website developer, one of the most important elements on your website is the navigation bar.

It is the primary way for visitors to find their way around your website and access the information they are looking for.

One popular design choice for navigation bars is to include a dropdown menu, which can be used to organize and group together related pages on your website.

In this CSS tutorial, we will show you how to create a dropdown navigation bar using CSS.

We will be using a simple example to demonstrate the process, and provide you with the code you need to create your own dropdown navigation bar.


Step 1: HTML Markup

The first step in creating a dropdown navigation bar is to create the HTML markup for the navigation bar itself.

We will be using an unordered list to create the navigation links, and nested within that list, we will create a sub-list to hold the dropdown menu items.

Here is the HTML markup for our example:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a>
      <ul>
        <li><a href="#">Our Team</a></li>
        <li><a href="#">Our History</a></li>
      </ul>
    </li>
    <li><a href="#">Services</a>
      <ul>
        <li><a href="#">Web Design</a></li>
        <li><a href="#">SEO</a></li>
        <li><a href="#">Social Media</a></li>
      </ul>
    </li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

Step 2: CSS Styling

Once we have the HTML markup in place, we can start to add the CSS styles to create the dropdown menu.

We will start by hiding the sub-list items by setting their display property to none.

We will then use the hover pseudo-class on the parent list item to show the sub-list when the mouse is hovered over the parent list item.

Here is the CSS code for our example:

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

Step 3: Finishing Touches

We can add some additional styling to make the navigation bar look more polished.

For example, we can add a background color to the sub-list items, and change the cursor to a pointer when hovering over the parent list items.

Here is the additional CSS code for our example:

nav ul li:hover > ul {
display: block;
background: #f1f1f1;
}
nav ul li > a {
cursor: pointer;
}

And with that, you have created a functional dropdown navigation bar using CSS.

Feel free to play around with different styling options to create a navigation bar that best suits the look and feel of your website.

In conclusion, creating a dropdown navigation bar is a great way to help visitors navigate your website and find the information they are looking for.

With just a little bit of HTML and CSS, you can create a professional-looking dropdown navigation bar that will make your website look great and be easy to use.