[英]How to format drop down menu with a sticky nav bar (no jQuery)
I'm trying to build a personal website with a sticky nav bar that also has a drop down menu under some of the items.我正在尝试建立一个带有粘性导航栏的个人网站,该网站在某些项目下还有一个下拉菜单。 The nav bar is "sticking" but the drop down menu won't appear.
导航栏“粘住”,但不会出现下拉菜单。 I have found that the issues revolve around setting the position of drop down content to absolute and the overflow of the the ul that creates the "nav bar" to hidden.
我发现问题围绕将下拉内容的位置设置为绝对位置以及将“导航栏”创建为隐藏的 ul 溢出。 (PS I'm relatively new to CSS, so I apologize ahead of time).
(PS 我对 CSS 比较陌生,所以我提前道歉)。
So far, I have tried changing the position of the drop down content to "fixed," but this only works properly until you scroll down.到目前为止,我已经尝试将下拉内容的位置更改为“固定”,但这只能在您向下滚动之前正常工作。 Once you scroll down (as aspected) it remains the same distance down and does not adjust to the nav bar's new location.
一旦你向下滚动(按方面),它会保持相同的向下距离,并且不会调整到导航栏的新位置。 In addition, I have changed the nav bar's overflow to visible.
此外,我已将导航栏的溢出更改为可见。 The drop down works correctly, but the formatting of the nav bar is completely messed up.
下拉菜单正常工作,但导航栏的格式完全混乱。 Essentially, all of these changes have made the problem "better," but only creates new issues with them.
从本质上讲,所有这些变化都使问题“更好”,但只会产生新的问题。
ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; position: -webkit-sticky; /* Safari */ position: sticky; width: 100%; top: 0px; } li { float: left; } li a, .dropbtn { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover, .dropdown:hover .dropbtn { background-color: rgb(228, 40, 40); } li.dropdown { display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #f1f1f1 } .dropdown:hover .dropdown-content { display: block; }
<header> <h1><a id="myNameTop" href="/">JK</a></h1> </header> <ul> <li class="nav-item"><a class="nav-link" href="/resume">Resumé</a></li> <li class="dropdown nav-item"> <a class="dropbtn" href="/code">Code</a> <div class="dropdown-content"> <a href="/gitHub">GitHub</a> <a href="/javaScriptProjects">Embeded JavaScript Projects</a> </div> </li> <li class="nav-item"><a class="nav-link" href="/graphicDesign">Graphic Design</a></li> <li class="nav-item"><a class="nav-link" href="/contact">Contact</a></li> </ul>
Currently, I hover over the nav bar item and the drop down item does not come down at all (no matter if I scrolled or not).目前,我将鼠标悬停在导航栏项目上,并且下拉项目根本没有下降(无论我是否滚动)。
I want to see the drop down menu when I hover.我想在悬停时看到下拉菜单。
The problem is you're hiding the overflow
of ul
.问题是你隐藏了
ul
的overflow
。
You added overflow: hidden
to the <ul>
so that its background extended under all it's <li>
s, because the <li>
s have float:left
, for no particular reason.你添加了
overflow: hidden
到<ul>
以便它的背景扩展到所有它的<li>
s下,因为<li>
s有float:left
,没有特别的原因。
I suggest "floating" the <li>
s using either display:inline-block
on them or display:flex
on their parent.我建议在
<li>
使用display:inline-block
或在其父级上使用display:flex
来“浮动”。 And, obviously, removing the offending overflow:hidden
.而且,显然,删除有问题的
overflow:hidden
。
ul { list-style-type: none; margin: 0; padding: 0; /* overflow: hidden; */ background-color: #333; position: -webkit-sticky; /* Safari */ position: sticky; width: 100%; top: 0px; } li { /* float: left; */ display: inline-block; } li a, .dropbtn { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover, .dropdown:hover .dropbtn { background-color: rgb(228, 40, 40); } li.dropdown { display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #f1f1f1 } .dropdown:hover .dropdown-content { display: block; } body { margin: 0; min-height: 200vh; }
<header> <h1><a id="myNameTop" href="/">JK</a></h1> </header> <ul> <li class="nav-item"><a class="nav-link" href="/resume">Resumé</a></li> <li class="dropdown nav-item"> <a class="dropbtn" href="/code">Code</a> <div class="dropdown-content"> <a href="/gitHub">GitHub</a> <a href="/javaScriptProjects">Embeded JavaScript Projects</a> </div> </li> <li class="nav-item"><a class="nav-link" href="/graphicDesign">Graphic Design</a></li> <li class="nav-item"><a class="nav-link" href="/contact">Contact</a></li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.