Pure CSS Menu Example
A pure CSS-driven dynamic menu. No javascript is required.
Steve Gibson's menu at GRC.com uses
this technique to provide a menu with common behaviors even if Javascript is disabled.
This particular example is courtesy of
CSS Play, which was cited by Steve Gibson.
CSS Play is a great resource for learning some useful style sheet techniques.
The CSS
.menu
{
border-right:1px solid #000;
font-family:arial, sans-serif;
height:32px;
width:669px;
position:relative;
z-index:100;}
/* hack to correct IE5.5 faulty box model */
* html .menu {width:670px; width:669px;}
/* remove all the bullets, borders and padding from the default list styling */
.menu ul
{
list-style-type:none;
margin:0;
padding:0;
}
.menu ul ul
{
width:130px;
}
/* float the list to make it horizontal and a relative positon so that you can control the
dropdown menu positon */
.menu li
{
float:left;
position:relative;
width:130px;
}
/* style the links for the top level */
.menu a, .menu a:visited
{
background:#09c;
border:1px solid #000;
border-width:1px 0 1px 1px;
color:#fff;
display:block;
font-size:12px;
font-weight:bold;
height:30px;
line-height:29px;
padding-left:10px;
text-decoration:none;
text-transform:uppercase;
width:138px;
}
/* a hack so that IE5.5 faulty box model is corrected */
* html .menu a, * html .menu a:visited {width:130px; width:118px;}
/* style the second level background */
.menu ul ul a.drop, .menu ul ul a.drop:visited
{
background:#d4d8bd url(http://www.cssplay.co.uk/menus/breadcrumbs/grey-arrow.gif) no-repeat 130px center;
}
/* style the second level hover */
.menu ul ul a.drop:hover
{
background:#c9ba65 url(http://www.cssplay.co.uk/menus/breadcrumbs/blue-arrow.gif) no-repeat 130px center;
}
.menu ul ul :hover > a.drop
{
background:#c9ba65 url(http://www.cssplay.co.uk/menus/breadcrumbs/blue-arrow.gif) no-repeat 130px center;
}
/* style the third level background */
.menu ul ul ul a, .menu ul ul ul a:visited
{
background:#e2dfa8;
}
/* style the third level hover */
.menu ul ul ul a:hover
{
background:#b2ab9b;
}
/* hide the sub levels and give them a positon absolute so that they take up no room */
.menu ul ul
{
visibility:hidden;
position:absolute;
height:0;
top:31px;
left:0;
width:130px;
border-top:1px solid #000;
}
/* another hack for IE5.5 */
* html .menu ul ul
{
top:30px;top:31px;
}
/* position the third level flyout menu */
.menu ul ul ul
{
left:149px;
top:-1px;
width:130px;
}
/* position the third level flyout menu for a left flyout */
.menu ul ul ul.left {left:-130px;}
/* style the table so that it takes no ppart in the layout - required for IE to work */
.menu table {position:absolute; top:0; left:0; border-collapse:collapse;}
/* style the second level links */
.menu ul ul a, .menu ul ul a:visited
{
background:#d4d8bd;
border-width:0 1px 1px 1px;
color:#000;
height:auto;
line-height:1em;
padding:5px 10px;
width:128px;
}
/* yet another hack for IE5.5 */
* html .menu ul ul a, * html .menu ul ul a:visited {width:130px;width:118px;}
/* style the top level hover */
.menu a:hover, .menu ul ul a:hover{color:#000; background:#b7d186;}
.menu :hover > a, .menu ul ul :hover > a {color:#000; background:#b7d186;}
/* make the second level visible when hover on first level list OR link */
.menu ul li:hover ul,
.menu ul a:hover ul{visibility:visible; }
/* keep the third level hidden when you hover on first level list OR link */
.menu ul :hover ul ul{visibility:hidden;}
/* make the third level visible when you hover over second level list OR link */
.menu ul :hover ul :hover ul{ visibility:visible;}
The HTML
<div class="menu">
<ul>
<li>
<a href="#">Menu 1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
<li><a class="drop" href="#">Submenu 5 >
<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Sub Submenu 1</a></li>
<li><a href="#">Sub Submenu 2</a></li>
<li><a href="#">Sub Submenu 3</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li>
<a href="#">Menu 2<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a class="drop" href="#">Submenu 1 >
<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Sub Submenu 1</a></li>
<li><a href="#">Sub Submenu 2</a></li>
<li><a href="#">Sub Submenu 3</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li>
<a href="#">Menu 3<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
<li><a class="drop" href="#">Submenu 5 >
<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Sub Submenu 1</a></li>
<li><a href="#">Sub Submenu 2</a></li>
<li><a href="#">Sub Submenu 3</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li>
<a href="#">Menu 4<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
<li><a class="drop" href="#">Submenu 5 >
<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Sub Submenu 1</a></li>
<li><a href="#">Sub Submenu 2</a></li>
<li><a href="#">Sub Submenu 3</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li>
<a href="#">Menu 5<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
<li><a class="drop" href="#">Submenu 5 >
<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">Sub Submenu 1</a></li>
<li><a href="#">Sub Submenu 2</a></li>
<li><a href="#">Sub Submenu 3</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
</ul>
</div>