问题描述
我有一个用于子菜单的 CSS 手册.我想知道如何向它添加子子菜单.例如,我将鼠标悬停在主菜单项上并弹出子菜单,然后将鼠标悬停在子菜单项上并弹出另一个子菜单.这是我现在使用的 JS Fiddle:
http://jsfiddle.net/PrinceofVegas/Dg3yQ/4/
这是我使用的 CSS:
.menu{边界:无;边框:0px;边距:0px;填充:0px;字体:67.5% Lucida Sans Unicode"、Bitstream Vera Sans"、Trebuchet Unicode MS"、Lucida Grande"、Verdana、Helvetica、sans-serif;字体大小:18px;字体粗细:粗体;}.menu ul{背景:#006633;高度:35px;列表样式:无;保证金:0;填充:0;}.menu li{向左飘浮;填充:0px;}.menu li a{背景:#006633 url("../images/seperator.gif") 右下角不重复;颜色:#ffffff;显示:块;字体粗细:正常;行高:35px;边距:0px;填充:0px 25px;文字对齐:居中;文字装饰:无;}.menu li a:hover, .menu ul li:hover a{背景:#003f20 url("../images/hover.gif") 底部中心不重复;颜色:#FFFFFF;文字装饰:无;}.menu li ul{背景:#006633;显示:无;高度:自动;填充:0px;边距:0px;边框:0px;位置:绝对;宽度:225px;z-索引:200;/*顶部:1em;/*左:0;*/}.menu li:hover ul{显示:块;}.menu li {background:url('../images/sub_sep.gif') 左下角不重复;显示:块;浮动:无;边距:0px;填充:0px;宽度:225px;}.menu li:hover li a{背景:无;}.menu li ul a{显示:块;高度:30px;字体大小:12px;字体样式:正常;边距:0px;填充:0px 10px 0px 15px;文本对齐:左;}.menu li ul a:hover, .menu li ul li:hover a{背景:#003f20 url('../images/hover_sub.gif') 居中左不重复;边框:0px;颜色:#ffffff;文字装饰:无;}.菜单p{清除:左;}
这是我正在使用的 HTML:
<ul><li><a href="#" target="_self" >Main Item 1</a></li><li><a href="#" target="_self" >主条目 2</a><ul><li><a href="#" target="_self" >测试子项</a></li><li><a href="#" target="_self" >测试子项</a></li><li><a href="#" target="_self" >测试子项</a></li><li><a href="#" target="_self" >测试子项</a></li><li><a href="#" target="_self" >主条目 3</a></li><li><a href="#" target="_self" >Main Item 4</a><ul><li><a href="#" target="_self" >测试子项</a></li><li><a href="#" target="_self" >测试子项</a></li><li><a href="#" target="_self" >测试子项</a></li>
以下是我将如何处理您正在寻找的内容:http://jsfiddle.net/Dg3yQ/26/
我在修改 CSS 方面有一些自由.修改后的 CSS 将代码减少了几百个字符,我相信它实现了您的意图.我希望这会有所帮助.
已添加了一个简化的代码示例,并对此答案进行了注释,说明如何完成这些子菜单.
#nav {列表样式:里面没有;保证金:0;填充:0;文字对齐:居中;}#导航李{显示:块;位置:相对;向左飘浮;背景:#006633;/* 菜单背景颜色 */}#nav li a {显示:块;填充:0;文字装饰:无;宽度:200px;/* 这是菜单项的宽度 */行高:35px;/* 这是菜单项的高度 */颜色:#ffffff;/* 列表项字体颜色 */}#nav li li a {font-size:80%;}/* 子菜单项的较小字体大小 */#nav li:hover {background:#003f20;}/* 悬停在子菜单上时突出显示当前悬停的列表项和父列表项 *//*--- 子列表样式 ---*/#nav ul {位置:绝对;填充:0;左:0;显示:无;/* 隐藏子列表 */}#nav li:hover ul ul {display:none;}/* 隐藏子列表 */#nav li:hover ul {display:block;}/* 在悬停时显示子列表 */#nav li li:hover ul {显示:块;/* 在悬停时显示子子列表 */左边距:200px;/* 这应该与父列表项的宽度相同 */边距顶部:-35px;/* 将子菜单的顶部与列表项的顶部对齐 */}
<li><a href="#">主条目 1</a></li><li><a href="#">主条目 2</a><ul><li><a href="#">子项目</a></li><li><a href="#">子项目</a></li><li><a href="#">SUB SUB LIST »</a><ul><li><a href="#">Sub Sub Item 1</a><li><a href="#">Sub Sub Item 2</a>
<li><a href="#">主条目 3</a></li>
I have a CSS manu that I am using with sub menus. I was wondering how I would go about adding a sub-submenu to it. For example, I hover over the main menu item and the submenu pops up, then I hover over the submenu item and another submenu pops up. Here is the JS Fiddle that I am using now:
http://jsfiddle.net/PrinceofVegas/Dg3yQ/4/
Here is the CSS I am using:
.menu{
border:none;
border:0px;
margin:0px;
padding:0px;
font: 67.5% "Lucida Sans Unicode", "Bitstream Vera Sans", "Trebuchet Unicode MS", "Lucida Grande", Verdana, Helvetica, sans-serif;
font-size:18px;
font-weight:bold;
}
.menu ul{
background:#006633;
height:35px;
list-style:none;
margin:0;
padding:0;
}
.menu li{
float:left;
padding:0px;
}
.menu li a{
background:#006633 url("../images/seperator.gif") bottom right no-repeat;
color:#ffffff;
display:block;
font-weight:normal;
line-height:35px;
margin:0px;
padding:0px 25px;
text-align:center;
text-decoration:none;
}
.menu li a:hover, .menu ul li:hover a{
background: #003f20 url("../images/hover.gif") bottom center no-repeat;
color:#FFFFFF;
text-decoration:none;
}
.menu li ul{
background:#006633;
display:none;
height:auto;
padding:0px;
margin:0px;
border:0px;
position:absolute;
width:225px;
z-index:200;
/*top:1em;
/*left:0;*/
}
.menu li:hover ul{
display:block;
}
.menu li li {
background:url('../images/sub_sep.gif') bottom left no-repeat;
display:block;
float:none;
margin:0px;
padding:0px;
width:225px;
}
.menu li:hover li a{
background:none;
}
.menu li ul a{
display:block;
height:30px;
font-size:12px;
font-style:normal;
margin:0px;
padding:0px 10px 0px 15px;
text-align:left;
}
.menu li ul a:hover, .menu li ul li:hover a{
background:#003f20 url('../images/hover_sub.gif') center left no-repeat;
border:0px;
color:#ffffff;
text-decoration:none;
}
.menu p{
clear:left;
}
Here is the HTML i am using:
<div class="menu">
<ul>
<li><a href="#" target="_self" >Main Item 1</a></li>
<li><a href="#" target="_self" >Main Item 2</a>
<ul>
<li><a href="#" target="_self" >Test Sub Item</a></li>
<li><a href="#" target="_self" >Test Sub Item</a></li>
<li><a href="#" target="_self" >Test Sub Item</a></li>
<li><a href="#" target="_self" >Test Sub Item</a></li>
</ul>
</li>
<li><a href="#" target="_self" >Main Item 3</a></li>
<li><a href="#" target="_self" >Main Item 4</a>
<ul>
<li><a href="#" target="_self" >Test Sub Item</a></li>
<li><a href="#" target="_self" >Test Sub Item</a></li>
<li><a href="#" target="_self" >Test Sub Item</a></li>
</ul>
</li>
</ul>
</div>
Here is how I would approach what you are looking for: http://jsfiddle.net/Dg3yQ/26/
I took some liberties on revising the CSS. The revised CSS reduced the code by a couple hundred characters and I believe it accomplishes what you intended. I hope this helps.
EDITED: Added a streamlined code example with comments to this answer on how these sub menus can be accomplished.
#nav {
list-style:none inside;
margin:0;
padding:0;
text-align:center;
}
#nav li {
display:block;
position:relative;
float:left;
background: #006633; /* menu background color */
}
#nav li a {
display:block;
padding:0;
text-decoration:none;
width:200px; /* this is the width of the menu items */
line-height:35px; /* this is the hieght of the menu items */
color:#ffffff; /* list item font color */
}
#nav li li a {font-size:80%;} /* smaller font size for sub menu items */
#nav li:hover {background:#003f20;} /* highlights current hovered list item and the parent list items when hovering over sub menues */
/*--- Sublist Styles ---*/
#nav ul {
position:absolute;
padding:0;
left:0;
display:none; /* hides sublists */
}
#nav li:hover ul ul {display:none;} /* hides sub-sublists */
#nav li:hover ul {display:block;} /* shows sublist on hover */
#nav li li:hover ul {
display:block; /* shows sub-sublist on hover */
margin-left:200px; /* this should be the same width as the parent list item */
margin-top:-35px; /* aligns top of sub menu with top of list item */
}
<ul id="nav">
<li><a href="#">Main Item 1</a></li>
<li><a href="#">Main Item 2</a>
<ul>
<li><a href="#">Sub Item</a></li>
<li><a href="#">Sub Item</a></li>
<li><a href="#">SUB SUB LIST »</a>
<ul>
<li><a href="#">Sub Sub Item 1</a>
<li><a href="#">Sub Sub Item 2</a>
</ul>
</li>
</ul>
</li>
<li><a href="#">Main Item 3</a></li>
</ul>
这篇关于将子子菜单添加到带有子菜单的 CSS 菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!