如何解决Tailwind CSS中导航下拉菜单与内容重叠的问题?
导航下拉菜单与页面主体内容重叠的解决方法

如图所示,导航下拉菜单与页面主体内容发生重叠,以下是相关代码及解决办法:
导航代码
<div class="dropdown relative"> <a class="dropdown-toggle flex items-center hidden-arrow" href="#" id="dropdownMenuButton2" role="button" data-bs-toggle="dropdown" aria-expanded="false"> <span class="visible md:invisible md:hidden lg:invisible lg:hidden py-4 px-2 text-black"> logo </span> </a> <ul class=" dropdown-menu min-w-max absolute hidden bg-white text-base z-10 float-left py-2 list-none text-left rounded-lg shadow-lg mt-1 hidden m-0 bg-clip-padding left-auto right-0" aria- labelledby="dropdownMenuButton2"> <li> <a class="dropdown-item text-sm py-2 px-4 font-normal block w-full whitespace-nowrap bg-transparent text-gray-700 hover:bg-gray-100" href="{{route('home')}}">Home</a> </li> <li> <a class="dropdown-item text-sm py-2 px-4 font-normal block w-full whitespace-nowrap bg-transparent text-gray-700 hover:bg-gray-100" href="{{route('contactUs')}}">Contact Us</a> </li> </ul> </div>
折叠面板代码
<div class="accordion accordion-flush font-pop" id="accordionFlushExample"> @foreach($parentalcare as $key=>$p) <div class="accordion-item border-t-0 border-l-0 border-r-0 rounded-none bg-white border border-gray-200"> <h2 class="accordion-header mb-0" id="flush-heading{{$key}}"> <button class="accordion-button relative flex items-center w-full py-4 px-5 text-gray-800 font-bold text-left text-xl bg-white border-0 rounded-none transition focus:outline-none {{($key=='0')?'':'collapsed'}} " type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapse{{$key}}" aria-expanded="{{($key=='0')?'true':''}}" aria-controls="flush-collapse{{$key}}"> {{$p['question']}} </button> </h2> <div id="flush-collapse{{$key}}" class="accordion-collapse border-0 collapse {{($key=='0')?'show':''}}" aria-labelledby="flush-heading{{$key}}" data-bs-parent="#accordionFlushExample"> <div class="accordion-body text-xl py-4 px-5 text-black"> {{$p['answer']}} </div> </div> </div> @endforeach </div>
解决办法
1. 提高下拉菜单的层级优先级
当前下拉菜单使用z-10的层级值,若页面主体内容(比如折叠面板)的层级更高,就会出现重叠覆盖问题。可以将下拉菜单的层级调高,比如替换为z-50(Tailwind CSS中z-50远高于z-10):
修改导航代码中的<ul>元素class,把z-10替换成z-50:
<ul class=" dropdown-menu min-w-max absolute hidden bg-white text-base z-50 float-left py-2 list-none text-left rounded-lg shadow-lg mt-1 hidden m-0 bg-clip-padding left-auto right-0" aria- labelledby="dropdownMenuButton2">
2. 检查主体内容的层级设置
查看折叠面板的容器或内部元素是否设置了过高的z-index属性,如果有且数值大于下拉菜单的层级,需要降低该值或者直接移除z-index设置,避免干扰下拉菜单的显示层级。
3. 确保导航父容器无溢出截断
检查导航的父级容器是否添加了overflow:hidden属性,该属性会截断下拉菜单的显示,同时也可能影响层级渲染,若存在则移除该属性。
内容的提问来源于stack exchange,提问作者Rushikesh Ganesh
相关产品推荐
相关产品推荐

