How To Build a Collapsible Responsive Menu for Genesis

responsive design

Updated March 3, 2013 — Thanks to several readers questioning whether or not this tutorial still works after the Genesis 1.9 release, I’ve revisited this code and realized that in fact it was no longer working properly. I’ve updated this tutorial and now it once again works with Genesis 1.91, the eleven40 theme using a custom menu in the header right widget. You can see the code in action on my Smoothie recipe website.

Most premium WordPress theme builders that offer responsive themes are lacking user-friendly navigation. Most premium WordPress theme builders simply stack the menu when their themes are viewed on smartphones.

I’m not a fan of that stacking the menu upon page load because it takes up valuable space on the screen. Forcing a user to scroll to read your article is a poor user experience.

studiopress agency theme mobileA classic case of the header and navigation taking up too much screen space can be seen on the Agency theme from StudioPress. Note: I’m a huge fan of StudioPress, but not everyone is pixel-perfect. Including me!

The Agency theme is responsive and a great skin, but I’m not a fan of the menu system. I feel that it can be improved upon.

Responsive Menu Choices

I’m seeing two trends in creating menus using responsive web designs.

Select Boxes — Many premium theme builders are changing their menu to be a select box once the screen-width decreases to a specific width. That solution is better than simply stacking your menu, but I’m not a fan.

The two images below demonstrate a responsive menu using the select box. The first image illustrates how the navigation looks when you’re not interacting with the menu. It’s unobtrusive, yet is not (in my opinion) what I think users are expecting to see when navigating a website.

The second image illustrates what happens once the user engages with the responsive menu. I don’t think this provides an optimal navigating experience.

responsive menu
responsive menu

When browsing the internet on your desktop computer, how many times have you seen the navigation of a website built using a select box?  Probably zero. On a desktop computer, users are familiar with either seeing a text-based menu system or an image-based menu system. I think that once a user is on a tablet or smart-phone, it’s not natural for them to have to think about how to navigate a website. Training them to look for a select box is silly.

Collapsible Menu — The second option I’m seeing used to save screen real estate and provide a user-friendly navigation system is to use a collapsible menu. What I’m seeing is a basic icon shape (thank god for standards) of three horizontal lines being used to indicate the presence of a menu/navigation item on the screen.

Let me show you three examples and tell me if you can spot a trend.

iPhone Reminders:

iphone reminders

Google Chrome on the iPhone:

chrome mobile browser

YouTube on the iPhone:

youtube mobile

Smart-phone and tablet users are already familiar with the three horizontal lines which indicate the presence of a menu or navigation. Why go against the flow and use a select box?

I think the three horizontal lines (responsive menu icon) is the best option and is what I’m using on my website. Go ahead and shrink your browser and watch the menu icon appear once you reach the proper size. To get this to work on my Genesis child theme took less than 10 minutes and requires zero Php coding. Ready to get started?

How To Implement a Responsive Menu in 10 Minutes

A summary of the code below: You will place a mobile responsive menu icon into your website’s navigation and initially it’s hidden. If a user of your website is viewing your site at a predefined width or smaller, your desktop menu is hidden and your mobile responsive menu icon appears. When a user clicks on the mobile menu icon, jQuery will toggle the menu to then show your full menu.

Take note of a couple things before you start.

  • My implementation and tutorial caters to those web developers using the Genesis Framework. It will work with any WordPress theme, but I’ve detailed some information below catering to Genesis. More thinking on your part is required if you’re not using Genesis.
  • The other very important thing to note is that in my example, I’m using a custom menu within the header widget. I’m pointing this out to you since your CSS ID and Class Selectors will probably differ from mine in the code below. The CSS selectors I’m referring to is: #menu-primarynavigation and .menu.

1. Place the code below into your theme’s CSS file that’s not inside of a media query.

#mobi-menu {
	display: none;
	visibility: hidden;
}

2. This next piece of CSS needs to be inside of a media query. I’ve chosen to switch to the mobile menu icon when the max-width of a browser is 400px.

@media only screen and (max-width: 400px) {
#mobi-menu { background: url("images/icon-mobile.png") no-repeat scroll 0 0 transparent; cursor: pointer; display: inline-block; 	float: right; height: 30px; margin: 12px; width: 41px; visibility: visible; }
#nav_menu-2 { display: none; }
}
/* Make sure main menu re-appears when scaled up */
@media only screen and (min-width: 401px) {
	#nav_menu-2 {
		display: block !important;
	}
}

The CSS styles mentioned above will get things working. Your homework is to style it properly for your particular implementation.

Now the jQuery…

Place the code below into your site’s head section. I’m using a Genesis child theme so I went to Genesis -> Theme Settings and placed the code into the “Header” scripts section.

<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function($){
	/* prepend menu icon */
	$('#title-area').append('<div id="mobi-menu"></div>');

	/* toggle nav */
	$("#mobi-menu").on("click", function(){
		$("#nav_menu-2").slideToggle();
		$(this).toggleClass("active");
	});
});
// ]]></script>

Grab my mobile menu icon here. Save it to your themes ‘images’ folder. If you save it elsewhere, you’ll need to adjust the code above.

Save everything and re-size your browser. BAM! You now have a responsive menu in less than 10 minutes.

As a reminder. You will have to style everything according to how you want everything displayed on your website.

Written by Rick R. Duncan

Rick is a Richmond WordPress Developer and cyclist that enjoys pedaling WordPress, SEO and Internet Marketing Tips on Twitter, Google+ and here on WPSyntax.com.

Comments

  1. Great tip. Concise & straightforward.

    I haven’t come across too many articles covering quality responsive navigation alternatives to SP’s nav issue (where I agree with you 100%).

    If you haven’t already check out this article by Brad Frost on responsive nav patterns, http://bradfrostweb.com/blog/web/responsive-nav-patterns/.

  2. Great tutorial! I am working through it. What is the #nav_menu-2 referring to? Should that be #menu-primarynavigation?

    Thanks,
    Amy

    • Hi Amy,

      #nav_menu-2 refers to the ID of my navigation menu at the top of the website. Using jQuery I’m simply pre-pending the menu-icon code to the existing menu and within my CSS it’s initially hidden from view.

      Hope that helps.
      -rick

  3. Thank you SO much for this. I can’t write jQuery (yet), but I knew this could be done, so I spent almost a week trying to cobble together a responsive menu last November. I finally got there, but not nearly so elegantly. I’m forever grateful to those of you who share your expertise. If you turned this into a plugin I bet it would be a huge hit!

    Thanks!
    Selena

    • You’re welcome Selena,

      I think that version 2 of the Genesis Framework will have this built into all of their new themes. My fingers are crossed that it will happen sooner rather than later.

      -rd

  4. Zach Superior says:

    Thanks for the tutorial Rick. I’m a little confused with your use of classes and IDs.

    In the Genesis default theme, the navigation div has an id of “#nav” and the ul of the menu has an id of #menu-primary”

    So it’s my understanding that:

    $(‘#nav_menu-2′).prepend(‘ becomes $(‘#nav’).prepend(‘

    but I’m not sure what replaces #menu-primarynavigation in $(“#menu-primarynavigation”).slideToggle();

    The ul id of #menu-primary doesn’t work and neither does #nav

    I’m having no problem getting the nav to display:none in appropriate resolutions, but #menu-icon refuses to prepend itself to #nav.

    Any ideas?

    • Hey Zach,

      I’ve updated the tutorial to cover Genesis 1.91 as something stopped working after the 1.9 release. Please try again using the updated information. If it still doesn’t work I’ll triple check the code, but it should be working now as I’ve tested and implemented it on one of my other websites which I reference in the post.

      Thanks!
      -rd

  5. Hi Rick, thanks for the fix! Works fine on my test site…

  6. Hi Rick

    It looks like an interesting script to me. However, I can’t get it work in the twentytwelve theme. Could you tell me how to add properly the given code in the last step in the head section of this theme?

    Thanks in advance,

    Ronald

    • Rick R. Duncan says:

      Hi Ronald,

      I thought the TwentyTwelve theme is already mobile responsive and provides a mobile menu icon when sized for smaller devices. Unfortunately, your request is beyond the scope of the free services provided on the blog. If however, you wish to hire us, please submit your information on our contact page.

      Thanks,
      -rd

  7. Thanks so much for this one!

    For those of you who are using Chrome as a browser, I can highly recommend this extension to check how things look at different breakpoints:

    https://chrome.google.com/webstore/detail/responsive-site-view/igfgkigklekkapmkhianeahnkfddkjbm

  8. Hi there,
    I don’t understand why you have to define:

    1 #menu-icon {
    2 display: none;
    3 visibility: hidden;
    4 }

    I don’ t see this class being used so why has it set to be hidden / not visible?

    Best
    Chris

    • Rick R. Duncan says:

      Hi Chris,

      It should read #mobi-menu. It’s hidden on page load for desktop browsers, then once it hits the proper screen resolution it will appear while making the nav_menu-2 disappear.

      -rd

Leave a Comment

Please, only use your real name, not your business name or keywords. We moderate all comments before they appear on our website and will delete and mark as spam anyone not using their real name.

*