我的纯 CSS 下拉菜单不起作用,有人知道如何解决吗?
My pure CSS dropdown isn't working, anybody knows how to fix it?
我是 CSS 初学者,我的纯 CSS 下拉菜单不起作用。
此 jsfiddle 中的示例:https://jsfiddle.net/uevewfsy/
我一直在网上四处寻找并试图自己修复它,但我已经尝试了无数种方法,但它仍然没有修复。有一个浮动:左;在主 ul 上似乎修复了它,但后来我的导航不再居中。
希望有人能帮助我,这样我就可以在编程方面走得更远;)
* {
padding: 0;
margin: 0;
border: 0;
}
html, body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 100;
}
.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}
.nav ul {
height: 100%;
width: 100%;
}
.nav > ul > li {
display: inline-block;
list-style: none;
margin: 0 20px 0 20px;
}
.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}
.nav > ul > li > ul {
display: none;
}
.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px 0 15px;
}
.nav li:hover > ul {
display: block;
}
.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}
.nav a:hover {
color: #FFF;
}
@media only screen and (max-device-width: 480px){
}
<body>
<div class="nav">
<ul>
<li>
<a href="#">PAGE</a>
<ul>
<li><a href="#">DROPDOWN</a></li>
<li><a href="#">DROPDOWN</a></li>
</ul>
</li>
<li><a href="#">PAGE</a></li>
<li><a href="#">PAGE</a></li>
<li><a href="#">PAGE</a></li>
</ul>
</div>
</body>
您的 nav > ul > li
、
中缺少 vertical-align:top
您也可以将 position:relative/top
添加到您的 li/ul
。
* {
padding: 0;
margin: 0;
border: 0;
}
html,
body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 100;
}
.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}
.nav ul {
height: 100%;
width: 100%;
}
.nav > ul > li {
display: inline-block;
list-style: none;
padding: 0 20px;
vertical-align: top
}
.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}
.nav > ul > li > ul {
display: none;
}
.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px;
position:relative
}
.nav li:hover > ul {
display: block;
position:absolute;
top:100%;
width:30%
}
.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}
.nav a:hover {
color: #FFF;
}
@media only screen and (max-device-width: 480px) {}
<body>
<div class="nav">
<ul>
<li>
<a href="#">PAGE</a>
<ul>
<li><a href="#">DROPDOWN</a>
</li>
<li><a href="#">DROPDOWN</a>
</li>
</ul>
</li>
<li><a href="#">PAGE</a>
</li>
<li><a href="#">PAGE</a>
</li>
<li><a href="#">PAGE</a>
</li>
</ul>
</div>
</body>
我是 CSS 初学者,我的纯 CSS 下拉菜单不起作用。
此 jsfiddle 中的示例:https://jsfiddle.net/uevewfsy/
我一直在网上四处寻找并试图自己修复它,但我已经尝试了无数种方法,但它仍然没有修复。有一个浮动:左;在主 ul 上似乎修复了它,但后来我的导航不再居中。
希望有人能帮助我,这样我就可以在编程方面走得更远;)
* {
padding: 0;
margin: 0;
border: 0;
}
html, body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 100;
}
.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}
.nav ul {
height: 100%;
width: 100%;
}
.nav > ul > li {
display: inline-block;
list-style: none;
margin: 0 20px 0 20px;
}
.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}
.nav > ul > li > ul {
display: none;
}
.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px 0 15px;
}
.nav li:hover > ul {
display: block;
}
.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}
.nav a:hover {
color: #FFF;
}
@media only screen and (max-device-width: 480px){
}
<body>
<div class="nav">
<ul>
<li>
<a href="#">PAGE</a>
<ul>
<li><a href="#">DROPDOWN</a></li>
<li><a href="#">DROPDOWN</a></li>
</ul>
</li>
<li><a href="#">PAGE</a></li>
<li><a href="#">PAGE</a></li>
<li><a href="#">PAGE</a></li>
</ul>
</div>
</body>
您的 nav > ul > li
、
vertical-align:top
您也可以将 position:relative/top
添加到您的 li/ul
。
* {
padding: 0;
margin: 0;
border: 0;
}
html,
body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-weight: 100;
}
.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}
.nav ul {
height: 100%;
width: 100%;
}
.nav > ul > li {
display: inline-block;
list-style: none;
padding: 0 20px;
vertical-align: top
}
.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}
.nav > ul > li > ul {
display: none;
}
.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px;
position:relative
}
.nav li:hover > ul {
display: block;
position:absolute;
top:100%;
width:30%
}
.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}
.nav a:hover {
color: #FFF;
}
@media only screen and (max-device-width: 480px) {}
<body>
<div class="nav">
<ul>
<li>
<a href="#">PAGE</a>
<ul>
<li><a href="#">DROPDOWN</a>
</li>
<li><a href="#">DROPDOWN</a>
</li>
</ul>
</li>
<li><a href="#">PAGE</a>
</li>
<li><a href="#">PAGE</a>
</li>
<li><a href="#">PAGE</a>
</li>
</ul>
</div>
</body>