A simple pure CSS dropdown menu with plain Html markup and pure CSS created by Phil Hoyt. Just click on the button Run Pen below to see how it works:
How to create pure CSS dropdown menu
Step 1: Add Html
<h1>Simple Pure CSS Drop Down Menu</h1>
<!-- Use nav tag as a wrapper for the dropdown menu -->
<nav id="primary_nav_wrap">
<!-- Use ul tag as a list of menu items -->
<ul>
<!--
+ Use li tag as a menu item follow conditions:
- If a menu item doesn't have submenu items,
just using anchor tag with a "href" and a text content.
- Otherwise, if a menu item has submenu items,
using ul tag again to make nested lists.
+ Next, if a menu item is active,
set class with value "current-menu-item" for it.
-->
<li class="current-menu-item"><a href="#">Home</a></li>
<li>
<a href="#">Menu 1</a>
<ul>
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
<li>
<a href="#">Sub Menu 4</a>
<ul>
<li>
<a href="#">Deep Menu 1</a>
<ul>
<li><a href="#">Sub Deep 1</a></li>
<li><a href="#">Sub Deep 2</a></li>
<li><a href="#">Sub Deep 3</a></li>
<li><a href="#">Sub Deep 4</a></li>
</ul>
</li>
<li><a href="#">Deep Menu 2</a></li>
</ul>
</li>
<li><a href="#">Sub Menu 5</a></li>
</ul>
</li>
<li>
<a href="#">Menu 2</a>
<ul>
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
</ul>
</li>
<li>
<a href="#">Menu 3</a>
<ul>
<li class="dir"><a href="#">Sub Menu 1</a></li>
<li class="dir">
<a href="#"
>Sub Menu 2 THIS IS SO LONG IT MIGHT CAUSE AN ISSEUE BUT MAYBE
NOT?</a
>
<ul>
<li><a href="#">Category 1</a></li>
<li><a href="#">Category 2</a></li>
<li><a href="#">Category 3</a></li>
<li><a href="#">Category 4</a></li>
<li><a href="#">Category 5</a></li>
</ul>
</li>
<li><a href="#">Sub Menu 3</a></li>
<li><a href="#">Sub Menu 4</a></li>
<li><a href="#">Sub Menu 5</a></li>
</ul>
</li>
<li><a href="#">Menu 4</a></li>
<li><a href="#">Menu 5</a></li>
<li><a href="#">Menu 6</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</nav>
Step 2: Add CSS
#primary_nav_wrap {
margin-top: 15px;
}
#primary_nav_wrap ul {
list-style: none; /* default value of list-style is disc */
position: relative;
float: left;
margin: 0;
padding: 0;
}
#primary_nav_wrap ul a {
display: block; /* the default value is inline-block */
color: #333;
text-decoration: none;
font-weight: 700;
font-size: 12px;
line-height: 32px;
padding: 0 15px;
font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0;
}
#primary_nav_wrap ul li.current-menu-item {
background: #ddd; /* change background color of the active menu item */
}
#primary_nav_wrap ul li:hover {
background: #f6f6f6; /* Change color of a menu item when user hovers it */
}
#primary_nav_wrap ul ul {
display: none; /* Normally, hide all nested list */
position: absolute;
top: 100%;
left: 0;
background: #fff;
padding: 0;
}
#primary_nav_wrap ul ul li {
float: none;
width: 200px;
}
#primary_nav_wrap ul ul a {
line-height: 120%;
padding: 10px 15px;
}
#primary_nav_wrap ul ul ul {
top: 0;
left: 100%;
}
#primary_nav_wrap ul li:hover > ul {
display: block; /* when user hovers a menu item, then show its nested list */
}