EN ES
Home > Web development > CSS Tutorials > How to Create Dropdown Menus with Pure HTML and CSS

How to Create Dropdown Menus with Pure HTML and CSS

Diego Cortés
Diego Cortés
September 28, 2024
How to Create Dropdown Menus with Pure HTML and CSS

Dropdown menus are a fundamental tool in modern web design. They not only enhance user navigation but also optimize space in a website's layout. In this article, we will teach you how to create dropdown menus using only HTML and CSS, providing you with a simple and effective approach.

What is a Dropdown Menu?

A dropdown menu is a type of graphical interface that allows users to access various options or submenus in a hierarchical manner. This type of menu opens when clicking or hovering over an element and closes when a selection is made or when clicking outside of it. It is a practical way to organize content in a website's navigation.

Basic Structure of a Dropdown Menu

To get started, we need to define the basic structure of our menu using HTML. Below is a simple example of how this can be done.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dropdown Menu</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <nav>
        <ul class="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">Services</a>
                <ul class="submenu">
                    <li><a href="#">Web Design</a></li>
                    <li><a href="#">SEO</a></li>
                    <li><a href="#">Digital Marketing</a></li>
                </ul>
            </li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>
</body>
</html>

In this code, we have created an unordered list (ul) that represents the main menu and a submenu within one of the list items.

Styling with CSS

Now that we have the HTML structure, it's time to style our menu using CSS. Below is how to do it:

CSS

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
}

nav {
    background-color: #000;
}

.menu {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
}

.menu > li {
    position: relative;
}

.menu > li > a {
    display: block;
    padding: 15px 20px;
    color: #ffffff;
    text-decoration: none;
}

.menu > li:hover > a {
    background-color: #444;
}

.submenu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    list-style: none;
    background-color: #333;
    padding: 0;
    margin: 0;
    z-index: 1000;
}

.submenu > li > a {
    padding: 10px 20px;
    color: #ffffff;
    text-decoration: none;
    display: block;
}

.menu > li:hover .submenu {
    display: block;
}

.submenu > li:hover > a {
    background-color: #444;
}

Explanation of CSS

  1. Basic Style: The body is configured with a readable font and light background color.
  2. Menu Style: The menu has a dark background, and the list items are set to float to the left. Links change color on hover.
  3. Submenu Style: The submenu is hidden by default (display: none;) and only shows when the user hovers over the parent item. It is positioned absolutely to appear directly below its corresponding item.

Compatibility

This approach with pure HTML and CSS is widely compatible with all modern browsers. However, if you're designing a website that needs support for older browsers, consider adding a bit of JavaScript to enhance functionality.

Conclusion

Creating a dropdown menu with pure HTML and CSS is a straightforward process that does not require external libraries or complex JavaScript. This method is not only effective but also improves the speed and loading time of the website. By following the steps above, you can design a functional menu that enhances the user experience on your website.

Now that you have the necessary knowledge, it’s time to implement your own dropdown menu and take your website's navigation to the next level!

By following these tips and examples, you will be well-equipped to create effective and attractive dropdown menus in your web projects. Good luck!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 34.87 ms