You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

鼠标悬停时图片覆盖固定顶部导航栏的原因及解决方法

问题:图片悬停时覆盖固定顶部导航栏如何解决?

我是编程新手,正在开发第一个Kirby网站。设置了固定顶部导航栏和可滚动图片画廊,实现了图片悬停变黑白的效果,但悬停图片时,图片会突然覆盖固定顶部导航栏。尝试用z-index解决但没效果,可能是CSS代码太混乱了,很多代码都是试错写出来的。

原始HTML代码

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $site->title() ?></title>
    <?= css('index.css') ?>


</head>


</head>
<body>
<header> 
<div class="left">  
<div class="fixed">   
<div class="logo">
        <a href="<?= $site->url() ?>"><?= $site->title() ?></a>
        <a class="sublogo"> Sound Design | Composing | Post Produktion </a>
        </div>
        </div>
        </div>
          
</header>   
<div class="flexbox"> 
  <div class="left">

      <div class="fixed">
      <nav class="menu" id="menu" >
      <?php
// get the first set of subpages which should be used
$subpages = $pages->find('projects')->children();
// create the snippet beginning with those subpages
snippet('treemenu', ['subpages' => $subpages]);
?>


        </nav>  
        <nav class="menu" id="menu2" >
 <?php foreach ($site->children()->listed() as $subpage): ?>
                <a href="<?= $subpage->url() ?>"><?= $subpage->title() ?></a>
                <?php endforeach ?>
                </nav>  
      </div>
  </div>


  <div class="right">
  <ul class="projects">
<?php foreach ($page->children()->listed() as $project): ?>
    <li>
    <div>
        <a href="<?= $project->url() ?>">
        <?= $project->image()->crop(500) ?>
        </a>
       
    </div>
</li>   
<?php endforeach ?>

</ul>  

  </div>

</div>
</body>
</body>

</body>
</html>

原始CSS代码

*,
*:after,
*:before {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box
}


html {
    height: 100%;
    overflow: hidden;
    font-family: ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji"
}

body {
  background-color: white;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
  word-wrap: break-word; 
  overflow: auto;
}
 
a {
  color: inherit;
  text-decoration: none;
  pointer-events: auto;
}

/*Helps With Images*/
img, video {
  width:100%;
  height:auto;
}

img:hover {
  filter: grayscale(100%);
  -webkit-filter: grayscale(100%);
}

li {
  list-style-type: none;
}

.logo {
  background-color: white;
  overflow: hidden;
  position: fixed;
  float: left;
  display: block;
  padding-top: 3%;
  top: 0;
  padding-bottom: 3%;
  width: 100%;
  margin: 0% 1%;
  font-size: 18px;
  font-weight: bold;
/* border-bottom: 0.125vw solid #000000; */
}

.sublogo {
  flex-basis: auto;
  margin-left: 85px;
  font-size: 18px;
  font-weight: normal;
}

.menu {
  line-height: 150%;
  font-size: 16px;
  color: black;
  
}

#menu {
  /* text-decoration: underline; */
  width: 70%;
  margin: 0% 5%;
}

#menu2 {
    width: 5%;
    margin-bottom: 30px;
    margin-left : 15px;
    position: fixed;
    bottom: 0;
}

a:hover {
  font-style: italic;
  text-decoration: underline;
}

a.logo:hover {
  font-style: normal;
  text-decoration: none;
}

a.sublogo:hover {
  font-style: normal;
  text-decoration: none;
}

.projects {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  list-style: none;
  width: 95%;
  grid-gap: 0.2rem;
  margin-top: 3.5%;
  margin-left: 11%;

}

ul.gallery {
  width: 80%;
  padding: 0% 10% 0% 10%;
  
  
}

.flexbox {
    display: flex;
    width: 100%;
    height: 100vh;
    
}

.left {
    flex-basis: auto;
    width: 12%;
    padding: 2rem;
    /* border-right: 0.125vw solid #a6a6a6; */
}

.right {
   width: 80%;
}

.fixed {
  position: fixed;
}

解决方案

问题核心原因

z-index失效是因为堆叠上下文规则:只有定位元素(position: relative/absolute/fixed/sticky)才能应用z-index,且同一堆叠上下文中,z-index值越高的元素层级越靠前。你的.logo虽然是fixed定位,但未设置z-index;同时HTML存在冗余标签(重复的</head>、多个</body>),可能干扰浏览器渲染优先级,导致图片悬停时覆盖导航栏。

具体修复步骤

  1. 清理HTML冗余标签:删除重复的</head>和多余的</body>,保证HTML结构合法。
  2. 给顶部导航栏设置高z-index:给.logo添加z-index: 999;,确保它处于所有元素最上层。
  3. 调整其他固定元素z-index:给左侧导航的.fixed容器设置较低z-index(如z-index: 100;),避免和顶部导航冲突。
  4. 修复导航栏宽度溢出:.logo的width:100%加margin:0%1%会导致横向溢出,把margin换成padding-left:1%; padding-right:1%;,同时去掉无用的float:left(fixed定位无需float)。

修改后的代码

清理后的HTML

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $site->title() ?></title>
    <?= css('index.css') ?>
</head>
<body>
<header> 
    <div class="left">  
        <div class="fixed">   
            <div class="logo">
                <a href="<?= $site->url() ?>"><?= $site->title() ?></a>
                <a class="sublogo"> Sound Design | Composing | Post Produktion </a>
            </div>
        </div>
    </div>
</header>   
<div class="flexbox"> 
    <div class="left">
        <div class="fixed">
            <nav class="menu" id="menu" >
                <?php
                // get the first set of subpages which should be used
                $subpages = $pages->find('projects')->children();
                // create the snippet beginning with those subpages
                snippet('treemenu', ['subpages' => $subpages]);
                ?>
            </nav>  
            <nav class="menu" id="menu2" >
                <?php foreach ($site->children()->listed() as $subpage): ?>
                    <a href="<?= $subpage->url() ?>"><?= $subpage->title() ?></a>
                <?php endforeach ?>
            </nav>  
        </div>
    </div>
    <div class="right">
        <ul class="projects">
            <?php foreach ($page->children()->listed() as $project): ?>
                <li>
                    <div>
                        <a href="<?= $project->url() ?>">
                            <?= $project->image()->crop(500) ?>
                        </a>
                    </div>
                </li>   
            <?php endforeach ?>
        </ul>  
    </div>
</div>
</body>
</html>

修改后的关键CSS片段

.logo {
  background-color: white;
  overflow: hidden;
  position: fixed;
  display: block;
  padding-top: 3%;
  top: 0;
  padding-bottom: 3%;
  width: 100%;
  padding-left: 1%;
  padding-right: 1%;
  font-size: 18px;
  font-weight: bold;
  z-index: 999; /* 新增:确保导航栏在最上层 */
}

.fixed {
  position: fixed;
  z-index: 100; /* 新增:给左侧导航设置较低层级 */
}

额外优化建议

  • 给.projects的margin-top改为基于顶部导航栏高度的定值(如margin-top: 80px;),避免百分比适配问题。
  • 给.logo添加box-shadow: 0 2px 4px rgba(0,0,0,0.1);,增强视觉层级,避免和图片混淆。

内容的提问来源于stack exchange,提问作者lea ikea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:59:59