Create Simplest Accordion Menu using jQuery

Let us create a simple Accordion Menu using jQuery. Accordion Menu are the menu with some animation effect. It has few top line menu items which when clicked toggles to open sub menu options. When another top level menu is selected, other open menu will automatically collapse and save useful screen area. We will use jQuery effects to animate the accordion menu. jQuery provides Fade In/Fade Out effect, but accordion menu looks more realistic if we use Slide In / Slide Out effect.
Related: 20 jQuery Tips & Tricks
accordion-menu-example

Step 1: Create HTML for your Menu

First we will create HTML for displaying out Menu. We will use List in HTML to render the menu and then we will use CSS to apply some style. Following will be our Menu code:

<ul id="accordion">
	<li><div>Sports</div>
		<ul>
			<li><a href="#">Golf</a></li>
			<li><a href="#">Cricket</a></li>
			<li><a href="#">Football</a></li>
		</ul>
	</li>
	<li><div>Technology</div>
		<ul>
			<li><a href="#">iPhone</a></li>
			<li><a href="#">Facebook</a></li>
			<li><a href="#">Twitter</a></li>
		</ul>
	</li>
	<li><div>Latest</div>
		<ul>
			<li><a href="#">Obama</a></li>
			<li><a href="#">Iran Election</a></li>
			<li><a href="#">Health Care</a></li>
		</ul>
	</li>
</ul>

If you check the HTML page now it should look something like:
html-menu-list

Step 2: Apply some style to your Menu using CSS

Lets apply some stylesheet to our menu. Copy following CSS code in your HTML file:

#accordion {
	list-style: none;
	padding: 0 0 0 0;
	width: 170px;
}
#accordion div {
	display: block;
	background-color: #FF9927;
	font-weight: bold;
	margin: 1px;
	cursor: pointer;
	padding: 5 5 5 7px;
	list-style: circle;
	-moz-border-radius: 10px;
	-webkit-border-radius: 10px;
	border-radius: 10px;
}
#accordion ul {
	list-style: none;
	padding: 0 0 0 0;
}
#accordion ul{
	display: none;
}
#accordion ul li {
	font-weight: normal;
	cursor: auto;
	background-color: #fff;
	padding: 0 0 0 7px;
}
#accordion a {
	text-decoration: none;
}
#accordion a:hover {
	text-decoration: underline;
}

Note that in above CSS code, we have applied Round Corner CSS to our menu to improve the look. Although this technique works with all the modern web browser, but it will not work with Internet Explorer. If we want to change the look n feel for internet explorer, we may want to include IE-Specific stylesheet to our HTML page.
Once we apply the stylesheet, our menu will look like:
accordion-menu-stylesheet

Step 3: Give life to your Menu using jQuery

So our basic skeleton is complete. We have used HTML code to display the Accordion menu content and then have applied CSS stylesheet to improve the usability. Let us add life to this accordion menu using jQuery. Copy following jQuery code to the HTML page:

$("#accordion > li > div").click(function(){

	if(false == $(this).next().is(':visible')) {
		$('#accordion ul').slideUp(300);
	}
	$(this).next().slideToggle(300);
});

$('#accordion ul:eq(0)').show();

If you notice the above code, we have made the first menu item in accordion menu visible.

$('#accordion ul:eq(0)').show();

To make a menu item visible by default, just add its index value to the above code. For example to display 1st menu item we added eq(0) in above code.

Online Demo

No article is complete without an online demo. Here is the online demo for Accordion Menu.

VIEW DEMO

DOWNLOAD



103 Comments

  • Jameel 19 May, 2012, 12:39

    Thanks, how to create one more ul inside the ul. this code support one more level and how to do ?

  • Rach 22 May, 2012, 16:10

    Thank you very much for the tutorial! It’s easy and useful.
    :D

  • mB 25 May, 2012, 2:56

    Hi there, i wonder, instead of clicking in the menu and the categorys dropdown, it is possible to rollover whit the mouse in the menu button and the subcategrys slide down automatacly?

  • Agustin 1 June, 2012, 22:27

    Thanks, how to create one more ul inside the ul. this code support one more level and how to do ?

  • david 5 June, 2012, 13:23

    Hi. Is there a way to highlight the tab which is opened, eg, when sports is clicked, it is black with orange text?

  • Tefymahery 8 June, 2012, 18:57

    the menu is not compatible with Internet Explore! it can run in IE ?

    • JimS 16 June, 2012, 3:53

      It does appear to be broken in IE v9. Not on the iframed version running on this page, but if you click on the VIEW DEMO version it is.

  • Pratheesh 23 June, 2012, 13:26

    I have checked your menu, but it is not working in internet explorer 7. Is there any solution to make it work in ie 7

    • Gerhard 5 July, 2012, 20:36

      It seems that it is quirks mode that is breaking it. You will need to force the browser out of quirks mode with the doc type. Also the rounded borders wont show in ie6-8 as it doesnt support it.

  • TD 12 July, 2012, 8:18

    How do u switch to horizontal mode? Thanks!

  • Jason Bodine 19 August, 2012, 7:20

    HELP! I’ve tried this and every single Accordion Menu tutorial I can find and always come up with the same result! The demos work fine on the website, but even when I copy and paste into my own files, it won’t work! The menus will NOT expand!

    • Viral Patel 19 August, 2012, 12:45

      Hi, Check if JQuery is properly included in your html. Also the javascript is invoked. If still doesn’t work, send me the code and I’ll have a look.

  • Patrick 19 August, 2012, 23:18

    It’s a great solution and help me so much. really thank you. Salute

  • Christiane 14 September, 2012, 20:23

    Thanks, for the nice tutorial. It helps me a lot! :-) )

  • raeyanna 14 September, 2012, 23:30

    Thank you so much!!! Days of searching, multiple cups of coffee later I have found this and can finally move forward! I am new to web design and am grateful for your tut!

  • saki 16 September, 2012, 14:41

    In your tutorial you have the “Sports”, “Technology” and “Latest” surrounded by a div.
    But then, in your running demo the divs are not there. I do need the divs but when I put them in my code then the example does not work.

  • Anu 20 September, 2012, 16:12

    Hi,
    Thanks for the nice tutorial.
    Can I gave the the submenu for an image click which is placed on right of a main category?

  • umapathy 8 October, 2012, 11:29

    Hi, thanks for ur menu tutorial.. working good.. and i hav one question.. how to write position for menu..

    if i hav 50 links in the menu.. and i click on 40th menu, position is not working.. pls help..

  • Daniel 17 October, 2012, 7:25

    great job, but seems to have problems with IE, is there any update?? to work with IE

  • HeadHunter 19 October, 2012, 18:53

    Very nice tutorial, great job…

  • peterpan 20 October, 2012, 14:42

    Thanks a lot! And thumbs up for your support on this site!

  • Andrew Goh 24 October, 2012, 11:42

    Hi, thank you for the easy to use Accordion menu. By the way, how do I create the stylesheet into a css file and refer to it. Regards, Andrew Goh

  • Rodrigo Bucketbranch 10 November, 2012, 0:12

    Hi, I’ve found this little tutorial very useful but I have an issue. When I am trying to aply the stylesheet nothing happens. I linked the stylesheet from the html file and I also tried to modify the parameters like the border color or background color, but nothing happens. Would you be kind and guide me to fix or solve this problem, please?

  • Emil Hougaard 10 November, 2012, 19:45

    Hi there and thanks for the tutorial!!

    My code looks like this, and the accordion effect works perfectly!:

    Series

    China
    Klima Knud

    Reportage

    Avantgarde world music from the toilet

    Portraits

    Raske Penge

    Due to the fact that i have a visible background in my menu and that my ‘s are wider than the ‘s they remain within, i would like to know if there is any way that the menu width can be set to adjust to the ‘s width, as long as the ‘s are hidden?

  • Emil Hougaard 10 November, 2012, 19:50

    sorry, i forgot the code field, here’s my code:

    
     <ul id="accordion">
    	<div>Series</div>
    	<ul>
    		<li>China</li>
    		<li>Klima Knud</li>
    	</ul>
    	<div>Reportage</div>
    	<ul>
    		<li>Avantgarde world music from the toilet</li>
    	</ul>
    	<div>Portraits</div>
    	<ul>
    		<li>Raske Penge</li>
    	</ul>
    </ul>
     
    • Emil Hougaard 10 November, 2012, 20:20

      Due to the fact that i have a visible background in my menu and that my div‘s are wider than the li‘s they remain within, i would like to know if there is any way that the menu width can be set to adjust to the div‘s width, as long as the li‘s are hidden?

  • DeDav 19 November, 2012, 10:03

    Thanks for a great plugin. But this is not working in IE7. I wan to make this compatable with IE 7+, Can you help me?

  • emily 20 November, 2012, 8:51

    Thank your tutorial.
    but my menu can’t expand.
    it will close very fast.
    can you help me?

    • emily 20 November, 2012, 8:57

      i refresh and it’ work.
      it’s fine :D

  • Russell Weil 29 November, 2012, 4:33

    I cannot get this to work in iE. I was reading through the thread, but not clear as to the solution. Can someone assist? Thank you.

    • Ben 12 December, 2012, 8:44

      First Thank you for the great tutorial

      It works in I.E 9 : may be upgrade your browser.
      Plus ,
      1) these are not working in IE. So, comment them and try again

            /*  -moz-border-radius: 10px;
                  -webkit-border-radius: 10px;
                  border-radius: 10px;*/
      

      2) Make sure you added the Jquery after style ( just safeguarding)
      ….

      3) write in complete format

          <script type="text/javascript">
              $(document).ready(function () {
                  $("#accordion > li > div").click(function () {
      
                      if (false == $(this).next().is(':visible')) {
                          $('#accordion ul').slideUp(300);
                      }
                      $(this).next().slideToggle(300);
                  });
                  $('#accordion ul:eq(0)').show();
      
              });
          </script>
      
  • ammar 5 December, 2012, 16:46

    Thank your tutorial

    How to get the current index value

  • Lisa 11 December, 2012, 21:22

    Is there a way to make the menu fully expand if a user has javascript disabled?

  • SANJU 5 January, 2013, 11:02

    hi…warm greetings!
    please can u let me know how to fetch the menus from the database…
    the codings are below:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript" language="javascript" src="jq files/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function()
    {
    	//slides the element with class "menu_body" when mouse is over the paragraph
    	$("#firstpane p.menu_head").click(function()
        {
    		$(this).css({backgroundImage:"url(images/down.png)"}).next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");
           	$(this).siblings().css({backgroundImage:"url(images/left.png)"});
    	});	
    });
    </script>
    <style type="text/css">
    body {
    	margin: 10px auto;
    	font: 75%/120% Verdana,Arial, Helvetica, sans-serif;
    }
    .menu_list {	
    	width: 150px;
    }
    .menu_head {
    	padding: 5px 10px;
    	cursor: pointer;
    	position: relative;
    	margin:1px;
        font-weight:bold;
        background:#6CC url(images/left.png) center right no-repeat;
    }
    .menu_body {
    	display:none;
    }
    .menu_body a{
      display:block;
      color:#006699;
      background-image:url(images/login_bg.jpg);
      padding-left:10px;
      font-weight:bold;
      text-decoration:none;
      text-align:left;
      margin-left:2px;
      }
    .menu_body a:hover{
      color: #000000;
      text-decoration:underline;
      border-bottom-style:groove;
      background-color:#FCF;
      margin:1px;
      }
     #menu1 { display : none;
     background-color:#99C;
     color:#399;
     margin-left:930px;
     padding-bottom:10px;
     }
    a:link {color:black; text-decoration:none}
    a:hover {color:blue; background-color:#3FC; text-decoration:inherit}
    </style>
    </head>
    <body>
    <center>
    <div id="container" style="width:1000px; margin-top:-10px">
    <div id="header" style="background-image:url(images/blue_bn.jpg);border:2px;border-color:#690">
    <img src="images/simple-apple.png" width="75" height="75" border="0" align="right"/><img src="images/genie.png" width="75" height="75" border="0" align="left"/><BR/>
    <BR/>
    <span class="message"> <h1 style="margin-bottom:0; color:#000"align="center">GENIIES.COM</h1>
          </span>
          <form>
          <h2 style="margin-bottom:0; color:#000" align="center"> PRODUCTION TRACKING SYSTEM
          </h2>
          <!--THE &nbsp; IS FOR THE BUTTON T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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
    <SPAN onMouseOver="document.all.menu1.style.display='block'"  onMouseOut="document.all.menu1.style.display='block'"><img src="images/settingsred.png" style="width:25px; height:25px"></SPAN><BR>
    <SPAN ID="menu1" onClick="document.all.menu1.style.display = 'none'">
    <a href="#">Privacy</a><br>
    <a href="#">Security</a><br>
    <a href="login.php">Logout</a><br>
    
    </SPAN>
        </div>
    <div id="menu" style="background-image:url(images/blue_bn.jpg);height:500px; width:200px; float:left; text-align:center;">
    <br/><br/>
    <br/><br/>
    <br/><br/>
                <center>
                 <div style="float:left;margin-left:20px">
       <div class="menu_list" id="firstpane"> 
    		<p class="menu_head"><img src="images/multimedia.png" width="29" height="28" align="absmiddle"/>&nbsp;&nbsp;&nbsp;Multimedia</p>
    		<div class="menu_body">
    		<a href="#"><img src="images/pending.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Pending</a>
             <a href="#"><img src="images/onprocess.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Onprocess</a>
             <a href="#"><img src="images/finish.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Finished</a>	
    		</div>
    		<p class="menu_head"><img src="images/copyright.png" width="29" height="28" align="absmiddle"/>&nbsp;&nbsp;&nbsp;Copyright</p>
    		<div class="menu_body">
    			<a href="#"><img src="images/pending.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Pending</a>
             <a href="#"><img src="images/onprocess.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Onprocess</a>
             <a href="#"><img src="images/finish.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Finished</a>	
    		</div>
    		<p class="menu_head"><img src="images/graphics.png" width="29" height="28" align="absmiddle"/>&nbsp;&nbsp;&nbsp;Graphics</p>
    		<div class="menu_body">
             <a href="#"><img src="images/pending.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Pending</a>
             <a href="#"><img src="images/onprocess.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Onprocess</a>
             <a href="#"><img src="images/finish.png" width="25" height="25" align="absmiddle"/>&nbsp;&nbsp;Finished</a>		
           </div>
      </div>      <!--Code for menu ends here-->
      </center>
      </div>
    <div id="content" style="background-image:url(images/globe-background-blue.jpg);height:500px;width:800px;float:right; text-align:left">
    
    </div>
    <div id="footer" style="background-image:url(images/blue_bn.jpg);clear:both;text-align:center; color:#000"><br/><br/><b> Copyright@geniies.com</b>
    <br/><br/>
    </div>
    </div>
    </center>
    </body>
    </html>
    
  • renren 7 January, 2013, 8:52

    hi!! it posible to add Active state in these accordion? reply is great help.

  • Lohith 9 January, 2013, 1:42

    I applied everything as it is but its not executing… why? my code as follows

    $(document).ready(function () {
    $(“#accordion > li”).click(function(){

    if(false == $(this).next().is(‘:visible’)) {
    $(‘#accordion > ul’).slideUp(300);
    }
    $(this).next().slideToggle(300);
    });
    $(‘#accordion > ul:eq(0)’).show();
    });

    Home

    About Us
    Price Plan
    Portfolio
    Contact Us

  • Koustav Ghosh 21 January, 2013, 12:58

    Thank you. Nice accordion menu.
    Btw I’m having a problem with this. Whenever I click on any menu item, it slides down and then it slightly slide up a little bit, like jerking. I don’t want this. How to sort out this problem?

  • abhijeet 14 February, 2013, 12:04

    Wow ! Simple yet effective. Thanks man !

  • Uttam Padukone 26 February, 2013, 6:16

    Would you have an example of multi-level displays? i.e. one menu with accordion and one of the options in this accordion having another menu, which in turn is an accordion menu with options. – i.e. 3 levels?

  • Arun kR 6 March, 2013, 10:51

    Really use full ,,,

    Thank you..

  • Albert de Vries 9 March, 2013, 17:13

    Very nice. Is there a way that after clicking on one of the URL’s from the accordion menu that in the next page the same tab is opened unfolded which contains the URL instead of the first as default.

  • kiran 20 March, 2013, 14:09

    How to add up down arrow

    • Shira 21 March, 2013, 18:40

      I will also enjoy to see that!

  • Shira 21 March, 2013, 18:39

    Thanks! really useful to beginners :)

  • Graham Jackson 5 April, 2013, 14:24

    Super example and very easy to implement, thanks very much indeed.

    I wonder if there’s something that needs to be added for the accordion to appear ‘closed’ when first loaded please? The top item always seems to be open when I load it.

  • Graham Jackson 6 April, 2013, 12:08

    Answered my own question – I removed the line

    $('#accordion > ul:eq(0)').show();

    Now works perfectly – again my thanks for this superb file which I’m using as a mobile version of my website (currently a work in progress – but the navigation now works OK with this accordion)

  • Guuz 9 April, 2013, 11:40

    Is there a way that after clicking on one of the URL’s from the accordion menu that in the next page the same tab is opened unfolded which contains the URL instead of the first as default.

  • Maavuz 20 April, 2013, 23:53

    this is Great!! thanku :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]