I have created a dropdown menu using pure CSS3, but I am facing an issue with the z-index property. The dropdown list is appearing above the menu when it should be dropping below it. I have spent all day trying to troubleshoot this problem to no avail, so now I am reaching out for help.
I have applied different background colors to highlight the problematic items and demonstrate what I am aiming for. The goal is to have the sub-menu with the red background display under the blue background.
P.S. I attempted to create this menu using jQuery slideDown/slideUp properties, but they do not provide the desired slide effect seen in my example. They appear more like stretching, which is not the outcome I desire.
ul {
list-style-type: none;
}
.menu_wrapper {
position: relative;
z-index: 999;
/* THE Z-INDEX IS NOT WORKING... */
height: 70px;
width: 600px;
background-color: blue;
}
.menu li {
display: inline;
float: left;
display: table;
height: inherit;
margin: 3px;
margin-top: 10px;
}
.menu li a {
display: table-cell;
font-family: Verdana;
font-size: 16px;
color: gold;
text-align: center;
text-decoration: none;
padding-left: 10px;
padding-right: 10px;
vertical-align: middle;
background: #05487F;
transition: .2s ease-in-out;
}
.sub-menu {
position: absolute;
z-index: 1;
/* THE Z-INDEX IS NOT WORKING... */
margin-top: -200px;
margin-left: -132px;
padding: 15px;
padding-top: 20px;
background-color: red;
transition: all 1s ease-in-out;
}
.sub-menu li {
float: none;
}
.sub-menu li a {
padding: 5px 15px;
}
.menu li a:hover + .sub-menu {
margin-top: 40px;
}
.sub-menu:hover {
margin-top: 40px;
}
<nav class="menu_wrapper">
<ul class="menu">
<li><a href="about/index.html">About</a>
</li>
<li><a href="news/index.html">News</a>
</li>
<li>
<a href="">PROBLEM HERE<br>(HOVER THIS)</a>
<ul class="sub-menu">
<li><a href="">link 1</a>
</li>
<li><a href="">link 2</a>
</li>
<li><a href="">link 3</a>
</li>
</ul>
</li>
<li><a href="">Something</a>
</li>
<li><a href="">Contacts</a>
</li>
</ul>
</nav>