• google plus

How to Create a Drop-Down Menu with CSS

In this how-to, you'll learn how to create a drop-down menu with CSS.

  1. Start with the following HTML document containing an unordered list:
    
    <!DOCTYPE HTML>
    <html>
    <head>
    	<meta charset="UTF-8">
    	<title>Drop-Down Navigation Menu</title>
    </head>
    <body>
    <main>
    	<header>
    		<h1>Horizontal Navigation with Drop-Down Menu</h1>
    	</header>
    	<nav>
    		<ul id="mainMenu">
    			<li><a href="home.html">Home</a></li>
    			<li><a href="services.html">Services</a></li>
    			<li><a href="products.html">Products</a></li>
    			<li><a href="support.html">Support</a></li>
    			<li><a href="blog.html">Blog</a></li>
    			<li><a href="about.html">About</a></li>
    			<li><a href="contact.html">Contact</a></li>
    		</ul>
    	</nav>
    </main>
    </body>
    </html>
    	
    With no CSS, it will display as follows:
    An unordered list
  2. Create a file for an external styesheet and link to it from the HTML using the following tag:
    
    <link href="menu.css" rel="stylesheet">
    	
  3. Inside the stylesheet, use the following CSS to create a basic horizontal menu:
    
    ol,ul {
    	list-style: none;
    }
    #mainMenu {
    	margin:10px;
    	width:900px;
    	font-family: sans-serif;
    }
    #mainMenu li {
    	display:block;
    	width:120px;
    	float:left;
    	margin-left:2px;
    	border:1px solid #000;
    }
    
    #mainMenu a {
    	display:block;
    	padding:3px;
    	text-decoration:none;
    	background-color:#fff;
    	color:#009;
    }
    
    #mainMenu a:hover {
    	background-color:#009;
    	color:#fff;
    }
    
  4. Open the HTML page in a browser. This code renders the following:
    A Horizontal Menu
  5. In the HTML, add a nested list underneath the About list item.
    
    
    <ul id="mainMenu">
    	<li><a href="home.html">Home</a></li>
    	<li><a href="services.html">Services</a></li>
    	<li><a href="products.html">Products</a></li>
    	<li><a href="support.html">Support</a></li>
    	<li><a href="blog.html">Blog</a></li>
    	<li><a href="about.html">About</a>
    		<ul>
    			<li><a href="history.html">Company History</a></li>
    			<li><a href="staff.html">Our Staff</a></li>
    			<li><a href="press.html">Press Releases</a></li>
    			<li><a href="investorInfo.html">Investor Information</a></li>
    		</ul>
    	</li>
    	<li><a href="contact.html">Contact</a></li>
    </ul>
    
  6. In the CSS, set the position of the main menu items to relative. We will need to position the submenus using absolute positioning, but absolutely positioned elements are positioned within their nearest non-statically positioned containing element. So, to prepare for that, we'll first make the main list items relatively positioned:
    
    #mainMenu li {
    	position:relative;
    	display:block;
    	width:120px;
    	float:left;
    	margin-left:2px;
    	border:1px solid #000;
    }
    
  7. Position the submenus absolutely. Submenus are contained in ul elements within li elements. The following rule uses absolute positioning to position them immediately below those li elements:
    #mainMenu li ul {
    	position:absolute;
    	margin:0px;
    	padding:0px;
    	left:-3px;
    	top:22px;
    }
    
  8. Hide the submenus. Add display:none; to the rule above to hide the submenus.
    #mainMenu li ul {
    	display:none;
    	position:absolute;
    	margin:0px;
    	padding:0px;
    	left:-3px;
    	top:22px;
    }
    
  9. Style the submenu options. The two rules below add borders around the submenu options. We turn the top border off for all but the first option, which we identify with the :first-child pseudo-class so that we don't get a double-thick border resulting from top and bottom borders of adjacent options.
    
    #mainMenu li ul {
    	position:absolute;
    	margin:0px;
    	padding:0px;
    	left:-3px;
    	top:22px;
    	display:none;
    }
    #mainMenu li ul li {
    	width:150px;
    	font-size:smaller;
    	border-top:none;
    }
    #mainMenu li ul li:first-child {
    	border-top:1px solid #000;
    }
    
  10. Display the submenu when the user hovers over the main option. Modern browsers allow the :hover pseudo-class for almost all elements, including list items. The code below sets the display property of the submenu to block when the user hovers over the parent list item:
    #mainMenu li:hover ul {
    	display:block;
    }
    
  11. The finished CSS should look like the following:
    
    #mainMenu {
    	margin:10px;
    	width:900px;
    	font-family: sans-serif;
    }
    #mainMenu li {
    	position:relative;display:block;
    	width:120px;
    	float:left;
    	margin-left:2px;
    	border:1px solid #000;
    }
    #mainMenu a {
    	display:block;
    	padding:3px;
    	text-decoration:none;
    	background-color:#fff;
    	color:#009;
    }
    #mainMenu a:hover {
    	background-color:#009;
    	color:#fff;
    }
    #mainMenu li ul {
    	position:absolute;
    	margin:0px;
    	padding:0px;
    	left:-3px;
    	top:22px;
    	display:none;
    }
    #mainMenu li ul li {
    	width:150px;
    	font-size:smaller;
    	border-top:none;
    }
    #mainMenu li ul li:first-child {
    	border-top:1px solid #000;
    }
    #mainMenu li:hover ul {
    	display:block;
    }
    			
  12. Open the HTML page in a browser. This code renders the following:
    A Horizontal Menu with a Drop Down
Author: Chris Minnick
Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.