How to Create Dropdown Navigation with jQuery and CSS

In this tutorial, we are going to learn how to create Dropdown Navigation with jQuery and CSS. This simple work can help you to build a beautiful yet powerful drop down navigation menu in your created page. This work built using nested HTML lists and a help of the jQuery’s plugin like slide down and slide up animations and using CSS. It will minimize your menu list in the navigation using the dropdown function.

Let’s work it:

1. We are going to create our simple Markup nested navigation list menu for the dropdown navigation of your page.
  1. <nav>
  2.   <div class="container">
  3.     <ul>
  4.       <li><a href="#">Home</a></li>
  5.       <li><a href="#">Free Tutorials</a></li>
  6.       <li> <a href="#">Programming<i class='fa fa-angle-down'></i></a>
  7.         <ul>
  8.           <li><a href="#">PHP</a></li>
  9.           <li><a href="#">HTML/CSS</a></li>
  10.           <li><a href="#">JAVA</a></li>
  11.           <li><a href="#">JavaScript</a></li>
  12.         </ul>
  13.       </li>
  14.       <li class='sub-menu'> <a href="#">Others<i class='fa fa-angle-down'></i></a>
  15.         <ul>
  16.           <li><a href="#">Submit Code</a></li>
  17.           <li><a href="#">Register User</a></li>
  18.           <li><a href="#">FAQ</a></li>
  19.         </ul>
  20.       </li>
  21.       <li><a href="#">Contact Us</a></li>
  22.     </ul>
  23.   </div>
  24. </nav>
2. After that, we required constructing the CSS style for our navigation styles. Note: You can customize your navigation menu style whatever your desired design by changing the CSS later.
  1. <style>
  2. a { text-decoration: none; }
  3.  
  4. .container {
  5.   width: 1000px;
  6.   margin: auto;
  7. }
  8.  
  9. h1 { text-align:center; margin-top:150px;}
  10.  
  11. /* Navigation Styles */
  12.  
  13. nav { background: -webkit-linear-gradient(#585858,#3d3d3d); }
  14.  
  15. nav ul {
  16.   font-size: 0;
  17.   margin: 0;
  18.   padding: 0;
  19. }
  20.  
  21. nav ul li {
  22.   display: inline-block;
  23.   position: relative;
  24. }
  25.  
  26. nav ul li a {
  27.   color: #fff;
  28.   display: block;
  29.   font-size: 14px;
  30.   padding: 15px 14px;
  31.   transition: 0.3s linear;
  32. }
  33.  
  34. nav ul li:hover { background: #126d9b; }
  35.  
  36. nav ul li ul {
  37.   border-bottom: 5px solid #2ba0db;
  38.   display: none;
  39.   position: absolute;
  40.   width: 250px;
  41. }
  42.  
  43. nav ul li ul li {
  44.   border-top: 1px solid #444;
  45.   display: block;
  46. }
  47.  
  48. nav ul li ul li:first-child { border-top: none; }
  49.  
  50. nav ul li ul li a {
  51.   background: #373737;
  52.   display: block;
  53.   padding: 10px 14px;
  54. }
  55.  
  56. nav ul li ul li a:hover { background: #126d9b; }
  57.  
  58. nav .fa.fa-angle-down { margin-left: 6px; }
  59. </style>
3. Kindly include this jQuery library on the page to completely have a drop-down navigation on your page.
  1. <script src="jquery-1.12.4.min.js"></script>
4. Lastly, this simple script will help us to have a simple sliding effect in our drop-down navigation on our page.
  1. <script>
  2. $('nav li').hover(
  3.   function() {
  4.           $('ul', this).stop().slideDown(200);
  5.   },
  6.         function() {
  7.     $('ul', this).stop().slideUp(200);
  8.   }
  9. );
  10. </script>

Output


Result
Note: You can customize your menu whatever your desired design by changing the CSS. That's it, kindly click the "Download Code" button below for the full source code. And try it to customize your own Dropdown Navigation. Enjoy coding. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment