影响 CSS / HTML 中父元素的背景图像?
我正在尝试用 HTML / CSS 构建一个向下滚动菜单,但遇到了一个恼人的问题...... 我使用以下教程来构建我的菜单: http: //ago.tanfa.co.uk/css/examples/menu/tutorial-h.html#hs7
我的问题: 鼠标悬停时,导航按钮的背景图像会发生变化,并且会出现下拉菜单 - 到目前为止,一切都很好 - 但是当您将指针移至下拉菜单时,导航按钮的图像将返回到其第一个状态。不过,我试图保持相同的图像,因此下拉菜单看起来像属于导航按钮(因为事实上,它确实......)
上的样子:
<ul id="nav_menu"><li id="nav_button"><a href="#">Products</a>
<ul>
<li id="submenu"><a href="#">Product 1</a></li>
<li id="submenu"><a href="#">Product 2</a></li>
<li id="submenu"><a href="#">Product 3</a></li>
<li id="submenu"><a href="#">Product 4</a></li>
</ul>
</li></ul>
以下是代码在HTML CSS:
#nav_button a {
display:block;
background-image:url(img1.jpg);
background-repeat:no-repeat;
}
#nav_button a:hover {
display:block;
background-image:url(image2.jpg);
background-repeat:no-repeat;
}
#nav_button ul {
display:none;
}
#nav_menu li:hover ul {
display:block;
}
#submenu a {
background-image: none;
}
我知道问题出在我的 #submenu a 中的“background-image:none”,这实际上影响了我代码中的整个 a...因为 CSS 中没有父选择器;不幸的是,我无法真正修改下拉列表中的导航按钮。
你知道怎么做吗?我想答案并不复杂,但我真的很感激一些帮助!
提前致谢! D .
I'm trying to build a scroll down menu in HTML / CSS and am experiencing an annoying problem...
I have used the following tutorial to build my menu: http://ago.tanfa.co.uk/css/examples/menu/tutorial-h.html#hs7
My problem:
On mouseover, my navigation button's background image changes, and the drop down menu appears - so far, so good - but when you move the pointer to the dropdown menu, the navigation button's image goes back to its first status. I'm trying to keep teh same image though, so the drop down menu would look as it belonged to the navigation button (since, in fact, it does...)
Here's how the code looks like on the HTML:
<ul id="nav_menu"><li id="nav_button"><a href="#">Products</a>
<ul>
<li id="submenu"><a href="#">Product 1</a></li>
<li id="submenu"><a href="#">Product 2</a></li>
<li id="submenu"><a href="#">Product 3</a></li>
<li id="submenu"><a href="#">Product 4</a></li>
</ul>
</li></ul>
And in the CSS:
#nav_button a {
display:block;
background-image:url(img1.jpg);
background-repeat:no-repeat;
}
#nav_button a:hover {
display:block;
background-image:url(image2.jpg);
background-repeat:no-repeat;
}
#nav_button ul {
display:none;
}
#nav_menu li:hover ul {
display:block;
}
#submenu a {
background-image: none;
}
I understand that the problem is the "background-image:none" in my #submenu a, which in fact impacts the whole a in my code... Since there's no parent selector in CSS; I can't really modify the navigation button from the drop down list, unfortunately.
Do you know a way to do it? I guess the answer is not that complicated, but I'd really appreciate some help!
Thanks in advance!
D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个...
而不是...
使用颜色而不是图像的演示: http://jsfiddle.net /wdm954/gwjmK/1/
Try this...
instead of...
Demo using colors instead of images: http://jsfiddle.net/wdm954/gwjmK/1/
我敢打赌:您正在更改包含子菜单的链接的图像。当您将鼠标移到子菜单上时,您不再位于
上,我认为您应该更改
元素的高度以适应显示时的子菜单之一。如果您将背景图像提供给
而不是我猜的,效果会更好。
My bet: you are changing the image of a link which contains a submenu. When you move the mouse over the submenu, you are not longer on the
<a>
, I think you should change the height of the<a>
element to adapt the one of the submenu when it's displayed. Would be better if you give the background image to the<li>
instead of the I guess.