How to Create a Fly-Out Menu with CSS

See CSS: Tips and Tricks for similar articles.
Looking to improve your CSS skills? Check out Webucator’s CSS classes.

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

  1. Start with the following HTML document containing an unordered list:
    
    <!DOCTYPE HTML>
    <html>
    <head>
    	<meta charset="UTF-8">
    	<title>CSS Menu</title>
    </head>
    <body><ul id="mainMenu">
    	<li><a href="home.html">Home</a></li>
    	<li><a href="services.html">Services</a><ul>
    		<li><a href="service1.html">Service 1</a></li>
    		<li><a href="service2.html">Service 2</a></li>
    		<li><a href="service3.html">Service 3</a></li>
    	</ul></li>
    	<li><a href="products.html">Products</a><ul>
    		<li><a href="product1.html">Product 1</a></li>
    		<li><a href="product2.html">Product 2</a></li>
    		<li><a href="product3.html">Product 3</a></li>
    	</ul></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></body>
    </html>
    
  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 vertical menu:
    
    #mainMenu {
    	margin:10px;
    	width:120px;
    	font-family: "Trebuchet MS";
    }
    
    #mainMenu li {
    	display:block;
    	border:1px solid #000;
    	border-top:0px;
    }
    
    #mainMenu li:first-child {
    	border-top:1px solid #000;
    }
    
    #mainMenu a {
    	display:block;
    	padding:3px;
    	text-decoration:none;
    	color:#009;
    }
    
    #mainMenu a:hover {
    	background-color:#009;
    	color:#fff;
    }
    
  4. Open the HTML page in a browser. This code renders the following: A Vertical Menu By modifying the CSS, we can move the submenus over to the right and hide them until the user hovers over one of the parent menu items. Here are the steps:
  5. Remove the default margins and padding from the list and list items:
    ul, li {
    	margin: 0;
    	padding: 0;
    }
    
  6. Set the position of the main menu items to relative. Like with dropdown menus, we will need to position the submenus using absolute positioning:
    
    #mainMenu li {
    	position:relative;
    	/*other declarations omitted*/
    }
    
  7. Position the submenus absolutely and hide the submenus.
    
    #mainMenu li ul li {
    	font-size:smaller;
    }
    
  8. Display the submenu when the user hovers over the main option.
    #mainMenu li:hover ul {
    	display:block;
    }
    
  9. The finished CSS should look like the following:
    
    ul, li {
    	margin: 0;
    	padding: 0;
    }
    #mainMenu {
    	margin:10px;
    	width:120px;
    	font-family: "Trebuchet MS";
    }
    #mainMenu li {
    	position:relative;
    	display:block;
    	border:1px solid #000;
    	border-top:0px;
    }
    #mainMenu li:first-child {
    	border-top:1px solid #000;
    }
    #mainMenu a {
    	display:block;
    	padding:3px;
    	text-decoration:none;
    	color:#009;
    }
    #mainMenu a:hover {
    	background-color:#009;
    	color:#fff;
    }
    #mainMenu li ul {
    	position:absolute;
    	width:150px;
    	left:118px;
    	top:5px;
    	display:none;
    }
    #mainMenu li ul li {
    	font-size:smaller;
    }
    #mainMenu li:hover ul {
    	display:block;
    }
    
  10. Open the HTML page in a browser. This code renders the following: A Fly-Out Menu

Related Articles

  1. Learn the Very Basics of CSS in One Minute
  2. How to Create a CSS External Style Sheet
  3. How to Align Text with CSS
  4. How to Create a Horizontal Navigation Menu with CSS
  5. How to Create a Fixed-Width Layout with CSS
  6. How to Remove Spacing Between Table Borders with CSS
  7. How to Set a Background Image with CSS
  8. How to Set Text Spacing and Placement in CSS
  9. How to Style a Table with CSS
  10. How to Create Boxes with Rounded Corners in CSS
  11. How to Create a Vertical Navigation Menu with CSS
  12. How to Use the CSS Opacity Property
  13. How to Use Multiple Background Images with CSS
  14. Absolute Positioning with CSS
  15. How to Use the CSS Border Shorthand Property
  16. How to Create CSS Button Links
  17. How to Create a Fluid-Width Layout with CSS
  18. How to Set Text and Background Color with CSS
  19. How to Create a CSS Embedded Style Sheet
  20. How to Add Inline Styles to CSS
  21. How to Create a Border with CSS
  22. How to Use the CSS Padding Shorthand Property
  23. How to Create a Fly-Out Menu with CSS (this article)
  24. How to Use CSS Media Queries in Responsive Design
  25. How to Adjust Margins with CSS
  26. How to Use the CSS Background Shorthand Property
  27. How to Create a Form without Tables Using CSS
  28. How to Modify Fonts in CSS
  29. How to Create a Drop-Down Menu with CSS
  30. How to Apply Padding with CSS
  31. Fixed Positioning with CSS
  32. How to Use CSS Transitions
  33. How to Use the CSS list-style Shorthand Property
  34. How to Change Text Style in CSS
  35. How to Create CSS Sprites
  36. How to Use CSS with Different Media Types
  37. How to Import Style Sheets with @import in CSS
  38. How to Use the CSS White-Space Property
  39. How to Use the CSS Z-index Property
  40. How to Create Drop Shadows with the box-shadow Property in CSS3