Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can make responsive menu using html+css with input checkbox?

When the checkbox is clicked, margin:left of nav should increases and enters the screen. When clicked again, it should decrease and go out of the screen. It doesn't work like that. What's the problem with my code?

Html

<input type="checkbox" id="res-nav">
<label for="res-nav"><i class="fa fa-reorder" style="font-size:36px"></i></label>

Style

 body label {
    position: fixed;
    display: block;
    margin-left:-100px; 
    margin-top: -115px; 
    z-index:99;
}

nav{
    margin-left:-130px;
    transition: all 0.5s;
    -moz-transition: all 0.5s;
    -ms-transition: all 0.5s;
    -o-transition: all 0.5s;
    -webkit-transition: all 0.5s;
}

#res-nav:checked ~ nav {
    margin-left:50px;
}
like image 556
Ahmet Yılmaz Avatar asked Feb 01 '26 04:02

Ahmet Yılmaz


2 Answers

label {
    position: fixed;
    display: block;
    margin-left:-100px; 
    margin-top: -115px; 
    z-index:99;
}

nav{
    margin-left:-9999px;
    transition: all 0.5s;
    -moz-transition: all 0.5s;
    -ms-transition: all 0.5s;
    -o-transition: all 0.5s;
    -webkit-transition: all 0.5s;
    background:tomato;
    max-width:100vw;
}

#res-nav:checked ~ nav {
    margin-left:0;
}
<input type="checkbox" id="res-nav">
<label for="res-nav"><i class="fa fa-reorder" style="font-size:36px"></i>s</label>
<nav>Navbar</nav>
like image 95
Martin Lecke Avatar answered Feb 02 '26 17:02

Martin Lecke


Is this what you are looking for ?

.nav{
    margin-left:-120px;
    transition: all 0.5s;
}

#res-nav:checked ~ .nav {
    margin-left: 10px;
}
<input type="checkbox" id="res-nav">
<label for="res-nav" class="nav">
	<i class="fa fa-reorder" style="font-size:36px">Hello</i>
</label>
like image 25
pixlboy Avatar answered Feb 02 '26 17:02

pixlboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!