简体   繁体   English

仅使用CSS将鼠标悬停在菜单上时显示div

[英]Show a div when hovering over a menu using just css

I have created a dropdown menu and now want a background that drops down along with it. 我创建了一个下拉菜单,现在想要一个与之一同下拉的背景。 Here is some of my code: 这是我的一些代码:

HTML: HTML:

<div id="background"></div>

CSS: CSS:

div#background{
height: 150px;
background-color: white;
display: none; }

ul#navmenu li:hover div#background
{
    display: block;
}

(I know there is something wrong with this code, this is what I picked up so far from the Internet...) li are the list items that comprise my menu. (我知道这段代码有问题,这是我到目前为止从Internet上获得的信息。)li是构成菜单的列表项。

In the HTML code, the "background" divider is inside and at the end of another divider which contains the dropdown menu: 在HTML代码中,“背景”分隔符位于另一个包含下拉菜单的分隔符的内部和结尾:

<div id="menu">
<ul id="navmenu"></ul>
<div id="background"></div>
</div>

ul is my unordered list which contains the menu. ul是我的无序列表,其中包含菜单。

What I want is to have the menu drop down along with the background. 我想要的是使菜单与背景一起下拉。 The background should also cover (be on top) of the text that comes immediately after the menu. 背景还应覆盖菜单顶部紧随其后的文本(位于顶部)。 (The menu drops onto the text). (菜单将显示在文本上)。

I would have loved to post a picture to make it a little clearer but I don't have enough reputation points yet... sorry :S 我很想发布一张图片以使其更清晰一点,但是我没有足够的声誉点...对不起:S

If possible I'd like to do it only using css, but I'm also open for other solutions. 如果可能的话,我只想使用CSS来做,但是我也愿意接受其他解决方案。 Any ideas? 有任何想法吗?

Your css is for a child of the li 你的css是给li的孩子的

This html code for your CSS 您的CSS的此html代码

<div id="menu">
<ul id="navmenu"><li><div id="background"></div></li></ul>
</div>

The background of your HTML is the sibling of navmenu. HTML的背景是navmenu的兄弟。

This CSS code for your HTML to show background when hovering over navmenu. 当您将鼠标悬停在navmenu上时,此CSS代码将为您的HTML显示背景。

<style>
div#background{
height: 150px;
background-color: white;
display: none; }

ul#navmenu:hover +div#background
{
    display: block;
}
</style>

If you want to do that from the LI you would need a parent's, sibling selector. 如果要通过LI进行操作,则需要一个父级的同级选择器。 I don't have one and would like one but jQuery could do the trick. 我没有一个,想要一个,但是jQuery可以解决这个问题。

Adjacent Sibling (+) combinator is available in Internet Explore 7 plus and is CSS 2.1 standard. Internet Explore 7 plus中提供了相邻的同级(+)组合器,它是CSS 2.1标准。

Assuming you want the background someplace other than inside the li block, position:relative it to the area you want it to appear. 假设您想要的背景不是li块内的其他位置,请将position:相对于您要显示的区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM