我正在为我的网站创建响应式导航栏,我遇到了这个看起来简单的问题。每当我单击汉堡菜单图标时,什么也没有发生。我的想法是——每当单击汉堡包图标时,菜单就会出现/消失,我什至在 CSS 第 75 行中进行了转换(转换:全部 .5s;)
我多次尝试检查我的代码,但我仍然不明白为什么这不起作用。这是我的 HTML 和 CSS 代码(带有复选框,我现在尝试避免使用 JS):
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="https://kit.fontawesome.com/a4b0d61691.js"></script>
</head>
<body>
<nav>
<input type="checkbox" id="check">
<label for="check" class="checkbtn">
<i class="fas fa-bars" area-hidden="true"></i>
</label>
<label class="logo">Test</label>
<ul>
<li><a href="http://localhost/list">Test1</a></li>
<li><a href="http://localhost/about">Test2</a></li>
<li><a href="http://localhost/faq">Test3</a></li>
<li><a href="http://localhost/contact">Test4</a></li>
</ul>
</nav>
</body>
</html>
*{
padding: 0;
margin: 0;
text-decoration: none;
list-style: none;
box-sizing: border-box;
}
body{
font-family: "Roboto","Arial",sans-serif;
}
nav{
background: #4287f5;
height: 80px;
width: 100%;
}
label.logo{
color: white;
font-size: 35px;
line-height: 80px;
padding: 0 100px;
font-weight: bold;
cursor: pointer;
}
nav ul{
float: right;
margin-right: 20px;
}
nav ul li{
display: inline-block;
line-height: 80px;
margin: 0 5px;
}
nav ul li a{
color: white;
font-size: 17px;
text-transform: uppercase;
}
a.active,a:hover{
background: #57aef2;
transition: .5s;
}
.checkbtn{
font-size: 30px;
color: white;
float: right;
line-height: 80px;
margin-right: 40px;
cursor: pointer;
display: none;
}
#check{
display: none;
}
@media (max-width: 952px){
label.logo{
font-size: 30px;
padding-left: 50px;
}
nav ul li a{
font-size: 16px;
}
}
@media (max-width: 858px){
.checkbtn{
display: block;
}
ul{
position: fixed;
width: 100%;
height: 100vh;
background: #1b354a;
top: 80px;
left: 0;
text-align: center;
transition: all .5s;
}
nav ul li{
display: block;
margin: 50px 0;
line-height: 30px;
}
nav ul li a{
font-size: 20px;
}
a:hover,a.active{
background: none;
color: #57aef2;
}
#check:checked ~ ul{
left: 0;
}
}
我真的很感激工作答案,因为我已经处理这个问题有一段时间了。预先感谢:)
您在
left: -100%
忘记了 ul
。
*{
padding: 0;
margin: 0;
text-decoration: none;
list-style: none;
box-sizing: border-box;
}
body{
font-family: "Roboto","Arial",sans-serif;
}
nav{
background: #4287f5;
height: 80px;
width: 100%;
}
label.logo{
color: white;
font-size: 35px;
line-height: 80px;
padding: 0 100px;
font-weight: bold;
cursor: pointer;
}
nav ul{
float: right;
margin-right: 20px;
}
nav ul li{
display: inline-block;
line-height: 80px;
margin: 0 5px;
}
nav ul li a{
color: white;
font-size: 17px;
text-transform: uppercase;
}
a.active,a:hover{
background: #57aef2;
transition: .5s;
}
.checkbtn{
font-size: 30px;
color: white;
float: right;
line-height: 80px;
margin-right: 40px;
cursor: pointer;
display: none;
}
#check{
display: none;
}
@media (max-width: 952px){
label.logo{
font-size: 30px;
padding-left: 50px;
}
nav ul li a{
font-size: 16px;
}
}
@media (max-width: 858px){
.checkbtn{
display: block;
}
ul{
position: fixed;
width: 100%;
height: 100vh;
background: #1b354a;
top: 80px;
left: -100%;
text-align: center;
transition: all .5s;
}
nav ul li{
display: block;
margin: 50px 0;
line-height: 30px;
}
nav ul li a{
font-size: 20px;
}
a:hover,a.active{
background: none;
color: #57aef2;
}
#check:checked ~ ul{
left: 0;
}
}
<script src="https://kit.fontawesome.com/a4b0d61691.js"></script>
<nav>
<input type="checkbox" id="check">
<label for="check" class="checkbtn">
<i class="fas fa-bars" area-hidden="true"></i>
</label>
<label class="logo">Test</label>
<ul>
<li><a href="http://localhost/list">Test1</a></li>
<li><a href="http://localhost/about">Test2</a></li>
<li><a href="http://localhost/faq">Test3</a></li>
<li><a href="http://localhost/contact">Test4</a></li>
</ul>
</nav>