CSS Hover Menu
Today I want to show a stylish way using CSS to create a hover drop down menu.
The HTML
<div id="navbarMain" class="navbar"> <ul> <li><a href="#">first level link</a> <ul> <li><a href="#">second level link</a> <ul> <li><a href="#">third level link</a></li> <li><a href="#">third level link</a></li> <li><a href="#">third level link</a></li> <li><a href="#">third level link</a></li> </ul> </li> <li><a href="#">second level link</a></li> <li><a href="#">second level link</a></li> <li><a href="#">second level link</a></li> <li><a href="#">second level link</a></li> <li><a href="#">second level link</a> <ul> <li><a href="#">third level link</a></li> <li><a href="#">third level link</a></li> </ul> </li> <li><a href="#">second level link</a></li> <li><a href="#">second level link</a></li> </ul> </li> <li><a href="#">first level link</a> <ul> <li><a href="#">second level link</a></li> <li><a href="#">second level link</a></li> </ul> </li> <li><a href="#">first level link</a> <ul> <li><a href="#">second level link</a></li> </ul> </li> <li><a href="#">first level link</a></li> </ul> </div><!-- /navbarMain -->
The CSS
.navbar{ position: relative; width: 100%; opacity: 0.75; } .navbar ul{ position: relative; padding: 0px; margin: 0px; } .navbar ul li{ position: relative; float: left; list-style-type: none; width: 25%; } .navbar ul li a{ text-decoration: none; display: block; height: 35px; line-height: 35px; /*centers the text vertically*/ text-align: center; border: 1px solid #111; background: #333; font-weight: bold; color: #fff !important; text-shadow: 0 /*shadow-x*/ 1px /*shadow-y*/ 1px /*shadow-radius*/ #111 /*shadow-color*/; background-image: -webkit-gradient(linear, left top, left bottom, from( #444444 /*background-start*/), to( #2d2d2d /*background-end*/)); /* Saf4+, Chrome */ background-image: -webkit-linear-gradient( #444444 /*background-start*/, #2d2d2d /*background-end*/); /* Chrome 10+, Saf5.1+ */ background-image: -moz-linear-gradient( #444444 /*background-start*/, #2d2d2d /*background-end*/); /* FF3.6 */ background-image: -ms-linear-gradient( #444444 /*background-start*/, #2d2d2d /*background-end*/); /* IE10 */ background-image: -o-linear-gradient( #444444 /*background-start*/, #2d2d2d /*background-end*/); /* Opera 11.10+ */ background-image: linear-gradient( #444444 /*background-start*/, #2d2d2d /*background-end*/); text-overflow: ellipsis; overflow: hidden; } .navbar ul li a:hover{ border: 1px solid #000; background: #444444; font-weight: bold; color: #fff !important; text-shadow: 0 /*shadow-x*/ 1px /*shadow-y*/ 1px /*shadow-radius*/ #111 /*shadow-color*/; background-image: -webkit-gradient(linear, left top, left bottom, from( #555555 /*background-start*/), to( #383838 /*background-end*/)); /* Saf4+, Chrome */ background-image: -webkit-linear-gradient( #555555 /*background-start*/, #383838 /*background-end*/); /* Chrome 10+, Saf5.1+ */ background-image: -moz-linear-gradient( #555555 /*background-start*/, #383838 /*background-end*/); /* FF3.6 */ background-image: -ms-linear-gradient( #555555 /*background-start*/, #383838 /*background-end*/); /* IE10 */ background-image: -o-linear-gradient( #555555 /*background-start*/, #383838 /*background-end*/); /* Opera 11.10+ */ background-image: linear-gradient( #555555 /*background-start*/, #383838 /*background-end*/); } .navbar ul li a:active{ border: 1px solid #000; background: #222; font-weight: bold; color: #fff !important; text-shadow: 0 /*shadow-x*/ 1px /*shadow-y*/ 1px /*shadow-radius*/ #111 /*shadow-color*/; background-image: -webkit-gradient(linear, left top, left bottom, from( #202020 /*background-start*/), to( #2c2c2c /*background-end*/)); /* Saf4+, Chrome */ background-image: -webkit-linear-gradient( #202020 /*background-start*/, #2c2c2c /*background-end*/); /* Chrome 10+, Saf5.1+ */ background-image: -moz-linear-gradient( #202020 /*background-start*/, #2c2c2c /*background-end*/); /* FF3.6 */ background-image: -ms-linear-gradient( #202020 /*background-start*/, #2c2c2c /*background-end*/); /* IE10 */ background-image: -o-linear-gradient( #202020 /*background-start*/, #2c2c2c /*background-end*/); /* Opera 11.10+ */ background-image: linear-gradient( #202020 /*background-start*/, #2c2c2c /*background-end*/); } .navbar ul li ul{ position: absolute; width: 100%; left: -99999em; } .navbar ul li:hover ul{ left: 0px; } .navbar ul li ul li, .navbar ul li ul li:hover ul li{ width: 100%; } .navbar ul li ul li ul, .navbar ul li:hover ul li ul{ position: absolute; width: 100%; left: -99999em; top: 0px; } .navbar ul li ul li:hover ul{ left: 100%; }
To see a working demo of the code follow this link.