我需要有关下拉菜单的帮助。当我将鼠标悬停在带有隐藏下拉菜单的选项卡顶部时,我遇到了这个问题。我遵循了W3Schools的说明
W3Schools的说明要求我使用position: absolute
,但如果我这样做而不是position: relative
,则下拉菜单甚至无法打开。
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .projects {
color: #fff39e;
border: none;
background-color: #f9fae8;
font-size: 18px;
padding: 12px;
text-decoration: none;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 900;
line-height: 25px;
margin: 0;
}
.dropdown-projects {
display: none;
position: relative;
color: #f9fae8;
background-color: #101119;
min-width: 160px;
z-index: 2;
}
.dropdown-projects a {
float: none;
color: #f9fae8;
padding: 10px;
text-decoration: none;
display: block;
text-align: left;
font-size: 16px;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 400;
line-height: 0.6;
margin: 0;
}
.dropdown-projects a:hover {
color: #fff39e !important;
}
.dropdown:hover .dropdown-projects {
display: block;
}
.dropdown .dilettante {
color: #caf5ce;
border: none;
background-color: #f9fae8;
font-size: 18px;
padding: 12px;
text-decoration: none;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 900;
line-height: 25px;
margin: 0;
}
.dropdown-dilettante {
display: none;
position: relative;
color: #f9fae8;
background-color: #101119;
min-width: 160px;
z-index: 2;
}
.dropdown-dilettante a {
float: none;
color: #f9fae8;
padding: 10px;
text-decoration: none;
display: block;
text-align: left;
font-size: 16px;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 400;
line-height: 0.6;
margin: 0;
}
.dropdown-dilettante a:hover {
color: #caf5ce !important;
}
.dropdown:hover .dropdown-dilettante {
display: block;
}

<div class="dropdown">
<button class="projects">PROJECTS</button>
<div class="dropdown-projects">
<a href="projects/artdirection.html">ART DIRECTION</a>
<a href="projects/branding.html">BRANDING</a>
<a href="projects/graphicdesigns.html">GRAPHIC DESIGNS</a>
<a href="projects/photography.html">PHOTOGRAPHY</a>
</div>
<button class="dilettante">DILETTANTE</button>
<div class="dropdown-dilettante">
<a href="instagram.html">INSTAGRAM</a>
<a href="quotes.html">QUOTES</a>
<a href="playlist.html">PLAYLIST</a>
<a href="goodreads.html">GOODREADS</a>
<a href="films.html">FILMS</a>
</div>
</div>
&#13;
答案 0 :(得分:0)
在HTML中,将两个菜单项都包装在<div class="dropdown">
。
在CSS中,在悬停时添加position: absolute
。
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .projects {
color: #fff39e;
border: none;
background-color: #f9fae8;
font-size: 18px;
padding: 12px;
text-decoration: none;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 900;
line-height: 25px;
margin: 0;
}
.dropdown-projects {
display: none;
position: relative;
color: #f9fae8;
background-color: #101119;
min-width: 160px;
z-index: 2;
}
.dropdown-projects a {
float: none;
color: #f9fae8;
padding: 10px;
text-decoration: none;
display: block;
text-align: left;
font-size: 16px;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 400;
line-height: 0.6;
margin: 0;
}
.dropdown-projects a:hover {
color: #fff39e !important;
}
.dropdown:hover .dropdown-projects {
display: block;
/* added */
position: absolute;
}
.dropdown .dilettante {
color: #caf5ce;
border: none;
background-color: #f9fae8;
font-size: 18px;
padding: 12px;
text-decoration: none;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 900;
line-height: 25px;
margin: 0;
}
.dropdown-dilettante {
display: none;
position: relative;
color: #f9fae8;
background-color: #101119;
min-width: 160px;
z-index: 2;
}
.dropdown-dilettante a {
float: none;
color: #f9fae8;
padding: 10px;
text-decoration: none;
display: block;
text-align: left;
font-size: 16px;
font-family: 'Lato', sans-serif, Helvetica;
font-weight: 400;
line-height: 0.6;
margin: 0;
}
.dropdown-dilettante a:hover {
color: #caf5ce !important;
}
.dropdown:hover .dropdown-dilettante {
display: block;
}
<div class="dropdown">
<button class="projects">PROJECTS</button>
<div class="dropdown-projects">
<a href="projects/artdirection.html">ART DIRECTION</a>
<a href="projects/branding.html">BRANDING</a>
<a href="projects/graphicdesigns.html">GRAPHIC DESIGNS</a>
<a href="projects/photography.html">PHOTOGRAPHY</a>
</div>
</div>
<div class="dropdown">
<button class="dilettante">DILETTANTE</button>
<div class="dropdown-dilettante">
<a href="instagram.html">INSTAGRAM</a>
<a href="quotes.html">QUOTES</a>
<a href="playlist.html">PLAYLIST</a>
<a href="goodreads.html">GOODREADS</a>
<a href="films.html">FILMS</a>
</div>
</div>
答案 1 :(得分:0)
在您的下拉列表中使用absolute position
并使用以下代码进行显示
.projects:hover + div.dropdown-projects {
display: block;
}
.dilettante:hover + div.dropdown-dilettante {
display: block;
}
你的代码也需要很多其他改进。
答案 2 :(得分:0)
这有帮助吗? JSFiddle
您必须相应地编辑CSS
.navbar {
overflow: hidden;
background-color: #fff;
/* Chnage the Background */
padding: 10px;
font-size: 16px;
font-family: 'Lato', sans-serif, Helvetica;
line-height: 0.6;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #FFF39E;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
font-weight: 900;
margin: 0;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: transparent;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #000;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: #fff;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
#dilettante {
color: #E1F7DB !important;
}
#projects {
color: #FFF39E;
}
.dropdown-content a:hover {
background-color: #000;
color: #FFF39E;
}
#dilettantedropdown a:hover {
color: #CAF5CE;
}
.dropdown:hover .dropdown-content {
display: block;
}
&#13;
<div class="navbar">
<div class="dropdown">
<button class="dropbtn" id="projects">PROJECTS</button>
<div class="dropdown-content">
<a href="projects/artdirection.html">ART DIRECTION</a>
<a href="projects/branding.html">BRANDING</a>
<a href="projects/graphicdesigns.html">GRAPHIC DESIGNS</a>
<a href="projects/photography.html">PHOTOGRAPHY</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn " id="dilettante">DILETTANTE
</button>
<div id="dilettantedropdown" class="dropdown-content dilettante">
<a href="instagram.html">INSTAGRAM</a>
<a href="quotes.html">QUOTES</a>
<a href="playlist.html">PLAYLIST</a>
<a href="goodreads.html">GOODREADS</a>
<a href="films.html">FILMS</a>
</div>
</div>
</div>
&#13;