I'm creating responsive navbar for my website, and I came across to this simple looking problem. Whenever I'm clicking on hamburger menu icon nothing happens. My idea is -- whenever hamburger icon is clicked, menu appear/disappear, I even made transition in CSS line 75 (transition: all .5s;)
I tried to go over my code multiple times, and I still don't see reason why is this not working. Here is my HTML and CSS code (with checkbox, I tried to avoid using JS for now):
<!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;
}
}
I would really appreciate working answer, since I'm dealing with this problem for some time. Thanks in advance :)
You forgot a left: -100%
at the 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>