如何在 CSS3 下拉菜单中使用(顶部)边距?
我正在尝试完全用 CSS3 创建一个下拉菜单。在大多数情况下,我已经完成了我想要的,但由于某种原因,我似乎无法添加 margin-top: 10px;
到下拉菜单,因为我不希望它接触主菜单关联。当悬停到达边缘时似乎会停用它?
我在 jsFiddle 上发布了一个没有边距的工作示例: http://jsfiddle.net/J5QSv/
这是使用 margin-top: 10px;
,并且不起作用:http://jsfiddle.net/RastaLulz/J5QSv/2/
如您所见,效果非常好。但是,当您取消注释 margin-top: 10px;
时,它不再起作用。
你知道解决这个问题的方法吗?或者解决方法?
HTMLCSS
<span class="LinksMenu">
<ul>
<li>
<a href="#">Account</a>
<ul>
<li><a href="#">Settings</a></li>
<li><a href="#">Logout</a></li>
</ul>
</li>
</ul>
</span>
body {
padding: 10px;
background: #F3F3F3;
}
a:link,a:visited { color:#000; text-decoration:none }
a:hover,a:active { color:#000; text-decoration:underline }
.LinksMenu {
margin:0;
padding:0;
clear: both;
}
.LinksMenu ul {
margin:0;
padding:0;
}
.LinksMenu li {
margin:0;
padding:0;
list-style:none;
float: left;
position: relative;
}
.LinksMenu ul ul li a {
margin: 0;
padding: 10px;
width: 100px;
display: block;
text-shadow: 0;
color: #000;
}
.LinksMenu ul ul {
/* margin-top: 3px; */
border: 1px solid #CCC;
border-radius: 3px;
background: #FFF;
position: absolute;
visibility: hidden;
}
.LinksMenu ul li:hover ul {
visibility: visible;
}
I am trying to create a drop down menu completely in CSS3. For the most part I have accomplished what I wanted, but for some reason I can't seem to add margin-top: 10px;
to drop down menu, as I don't want it touching the main link. It seems to deactivate the hover when it hits the margin?
I have posted a working example with no margin on jsFiddle: http://jsfiddle.net/J5QSv/
This is with the margin-top: 10px;
, and does not work: http://jsfiddle.net/RastaLulz/J5QSv/2/
As you can see, that works perfectly fine. However, when you uncomment margin-top: 10px;
it no longer works.
Do you know a way to fix this? or a work around?
HTML
<span class="LinksMenu">
<ul>
<li>
<a href="#">Account</a>
<ul>
<li><a href="#">Settings</a></li>
<li><a href="#">Logout</a></li>
</ul>
</li>
</ul>
</span>
CSS
body {
padding: 10px;
background: #F3F3F3;
}
a:link,a:visited { color:#000; text-decoration:none }
a:hover,a:active { color:#000; text-decoration:underline }
.LinksMenu {
margin:0;
padding:0;
clear: both;
}
.LinksMenu ul {
margin:0;
padding:0;
}
.LinksMenu li {
margin:0;
padding:0;
list-style:none;
float: left;
position: relative;
}
.LinksMenu ul ul li a {
margin: 0;
padding: 10px;
width: 100px;
display: block;
text-shadow: 0;
color: #000;
}
.LinksMenu ul ul {
/* margin-top: 3px; */
border: 1px solid #CCC;
border-radius: 3px;
background: #FFF;
position: absolute;
visibility: hidden;
}
.LinksMenu ul li:hover ul {
visibility: visible;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种修复方法是在悬停时添加悬停元素的高度,使该元素位于出现的元素下方。您需要将一个类添加到顶级
中。
检查小提琴。
One fix is to add height to the hovered element on hover, so the element is underneath the one that appears. You'll need to add a class to the top level
<li>
s.Check the fiddle.