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

为何img元素会覆盖fixed定位header元素的黑色背景?

解决固定Header被图片覆盖的问题

你的问题根源在于堆叠层级(z-index):固定定位(fixed)的Header默认没有设置足够高的堆叠优先级,导致被下方的图片容器覆盖,黑色背景无法正常显示。

只需给Header添加Tailwind的z-50类(对应z-index: 50,数值足够覆盖页面内其他普通元素),就能让Header始终处于页面最上层:

修改后的Header代码:

<header class="fixed top-0 w-full bg-black z-50">
    <div class="sm:flex sm:justify-between">
    <div class="flex items-center justify-between px-2 py-2">
      <div>
        <a class="text-white font-bold text-xl" href="#"
        >Corner Hair Salon</a>
      </div>
      <div class="sm:hidden">
        <button type="button" class="block" onclick="myFunction()">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 fill-current text-white"><path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5"/></svg>
       </button>
      </div>
      </div>
      <div class="text-white px-1 pb-1 hidden sm:flex sm:items-center sm:pb-0" id="myLinks">
        <a href="#" class="block px-1 rounded hover:bg-gray-400">Contact Us</a>
        <a href="#" class="mt-1 block px-1 rounded hover:bg-gray-400 sm:mt-0 sm:ml-2">Book an Appointment</a>
        <a href="#" class="mt-1 block px-1 rounded hover:bg-gray-400 sm:mt-0 sm:ml-2">Employee Sign-In</a>
      </div>
    </div>

    <div class="flex flex-col space-y-1">
      <div class="bg-white w-full" style="height: 3px"></div>
      <div class="bg-white w-full" style="height: 2px"></div>
      <div class="bg-white w-full" style="height: 1px"></div>
    </div>
  </header>

添加z-50后,Header的黑色背景会保持不透明,滚动时始终显示在图片上方,不会被覆盖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 00:22:16