Inad Islam
Inad Islam

Reputation: 35

Open close sidenav same button

I want to make this code responsive. It works on mobile perfectly, but when I click it on desktop, it does not work. I want to make that sidebar like wp. When I am on mobile it will show like this code, but when I click it on a desktop, it will show like wp dashboard.

body {
  margin: 0;
}
.sidebar-toggle {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  padding: 10px;
  background: red;
  color: white;
}
#sidebar-toggle-input {
  display: none;
}
#sidebar-toggle-input+label:before {
  content: "➡️";
}
#sidebar-toggle-input:checked + label:before {
  content: "❌"
}
.sidebar {
  position: fixed;
  width: 200px;
  transition: 0.3s;
  background: blue;
  height: 100vh;
  top: 0;
  left: 0;
  transform: translateX(-100%);
}
#sidebar-toggle-input:checked ~ .sidebar {
  transform: none;
}
<input type="checkbox" id="sidebar-toggle-input" />
<label class="sidebar-toggle" for="sidebar-toggle-input"></label>
<div class="sidebar"></div>

Here is a Codepen of my project

Upvotes: 1

Views: 269

Answers (1)

Khaled Mahfoz
Khaled Mahfoz

Reputation: 95

i'm sorry i don't think i get the question but i assume that's what you want so here i add two different kind of nav and using media query i changed the display of each of them

body {
  margin: 0;
  background-color: #ddd;
}
.sidebar-toggle {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  padding: 10px;
  background: red;
  color: white;
}
#sidebar-toggle-input {
  display: none;
}
#sidebar-toggle-input+label:before {
  content: "➡️";
}
#sidebar-toggle-input:checked + label:before {
  content: "❌"
}
.sidebar {
  position: fixed;
  width: 200px;
  transition: 0.3s;
  background: blue;
  height: 100vh;
  top: 0;
  left: 0;
  transform: translateX(-100%);
}
#sidebar-toggle-input:checked ~ .sidebar {
  transform: none;
}
.sidebar ul{
  padding: 0;
  margin: 0;
  color: #fff;
  margin-top: 50px;
  text-align: center;
}
.sidebar ul li{
  margin-top: 10px;
  cursor: pointer;
  font-size: 20px;
  display: inline;
}

@media only screen and (min-width: 700px){
  .sidebar-toggle-input, .sidebar-toggle{
    display: none;
  }
  .sidebar{
    width: 100px;
    transform: translatex(0);
  }
  .sidebar ul div li{
    display: none;
  }
  .sidebar ul div span{
    font-size: 2rem;
  }
}
@media only screen and (min-width: 900px){
.sidebar{
    width: 300px;
  }
  .sidebar ul div li{
    display: block;
  }
}
<input type="checkbox" id="sidebar-toggle-input" />
<label class="sidebar-toggle" for="sidebar-toggle-input"></label>
<div class="sidebar">
  <ul>
    <div><span>&#9731;</span><li>home</li></div>
    <div><span>&#9731;</span><li>about</li></div>
    <div><span>&#9731;</span><li>contact</li></div>
  </ul>
</div>

Upvotes: 3

Related Questions