Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

z-index not working on drop down menu

I have a drop down menu which has the following html structure:

<ul class="menu">
   <li><a href="">Menu Item 1</a>
      <ul class="sub-menu">
         <li><a href="">Sub Menu Item 1</a></li>
      </ul>
   </li>
</ul> 

and I have the following css rules:

.menu {float:left}
.menu > li {position:relative; float:left}
.menu > li > a {display:block}
.sub-menu {display:none; z-index:100; position:absolute; top:40px; width:180px;}

I'm using javascript to show the drop down menu.

The issue I have is that the sub-menus are appearing below a slideshow which I have close to the navigation. No matter how high or how low I set the z-index of .sub-menu, nothing changes.

Does anyone know what could possibly trigger z-index to not work at all?

Thanks.

EDIT: The issue is with all browsers. Testing in Firefox, Chrome and Internet Explorer

like image 261
Luke Vella Avatar asked Apr 11 '11 13:04

Luke Vella


People also ask

Why your Z index is not working?

You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

Why Z index is not working with div?

The root element forms the root stacking context. Other stacking contexts are generated by any positioned element (including relatively positioned elements) having a computed value of 'z-index' other than 'auto'. Stacking contexts are not necessarily related to containing blocks.

Do you need position absolute for Z index?

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

Does Z Index work with static positioning?

z-index only affects elements that have a position value other than static (the default). Elements can overlap for a variety of reasons, for instance, relative positioning has nudged it over something else. Negative margin has pulled the element over another. Absolutely positioned elements overlap each other.


2 Answers

I think I have found the issue. I was using opacity on the div containing the menu. For some reason this caused z-index to not work on the sub-menu. I am not sure why this is. It works fine now that I've removed the opacity rule.

like image 145
Luke Vella Avatar answered Oct 08 '22 16:10

Luke Vella


Check the z-index of the elements it is supposed to appear above. make sure they are lower. Also make sure that the parent element hasn't got the overflow hidden.

like image 43
Stu Avatar answered Oct 08 '22 16:10

Stu