简体   繁体   English

带边框和z-index的CSS下拉菜单

[英]CSS Dropdown Menu with Border & z-index

I have a fiddle going here: https://jsfiddle.net/vjdz8kxr/ 我在这里摆弄一个小提琴: 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. 我期望的是,橙色线将显示在灰色顶部,并且具有我已设置的z索引。 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) 移除主菜单li:hover上的边框底部(橙色)(border-bottom:none)

  • Also remove the z-index of 5 from that CSS Rule 同时从该CSS规则中删除5的z-index

  • 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/ 或只是遵循此: 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. 补充一点说明:如果您有父母,则希望父母出现在孩子的后面,则父母不能有z-index声明AT ALL。 Even if it is negative. 即使是负面的。 Here is some proof: http://codepen.io/tylerism/pen/NGNxWR 这是一些证明: http : //codepen.io/tylerism/pen/NGNxWR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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