我有一个菜单,除了子菜单外,每个菜都有效。当子菜单的<li>
为hover
时,它假设显示子菜单,并且确实如此。但问题在于子菜单的位置,它没有在<li>
下显示它left: 0%
。
http://jsfiddle.net/2fDQz/1/ - 尝试将鼠标放在“admin”和“home”上。
这里是css(尽管你可以在jsfiddle中看到它):
CSS
body {
}
/* Base Styles */
#personalbar ul, #personalbar li, #personalbar a {
list-style: none;
margin: 0;
padding: 0;
border: 0;
line-height: 1;
font-family:'Lato', sans-serif;
}
#personalbar {
border: 1px solid #123e3f;
width: auto;
}
#personalbar ul {
zoom: 1;
background: #33b3b7;
background: -moz-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #33b3b7), color-stop(100%, #288c8f));
background: -webkit-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: -o-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: -ms-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: linear-gradient(top, #33b3b7 0%, #288c8f 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='@top-color', endColorstr='@bottom-color', GradientType=0);
padding: 5px 10px;
}
#personalbar ul:before {
content:'';
display: block;
}
#personalbar ul:after {
content:'';
display: table;
clear: both;
}
#personalbar li {
float: left;
margin: 0 5px 0 0;
border: 1px solid transparent;
}
#personalbar li a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
padding: 8px 15px 9px 15px;
display: block;
text-decoration: none;
color: #ffffff;
border: 1px solid transparent;
font-size: 16px;
}
#personalbar li.active {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
border: 1px solid #33b3b7;
}
#personalbar li.active a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
display: block;
background: #1d6567;
border: 1px solid #123e3f;
-moz-box-shadow: inset 0 5px 10px #123e3f;
-webkit-box-shadow: inset 0 5px 10px #123e3f;
box-shadow: inset 0 5px 10px #123e3f;
}
#personalbar li:hover {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
border: 1px solid #33b3b7;
}
#personalbar li:hover a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
display: block;
background: #1d6567;
border: 1px solid #123e3f;
-moz-box-shadow: inset 0 5px 10px #123e3f;
-webkit-box-shadow: inset 0 5px 10px #123e3f;
box-shadow: inset 0 5px 10px #123e3f;
}
#personalbar ul ul li:hover a, #personalbar li:hover li a {
background: none;
border: none;
color: #666;
-webkit-box-shadow: none;
-moz-box-shadow: none;
}
#personalbar ul ul a:hover {
background: #7d7d7d;
color: #fff !important;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}
#personalbar li:hover > ul {
display: block;
}
#personalbar ul ul {
position:absolute;
z-index: 1000;
display: none;
margin: 0;
padding: 0;
width: 185px;
top: 40px;
left: 0;
background: #ffffff;
border: solid 1px #b4b4b4;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
#personalbar ul ul li {
position: relative;
float: none;
margin: 0;
padding: 3px;
}
HTML:
<div id='personalbar' style="position:absolute; top:0%;left:0%; width:100%;">
<ul>
<li><a href='# '><span>Home</span></a>
<ul>
<li id="Li2" runat="server"><a id="A5" href="#" runat="server"><span>bla</span></a>
</li>
<li id="Li3" runat="server"><a id="A6" href="#" runat="server"><span>bli</span></a>
</li>
</ul>
</li>
<li id="L1" runat="server"><a id="A1" href="../ClientSide/newsFeed/allEr.aspx" runat="server"><span>My Wall</span></a>
</li>
<li id="L2" runat="server"><a id="A2" href="../ClientSide/employee/eeSettings.aspx" runat="server"><span>Setting</span></a>
</li>
<li id="Li1" runat="server"><a id="A4" href="../ClientSide/employee/eeSettings.aspx" runat="server"><span>Admin</span></a>
<ul>
<li runat="server"><a href="#" runat="server"><span>bla</span></a>
</li>
<li runat="server"><a href="#" runat="server"><span>bli</span></a>
</li>
</ul>
</li>
<li id="L3" runat="server" style="position:absolute; right:1%;"><a id="A3" href="../ClientSide/Registration/registration.aspx" runat="server"><span>Sign Up</span></a>
</li>
</ul>
</div>
我尝试将#personalbar ul ul
的{{1}}更改为position
,以下是做了什么:http://jsfiddle.net/2fDQz/2/
答案 0 :(得分:4)
您需要将absolute
定位元素包含在position: relative;
容器中...
#personalbar li {
float: left;
margin: 0 5px 0 0;
position: relative;
border: 1px solid transparent;
}
请注意,正如@Amarnath指出的那样,在上面的演示中,菜单会在您悬停子元素时崩溃,这是因为您正在使用top: 40px;
所以摆脱它 - {{3 }}
#personalbar ul ul {
position:absolute;
z-index: 1000;
display: none;
margin: 0;
padding: 0;
width: 185px;
top: 40px; /* Take this out from here */
left: 0;
background: #ffffff;
border: solid 1px #b4b4b4;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
另外,想建议你,当你处理这样的标记时,最好使用>
选择器,这样你通过CSS选择的元素就是精确的..如果你不知道什么做那个选择器,它选择元素的直接子元素。例如使用
ul li ul {
/* Selects all ul element nested under li */
}
而使用类似
的东西ul > li > ul {
/* Selects direct ul element nested under li - First Level */
}
答案 1 :(得分:0)
您需要将所有li
的位置设置为relative
才能生效。制作position: relative
内容会重置任何子元素的定位值。在相对定位的父级内部使用绝对定位时,子级(例如,设置为left: 0; top: 0;
时)将自己定位到父级的左上边缘而不是文档。值得注意的一点是,z-index属性也相对于父级重置。
试试这个:
body {}
/* Base Styles */
#personalbar ul,
#personalbar li,
#personalbar a {
list-style: none;
margin: 0;
padding: 0;
border: 0;
line-height: 1;
font-family: 'Lato', sans-serif;
}
#personalbar {
border: 1px solid #123e3f;
width: auto;
}
#personalbar ul {
zoom: 1;
background: #33b3b7;
background: -moz-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #33b3b7), color-stop(100%, #288c8f));
background: -webkit-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: -o-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: -ms-linear-gradient(top, #33b3b7 0%, #288c8f 100%);
background: linear-gradient(top, #33b3b7 0%, #288c8f 100%);
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='@top-color', endColorstr='@bottom-color', GradientType=0);
padding: 5px 10px;
}
#personalbar ul:before {
content: '';
display: block;
}
#personalbar ul:after {
content: '';
display: table;
clear: both;
}
#personalbar li {
float: left;
margin: 0 5px 0 0;
border: 1px solid transparent;
position: relative; /* This is the important bit */
}
#personalbar li a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
padding: 8px 15px 9px 15px;
display: block;
text-decoration: none;
color: #ffffff;
border: 1px solid transparent;
font-size: 16px;
}
#personalbar li.active {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
border: 1px solid #33b3b7;
}
#personalbar li.active a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
display: block;
background: #1d6567;
border: 1px solid #123e3f;
-moz-box-shadow: inset 0 5px 10px #123e3f;
-webkit-box-shadow: inset 0 5px 10px #123e3f;
box-shadow: inset 0 5px 10px #123e3f;
}
#personalbar li:hover {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
border: 1px solid #33b3b7;
}
#personalbar li:hover a {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
display: block;
background: #1d6567;
border: 1px solid #123e3f;
-moz-box-shadow: inset 0 5px 10px #123e3f;
-webkit-box-shadow: inset 0 5px 10px #123e3f;
box-shadow: inset 0 5px 10px #123e3f;
}
#personalbar ul ul li:hover a,
#personalbar li:hover li a {
background: none;
border: none;
color: #666;
-webkit-box-shadow: none;
-moz-box-shadow: none;
}
#personalbar ul ul a:hover {
background: #7d7d7d;
color: #fff !important;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}
#personalbar li:hover > ul {
display: block;
}
#personalbar ul ul {
position: absolute;
z-index: 1000;
display: none;
margin: 0;
padding: 0;
top: 40px;
left: 0;
background: #ffffff;
border: solid 1px #b4b4b4;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
#personalbar ul ul li {
position: relative;
float: none;
margin: 0;
padding: 3px;
}
<div id='personalbar' style="position:absolute; top:0%;left:0%; width:100%;">
<ul>
<li><a href='# '><span>Home</span></a>
<ul>
<li id="Li2" runat="server"><a id="A5" href="#" runat="server"><span>bla</span></a>
</li>
<li id="Li3" runat="server"><a id="A6" href="#" runat="server"><span>bli</span></a>
</li>
</ul>
</li>
<li id="L1" runat="server"><a id="A1" href="../ClientSide/newsFeed/allEr.aspx" runat="server"><span>My Wall</span></a>
</li>
<li id="L2" runat="server"><a id="A2" href="../ClientSide/employee/eeSettings.aspx" runat="server"><span>Setting</span></a>
</li>
<li id="Li1" runat="server"><a id="A4" href="../ClientSide/employee/eeSettings.aspx" runat="server"><span>Admin</span></a>
<ul>
<li runat="server"><a href="#" runat="server"><span>bla</span></a>
</li>
<li runat="server"><a href="#" runat="server"><span>bli</span></a>
</li>
</ul>
</li>
<li id="L3" runat="server" style="position:absolute; right:1%;"><a id="A3" href="../ClientSide/Registration/registration.aspx" runat="server"><span>Sign Up</span></a>
</li>
</ul>
</div>