Previously I wrote a tutorial on how to make a mobile navigation for responsive design, now I've discovered a new technique to produce a responsive menu without having to use Javascript. It uses clean and semantic HTML5 markup. The menu can be aligned left, center or right. Unlike the previous tutorial where it is clicked to toggle, this menu toggles on hover which is more user friendly. It also has an indicator to show the active/current menu item. It works on all mobile and desktop browsers including Internet Explorer!
The Purpose
The purpose of this tutorial is to show you how turn a regular list menu into a dropdown menu on smaller display.
This trick is more useful on navigation with a lot of links like the screenshot below. You can condense all the buttons into an elegant dropdown.
Nav HTML Markup
Here is the markup for the navigation. The <nav> tag is required to create the dropdown with the css property absolute position. I will explain this later in the tutorial. The .current class indicates the active/current menu link.
<nav class="nav">
<ul>
<li class="current"><a href="#">Portfolio</a></li>
<li><a href="#">Illustration</a></li>
<li><a href="#">Web Design</a></li>
<li><a href="#">Print Media</a></li>
<li><a href="#">Graphic Design</a></li>
</ul>
</nav>
CSS
The CSS for the navigation (desktop view) is pretty straight forward, so I'm not going to get into the details. Note that I specified display:inline-block instead of float:left for the nav <li> element. This allows the menu buttons to be able to align left, center or right by specifying text-align on the <ul> element.
/* nav */
.nav {
position: relative;
margin: 20px 0;
}
.nav ul {
margin: 0;
padding: 0;
}
.nav li {
margin: 0 5px 10px 0;
padding: 0;
list-style: none;
display: inline-block;
}
.nav a {
padding: 3px 12px;
text-decoration: none;
color: #999;
line-height: 100%;
}
.nav a:hover {
color: #000;
}
.nav .current a {
background: #999;
color: #fff;
border-radius: 5px;
}
Center and Right Alignment
As mentioned above, you can change the alignment of the buttons by using text-align property.
/* right nav */
.nav.right ul {
text-align: right;
}
/* center nav */
.nav.center ul {
text-align: center;
}
Internet Explorer Support
HTML5 <nav> tag and media query is not supported by Internet Explorer 8 or older. Include css3-mediaqueries.js (or respond.js) and html5shim.js to provide fallback support. If you don't want to add html5shim.js, replace the <nav> tag with a <div> tag.
<!--[if lt IE 9]>
<script src="http://css3-mediaqueries-js.googlecode.com/files/css3-mediaqueries.js"></script>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
Responsive
Now here comes the fun part - making the menu responsive with media query! Read my previous articles on responsive design and media query if you are not familar with responsive design.
On 600px breakpoint, I set the nav element to relative position so I can place the <ul> menu list on top with absolute position. I hide all <li> elements by specifying display:none, but keep the .current <li> displaying as block. Then on the nav hover, I set all <li> back to display:block (this will produce the dropdown list result). I added a check icon graphic on the .current element to indicate it is the active item. For the center and right alignment menu, use left and right property to position the <ul> list. View the demo to see the final result.
@media screen and (max-width: 600px) {
.nav {
position: relative;
min-height: 40px;
}
.nav ul {
width: 180px;
padding: 5px 0;
position: absolute;
top: 0;
left: 0;
border: solid 1px #aaa;
background: #fff url(images/icon-menu.png) no-repeat 10px 11px;
border-radius: 5px;
box-shadow: 0 1px 2px rgba(0,0,0,.3);
}
.nav li {
display: none; /* hide all <li> items */
margin: 0;
}
.nav .current {
display: block; /* show only current <li> item */
}
.nav a {
display: block;
padding: 5px 5px 5px 32px;
text-align: left;
}
.nav .current a {
background: none;
color: #666;
}
/* on nav hover */
.nav ul:hover {
background-image: none;
}
.nav ul:hover li {
display: block;
margin: 0 0 5px;
}
.nav ul:hover .current {
background: url(images/icon-check.png) no-repeat 10px 7px;
}
/* right nav */
.nav.right ul {
left: auto;
right: 0;
}
/* center nav */
.nav.center ul {
left: 50%;
margin-left: -90px;
}
}
This html 5 based responsive css menu works fine on Mozilla Firefox.
I wish I found it before. I have just finalised one website and spent a lot of time on something you exaplained above. Thank you for sharing, it is very cool tutorial.
Thanks for the tutorial. The one problem is that doesn’t work if below the menu have, for example, a image with a link. When click in a link in the menu, he always go to link for the image below. Anybody knows how to fix this?
Cab, if you’re still looking, try putting a z-index on the menu. I did this with the jquery version of this menu, and it worked like a charm.
Hi,
I have the same issue. The link default to the first option on mobile devise.
Where did you set the zindex? In the .nav tag?
I like this blog so much, bookmarked. “Nostalgia isn’t what it used to be.” by Peter De Vries.
Very nice menu and very good blog ;)
Usefull Post. Thanks
To all those who need it to work on ipads etc… you will need to include jquery:
http://blog.0100.tv/2010/05/fixing-the-hover-event-on-the-ipadiphoneipod/
Idea sounds great to me!! I heard about this before but did not get complete information which you have shared on your post.
Thank for explaining.
Your tutorial is very informative gathered… Thanks for sharing.
I was just looking around any tutorial this only, thanks for sharing this tutorial. It has helped me a lot is resolving the problem.
Thanks for this great tutorial. it’d definitely help me to customize the navigation menu according to client’s requirement.. :)
Thanks for this tutorial. I’m working on developing a website. For this, i need a responsive navigation menu bar. And i found it here. :)
Thanks much for this great tutorial! Very helpful for someone who is working across different browser settings. The responsive nav menu bar is definitely the way to go!
Thx for this tutorial. Very Nice. We want do create a new responsive web design for our webdesigner agency and this tutorial is very helpfully. We like your Blog.
This is a great tutorial – Just what we have been looking for. How does it perform across all browsers when more styles are applied?
This information really worth reading! Thanks!
thanks for this. its a must have for all sites these days.
Designing menus is a main task in any web page. You should have to take care of different things like; it should be completed in available space; all important menus are easily available to the user… etc. For this, you can make categories of menu and show the sub-categories in drop-down menu, procedure of making drop down is available in above article.
Hi! This works great in responsive layouts that accept :hover but, as many of other comments I’ve read here, there is the problem of touch devices.
If pressing the link takes you to a new website, there is no problem because the code will reload and the display:none of the li elements will be again activated but, what if the layout is a single page website and the li elements takes you to anchors inside the same web?
In that case the menu won’t close.
Has anyone find a solution to this? I’ve been reading everywhere but can’t figure out how to hide the menu again after pressing one of the links in a touch-device.
Nice design but as other have mentioned, does not work on mobile due to reliance on :hover which is treated the same as a click and will send the browser to the page currently selected as soon as you tap (it doesn’t in the demo as it’s only using dummy anchor links). Don’t waste your time trying this and stick to a javascript/pseudo-element solution.
works fine on my ipad with propper links..