Friday, October 18, 2013

Resposive Menu to a Dropdown for Small Screens

The Five Simple Steps website has a responsive design with a neat feature. When the browser window is narrow, the menu in the upper right converts from a regular row of links into a dropdown menu. 

 http://cdn.css-tricks.com/wp-content/uploads/2011/07/fivesimplesteps.jpg

 

When you're on a small screen (iPhone shown here) and click the dropdown, you get an interface to select an option where each option is nice and big and easy to choose.

 

That sure makes it easier to pick a place to go than a tiny link. Yeah, it's two taps instead of one, but that's arguable since you'd probably have to zoom in to tap the right link otherwise.

 

HTML Code version:

HTML code for these two menus is different. As far as I know, you can't style <select> and <option> elements to look and behave like <a>s or vice versa. So we need both. You could just put both in the markup. That's what Five Simple Steps does:



    
Let's go with that for now.


The CSS:

By default we'll hide the select menu with display: none;. This is actually good for accessibility, as it will hide the redundant menu from screen readers.
 
 


Then using media queries, we'll do the switcheroo at some specific width. You can determine that on your own (here's some standard breakpoints).

But now you gotta maintain two menus?

Well yeah, that's one concern. Maybe your menus are created dynamically and you can't control the output easily. Maybe you and hand crafting menus but want to make sure you don't accidentally get your menus out of sync. One way we can fight this is to dynamically create the dropdown menu from the original.
Using jQuery, we can do that with just a few lines of code:

Then to make the dropdown menu actually work...

But aren't dropdown menus kinda obtrusive?

Kinda. Most small screens these days are mobile and most mobile devices are JavaScript friendly, so not a huge concern. But, if you want to ensure this works with or without JavaScript I have an article about that.

Demo & Download

 VIEW DEMO    DOWNLOAD DEMO

 

TO VIEW MORE WORK CLICK HERE

 

 

 

http://css-tricks.com/convert-menu-to-dropdown/