【发布时间】:2026-01-06 18:45:02
【问题描述】:
HTML
<body>
<div class="wordsmith">
<p>WORDSMITH: dummy text here.</p>
</div>
<div class="menu">
<ul>
<li><a id="wordsmith">The Wordsmith</a></li>
<li><a id="tracksmith">The Tracksmith</a></li>
<li><a id="nerdsmith">The Nerdsmith</a></li>
<li><a id="family">The Family Man</li>
</ul>
</div>
CSS
.menu {
background: rgba(0,0,0,0.6);
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
text-align: center;
}
.wordsmith {
background: rgba(0,0,0,0.6);
max-width: 500px;
margin: 0 auto;
padding: 15px;
display: none;
}
.menu #wordsmith:hover ~ .wordsmith {
display: block;
}
我有一个位于页面中间的菜单,其中包含 UL 中的文本链接。我想要实现的是将鼠标悬停在其中一个菜单项上,一个带有信息的 div 将出现在顶部。我找到了here(JS Fiddle)的原始解决方案。如果我尝试在 div 和 UL 之外实现这一点,那么一切正常。一旦我将菜单项放在 div 或 UL 中,CSS 就会中断。我是否使用“.menu #wordsmith”部分正确调用了 CSS 选择器?任何帮助表示赞赏!请记住,我正在尝试严格使用 CSS 和 html,而不是 JS。谢谢。
【问题讨论】:
-
因为它不是
a的兄弟,你需要使用 jQuery 或 javascript