简体   繁体   中英

CSS Dropdown Menu with Border & z-index

I have a fiddle going here: https://jsfiddle.net/vjdz8kxr/

<div class="span-8 last" id="primary_nav_wrap">
    <ul>
        <li>
            <a href="http://example.com/" class="home">Home</a>
        </li>
       <li><a href="http://example.com/kb">Knowledge</a>
           <ul>
                <li><a href="http://example.com/kb1">kb1</a></li>
                <li><a href="http://example.com/kb2">kb2</a></li>
                <li><a href="http://example.com/kb3">kb3</a></li>
            </ul>
        </li>
       <li><a href="http://example.com/forums">Forums</a>
           <ul>
                <li><a href="http://example.com/forum1">forum1</a></li>
                <li><a href="http://example.com/forum2">forum2</a></li>
                <li><a href="http://example.com/forum3">forum3</a></li>
            </ul>
        </li>
       <li><a href="http://example.com/pure_groups">Groups</a>
           <ul>
                <li><a href="http://example.com/group1">group1</a></li>
                <li><a href="http://example.com/group2">group2</a></li>
                <li><a href="http://example.com/group3">group3</a></li>
            </ul>
        </li>
       <li><a href="http://example.com/blog">Blogs</a>
           <ul>
                <li><a href="http://example.com/blog1">blog1</a></li>
                <li><a href="http://example.com/blog2">blog2</a></li>
                <li><a href="http://example.com/blog3">blog3</a></li>
            </ul>
        </li>
    </ul>

#primary_nav_wrap { float:right; }
#primary_nav_wrap ul { list-style:none; margin:0; padding:0; text-transform: uppercase;  }
#primary_nav_wrap ul a { display:block; color:#666666; text-decoration:none; font-weight:600; font-size:14px; line-height:30px; padding:0 15px; font-family:proxima-nova,sans-serif;}
#primary_nav_wrap ul li { float:left; padding:2px; }
#primary_nav_wrap ul li:hover {padding:0; border: 2px solid #efefef; border-bottom:2px solid #ee6129; position:relative; z-index:5; }
#primary_nav_wrap #home ul li:hover {border:2px solid #efefef; border-bottom:2px solid #efefef;}
#primary_nav_wrap ul ul { display:none; position:absolute; left:0; background:#fff; text-transform:none; border:2px solid #efefef; margin-left:-2px; margin-top:0; z-index:-1;}
#primary_nav_wrap ul li:nth-last-child(1) ul  { left:-101px;} /* Right Most Menu will pop to left so it doesn't bleed off page*/
#primary_nav_wrap ul ul li {float:none; width:220px; text-align:left;}
#primary_nav_wrap ul ul li:hover {background: #ee6129; text-align:left; border:none; padding:2px; }
#primary_nav_wrap ul ul a {  line-height:120%; padding: 5px;}
#primary_nav_wrap ul ul ul { top:0; left:100% }
#primary_nav_wrap ul li:hover > ul { display:block}

Basically, I am trying to make a border around the drop down, minus the part of the border that exists under the main menu's title. In this case, I have made that line Orange, so it is obvious whether this is working.

What I am expecting is that the orange line will show on top of the grey, with the z-index I have in place. I also am assuming that since the elements have a position (absolute or relative) that this should work.

Can anyone see what I am doing wrong here?

Here is a solution assuming the colors are basically staying the same.

  • Remove the border bottom (orange) on the main menu li:hover (border-bottom:none)

  • Also remove the z-index of 5 from that CSS Rule

  • Add a background of white to the selector.

  • Add a negative margin-top to your child (submenu). You added a negative left margin but there is no negative top.

Or just follow this: https://jsfiddle.net/vjdz8kxr/6/

#primary_nav_wrap ul li:hover {padding:0; border: 2px solid #efefef; border-bottom:none; position:relative; background:white;}
#primary_nav_wrap ul ul { display:none; position:absolute; left:0; background:#fff; text-transform:none; border:2px solid #efefef; margin-left:-2px; margin-top:-2px; z-index:-1;}

EDIT:

To add a little explanation: If you have a parent you want the parent to appear behind the child, the parent cannot have a z-index declaration AT ALL. Even if it is negative. Here is some proof: http://codepen.io/tylerism/pen/NGNxWR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM