Reputation: 20051
I want to know if it is possible to design a multilevel menu for below mentioned menu structure based on css only. On cssmenumaker.com one can see several examples and & menu with 2 - 3 level submenus they usually add a class like has-submenu
.
Can we do it with any any classes to the sub menu and manage it from css only.
something like
.nav ul {}
.nav ul ul {}
.nav ul li{}....
<ul class="nav">
<li><a href="#">Home</a>
</li>
<li><a href="#">about us</a>
</li>
<li><a href="#">News</a>
</li>
<li><a href="#">Gallery</a>
<ul>
<li><a href="#">Image Gallery</a>
</li>
<li><a href="#">Video Gallery</a>
</li>
</ul>
</li>
<li><a href="#">Contact</a>
</li>
</ul>
Example on fiddle has example based on cssmenu.com also http://jsfiddle.net/zo61z9sw/
Or we have to use any sort of jquery which can handle multilevel menus also.
Upvotes: 0
Views: 136
Reputation: 14183
In principle this works because the sub menu ul
is nested inside the li
so:
ul
to display: none;
by defaultli
is hovered set the child ul
to display: block;
. This is done via li:hover > ul
where >
will ensure only immediate children ul
to the li
will be effectedIn this case instances of .has-sub
aren't really needed as the same check can be done on all li
and will only do anything if a child ul
is found.
CSS:
/*CSS for menu without subclasses*/
#testmenu {
border-radius: 5px;
box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.15);
background: linear-gradient(to bottom, #f2edea 0%, #c0bebf 100%);
font-weight: 600;
height: 52px;
width: auto;
margin-bottom: 100px;
}
#testmenu ul, #testmenu li, #testmenu span, #testmenu a {
border: 0;
margin: 0;
padding: 0;
position: relative;
}
#testmenu:after, #testmenu ul:after {
content:'';
display: block;
clear: both;
}
#testmenu a {
box-shadow: inset 0 1px 0 whitesmoke;
background: linear-gradient(to bottom, #f2edea 0%, #c0bebf 100%);
color: #666666;
display: inline-block;
font-family: Arial, Verdana, sans-serif;
font-size: 12px;
line-height: 52px;
padding: 0 28px;
text-decoration: none;
}
#testmenu ul {
list-style: none;
box-shadow: inset 0 1px 0 whitesmoke;
}
#testmenu > ul > li {
float: left;
}
#testmenu > ul > li:first-child a {
border-radius: 5px 0 0 5px;
}
#testmenu > ul > li:hover > a {
box-shadow: inset 0 -2px 3px rgba(0, 0, 0, 0.15);
color: white;
background: linear-gradient(to bottom, #4a5662 0%, #606f7f 100%);
}
#testmenu ul > li:hover > ul {
display: block;
}
#testmenu ul > li ul {
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
display: none;
position: absolute;
width: 200px;
top: 100%;
left: 0;
}
#testmenu ul > li ul li a {
background: #606f7f;
border-bottom: 1px solid #59636f;
border-bottom: 1px solid #556371;
box-shadow: inset 0 1px 0 #606f7f;
color: white;
display: block;
line-height: 160%;
padding: 15px 10px;
font-size: 12px;
}
#testmenu ul > li ul li:hover a {
background: #4a5662;
box-shadow: inset 0 0 3px 1px rgba(0, 0, 0, 0.15);
}
#testmenu ul > li li:hover > ul {
display: block;
}
#testmenu ul > li li ul {
display: none;
position: absolute;
left: 100%;
top: 0;
}
#testmenu ul > li li ul li a {
background: #606f7f;
box-shadow: none;
}
#testmenu ul > li li ul li a:hover {
background: #4a5662;
box-shadow: inset 0 0 3px 1px rgba(0, 0, 0, 0.15);
}
I've stripped some of the vendor prefixes to shorten the code but this CSS can probably be optimised to remove/consolidate some of the styles.
JS Fiddle: http://jsfiddle.net/x9fmzc82/
Upvotes: 1