CSS 位置绝对覆盖导航栏位置固定

发布于 2025-01-12 14:09:09 字数 348 浏览 8 评论 0原文

我创建了两个图像的悬停效果。将鼠标悬停在 div“img3”中时,图像 1 会更改为图像 2。两个图像都在 div"img3" 中,image2 位置是绝对的。现在我的导航栏或标题(“标题”部分)位置已固定。因此,每当我将鼠标悬停在 image1 上时,image2 就会出现在导航栏的顶部,如下图 2 所示。 图片1 图片2

I have created hover effect of two images. image1 changes to image2 on hover in div"img3". Both images are in div"img3" and image2 position is absolute. Now my Navbar or header (section "header") position is fixed. So whenever I hover on image1 image2 comes on top of the Navbar as shown in the image2 below.
Image1
Image2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2025-01-19 14:09:09

将其添加到您的导航栏,

z-index: 100;

因为您添加的任何内容都不会超出导航栏。

Add this to your navbar

z-index: 100;

because of this whatever u add will not poke over navbar.

木格 2025-01-19 14:09:09

DarkForest的答案是正确的。
只是进一步解释一下 - z-index 的属性
定义网站元素的分层适用于 position:fixedposition:absolute。分层的默认行为是分层的,这意味着代码中较靠前的元素将最初覆盖第一个元素。
除非您为这些元素中的一个或两个定义属性 z-index
数字越大,显示越上层。

通常,您希望为固定导航栏分配尽可能高的 z-index ,因为您实际上并不希望在其上显示任何内容(除非它是其中的元素)

总而言之:

.navbar {
   /*dimensions and other styles*/
   position:fixed;
   z-index: 100;
}

应该做把戏

The answer of DarkForest is correct.
Just to explain a bit further - the attribute of z-index which
defines layering of site elements is applicable to both position:fixed and position:absolute. The default behavior of layering is hierarchical, which means, that the element which is further in the code will initialy overlay the first element.
That is unless you define attribute z-index to one or both of these elements.
the higher the number, the more upper layer of display.

Usually, you want to assign as high z-index as you can to the fixed navbar as you don't really want anything to display over it (unless it's an element inside of it)

To summarize:

.navbar {
   /*dimensions and other styles*/
   position:fixed;
   z-index: 100;
}

should do the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文