gpt4 book ai didi

html - CSS - 在 UL 的类中调用 id 以在悬停时显示 div

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:06 24 4
gpt4 key购买 nike

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。谢谢。

最佳答案

据我所知,它们没有前任或祖先组合器/选择器,所以这在 CSS 中是不可能的。 ~是一般兄弟组合子,#wordsmith.wordsmith 不是 sibling ,.wordsmith#wordsmith 的祖先的前身
如果您确实需要这样做,则必须使用 JavaScript 或重构您的 HTML。

关于html - CSS - 在 UL 的类中调用 id 以在悬停时显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com