将非同级与 z 索引交错
我试图让侧边栏菜单与内容 div
重叠,其中活动菜单项出现在 div
上方,非活动菜单项出现在下面。 ul
和 div
之间的交集很小,但交错效果会产生深度错觉。
我知道 z-index
仅适用于同级元素。 因此,以下内容不起作用:
#menu {z-index:0}
#menu li.active {z-index:2}
#content {z-index:1}
<ul id="menu">
<li class="active">Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<div id="content">Side content</div>
有没有一种好的方法可以做到这一点,而不必使每个菜单项成为与 #content
处于同一级别的 div
?
I'm trying to have a sidebar menu overlap a content div
, where the active menu item appears over the div
and the non-active items would appear under. The intersection between a ul
and div
would be small, but the interleaving effect would create an illusion of depth.
I understand that z-index
only applies to sibling elements. So the following doesn't work:
#menu {z-index:0}
#menu li.active {z-index:2}
#content {z-index:1}
<ul id="menu">
<li class="active">Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<div id="content">Side content</div>
Is there a good way to do this without having to make each menu item a div
on the same level as #content
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您希望一些
li
出现在div
的前面,而其他则出现在其后面。实现此目的的唯一方法是确保所有
li
和div
属于同一个 堆叠上下文。 这意味着ul
不得创建任何堆叠上下文,因此不要为其设置任何z-index
。 请参阅哪些 CSS 属性创建堆叠上下文?了解到底应该避免什么。然后就很简单了:只需根据需要将
z-index
属性设置为li
和div
即可。 但是,请记住,z-index
仅适用于定位元素,例如position:relative
。You want some
li
to appear at the front of adiv
, and the other ones at its back.The only way to achieve this is making sure all the
li
and thediv
belong to the same stacking context. That means theul
must not create any stacking context, so don't set anyz-index
to it. See Which CSS properties create a stacking context? for what exactly should be avoided.And then it will be easy: just set the
z-index
properties to theli
and thediv
as desired. However, remember thatz-index
only applies to positioned elements, e.g.position: relative
.我可以建议使用一些背景技巧,而不是搞乱 z 索引和毛茸茸的 CSS 黑客吗? 您可以做的一件事是为菜单提供重复的背景,以便其一部分看起来是内容 div 的一部分。 然后,活动项目可以具有看起来延伸到内容中的背景。 这要简单得多,并且可以避免很多令人头痛的事情。
Rather than messing with z-indices and hairy CSS hacks, may I suggest using some background tricks? One thing you can do is to give the menu a repeating background so that a portion of it APPEARS to be part of the content div. Then the active item can have a background that appears to extend into the content. This is much simpler and avoids a lot of headaches.
我建议不要使用 z-index 来执行此操作,而是尝试根据 li 项目是否处于活动状态来设置它们的宽度。 如果您使侧边栏导航与内容 div 齐平,则可以使 .active li 稍微宽一些,以便它与内容 div 重叠。 这样,菜单的非活动部分看起来就位于内容 div 下方。 如果我没有正确理解你的想法,也许你可以给我举个例子。 祝你好运,听起来效果很不错。
I suggest instead of using z-index to do this, try setting the widths of the li items based on whether they are active or not. If you make the side bar navigation flush with the content div, you could then feasibly make the .active li slightly wider so that it overlaps the content div. This way it will look like the non-active part of the menu is under the content div. If I'm not understanding your idea correctly perhaps you could show me an example. Good luck, sounds like a nifty effect.
给 li 一个 z-index 而不是 #menu:
并且不要忘记将它们相对定位以使 z-index 生效。
give the li a z-index instead of the #menu:
And don't forget to position them relative for the z-index to take effect.