In this tutorial we explain how to make a dropdown menu to display a username and allow the user to access their settings, history and sign out button. We started using the reinventing a dropdown with css and jquery tutorial and modified it to our needs and to improve the overall look and feel by using CSS3 shadows and gradients.
First the HTML markup:
<dl class="dropdown"> <dt><a href=""><span>johndoe<img src="images/user.png" alt="" /></span></a></dt> <dd> <ul> <li><a href="">Edit Profile<img src="images/profile.png" alt="" /></a></li> <li><a href="">Settings<img src="images/settings.png" alt="" /></a></li> <li><a href="">History<img src="images/history.png" alt="" /></a></li> <li><a href="">Sign Out<img src="images/signout.png" alt="" /></a></li> </ul> </dd> </dl>
Next the CSS styles:
.dropdown dd, .dropdown dt, .dropdown ul { margin:0px; padding:0px; z-index: 1; } .dropdown dd { position:relative; } .dropdown a, .dropdown a:visited { color:#fff; text-decoration:none; outline:none;} .dropdown a:hover { color: #fff;} .dropdown dt a:hover { color: #fff; border: 1px solid #444; } .dropdown dt a { border-radius: 3px; -moz-border-radius: 3px; box-shadow: 0 1px 2px rgba(0,0,0,0.15); background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#666), to(#333)); /* Safari 4+, Chrome 1-9 */ background-image: -webkit-linear-gradient(top, #666, #333); /* Safari 5.1+, Mobile Safari, Chrome 10+ */ background-image: -moz-linear-gradient(top, #666, #333); /* Firefox 3.6+ */ background-image: -ms-linear-gradient(top, #666, #333); /* IE 10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#666', endColorstr='#333',GradientType=0 ); /* M$ */ background-image: -o-linear-gradient(top, #666, #333); /* Opera 11.10+ */ display: block; padding-right: 20px; border: 1px solid #444; width: 150px; } .dropdown dt a:active, .dropdown dt a.active { border-radius: 4px 4px 0 0; -moz-border-radius: 4px 4px 0 0; background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#444), to(#666)); /* Safari 4+, Chrome 1-9 */ background-image: -webkit-linear-gradient(top, #444, #666); /* Safari 5.1+, Mobile Safari, Chrome 10+ */ background-image: -moz-linear-gradient(top, #444, #666); /* Firefox 3.6+ */ background-image: -ms-linear-gradient(top, #444, #666); /* IE 10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#444', endColorstr='#666',GradientType=0 ); /* M$ */ background-image: -o-linear-gradient(top, #444, #666); /* Opera 11.10+ */ } .dropdown dt a span { background: transparent url(../images/arrow.png) no-repeat scroll 150px 5px; cursor: pointer; display: block; padding: 4px; width: 160px; line-height: 18px; } .dropdown dt a.active span { background: transparent url(../images/arrow.png) no-repeat scroll 150px -12px; } .dropdown dd ul { border-radius: 0 0 4px 4px; -moz-border-radius: 0 0 4px 4px; box-shadow: 0 1px 2px rgba(0,0,0,0.15); background: #333 none repeat scroll 0 0; border: 1px solid #666; color: #fff; display: none; left: 0px; padding: 0 0 4px; position: absolute; top: 1px; width: auto; min-width: 170px; list-style:none; } .dropdown span.value { display:none; } .dropdown dd ul li a { padding:5px; display:block; border-bottom: 1px solid #444; } .dropdown dd ul li a:hover { background-color: #444; background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#444), to(#666)); /* Safari 4+, Chrome 1-9 */ background-image: -webkit-linear-gradient(top, #444, #666); /* Safari 5.1+, Mobile Safari, Chrome 10+ */ background-image: -moz-linear-gradient(top, #444, #666); /* Firefox 3.6+ */ background-image: -ms-linear-gradient(top, #444, #666); /* IE 10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#444', endColorstr='#666',GradientType=0 ); /* M$ */ background-image: -o-linear-gradient(top, #444, #666); /* Opera 11.10+ */ } .dropdown img { border:none; vertical-align:middle; margin-right: 10px; float: left; }
And finally, the jQuery script:
$(document).ready(function() { $(".dropdown dt a").click( function(e) { $(".dropdown dd ul").toggle(); $(".dropdown dt a").toggleClass('active'); e.preventDefault(); } ); $(".dropdown dd ul li a").click(function() { $(".dropdown dd ul").hide(); }); $(document).bind('click', function(e) { var $clicked = $(e.target); if (! $clicked.parents().hasClass("dropdown")) { $(".dropdown dd ul").hide(); $(".dropdown dt a").removeClass('active'); } }); });


