Home > Software engineering >  CSS Problem, checked state doesnt change style
CSS Problem, checked state doesnt change style

Time:11-06

So to make it simple i want to make a responsive nav-bar using only html and css but ive encountered a problem, i trying to use a checkbox to make a hamburger menu so when i click it it will display the nav bar below it but when i click the checked state apparently dont work

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>King Toot's Tienda de instrumentos</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script src="https://kit.fontawesome.com/f436282179.js" crossorigin="anonymous"></script>
</head>
<body>
    <header>
        <nav>
            <div class="logo-container">
                <img src="">
                <label class="logo-txt">King Toot's</label>
            </div>
        
            <div>
                <input type="checkbox" name="check">
                <label for="check" class="hamburger-btn">
                    <i class="fas fa-bars"></i>
                </label>
            </div>
            <ul class="nav-list">
                <li><a href="#">Instrumentos</a></li>
                <li><a href="#">Marcas</a></li>
                <li><a href="#">Contacto</a></li>
            </ul>
        </nav>
    </header>
    <section></section>
    <section></section>
    <footer></footer>
</body>
</html>

CSS

*{
    padding: 0;
    margin: 0;
    text-decoration: none;
    list-style: none;
    box-sizing: border-box;
    
}
@font-face{
    font-family: slenco;
    src: url(../slenco.otf);
}

body{
    font-family: slenco, sans-serif;
}

/*Navbar*/
nav{
    height: 75px;
    width: 100%;
    background-color: #202020;
    display: flex;
    justify-content: space-between;
}
.logo-container{
    display: flex;
    align-items: center;
    width: auto;
}
.logo{
    margin-left: 2px;
}
.logo-txt{
    font-size: 30px;
    line-height: 75px;
    padding-left: 10px;
    font-weight: bold;
    color: white;
}
.nav-list{
    position: fixed;
    width: 100%;
    height: 0vh;
    top: 75px;
    background-color: #2f2f2f;
    float: right;
    text-align: center;
    transition: all .5s;
}
.nav-list li{
    display: none;
    line-height: 30px;
    margin: 50px 0;
    transition: all .5s;
}
.nav-list li a{
    color: white;
    font-size: 20px;
    text-transform: uppercase;
}
.nav-list li a:hover{
    color: #3f3f3f;
    transition: 0.5s;
}
.hamburger-btn{
    display: block;
    font-size: 30px;
    color: white;
    float: right;
    line-height: 75px;
    margin-right: 45px;
    cursor: pointer;
}
#check{
    display: none;
}

#check:checked ~ .nav-list{
    height: 100vh;
}
#check:checked ~ .nav-list li{
    display: block;
}

i will be grateful if u can help me im only a student barely know any of this stuff

CodePudding user response:

Your CSS is attempting to target #check (which is an ID selector) but the checkbox does not have an id of "check".

Also, the general sibling combinator only works on siblings. So #check:checked~.nav-list does not work while there is a <div> around the <input> and <label>, because #check and .nav-list are not siblings.

nav {
  width: 100%;
  background-color: #202020;
  display: flex;
  justify-content: space-between;
}

.nav-list {
  list-style: none;
  position: fixed;
  width: 100%;
  height: 0vh;
  top: 75px;
  background-color: #2f2f2f;
  transition: all .5s;
}

.nav-list li {
  display: none;
}

.hamburger-btn {
  font-size: 30px;
  color: white;
  line-height: 75px;
}

#check {
  display: none;
}

#check:checked~.nav-list {
  height: 100vh;
}

#check:checked~.nav-list li {
  display: block;
}
<script src="https://kit.fontawesome.com/f436282179.js" crossorigin="anonymous"></script>
<header>
  <nav>
    <input id="check" type="checkbox">
    <label for="check" class="hamburger-btn"><i class="fas fa-bars"></i></label>
    <ul class="nav-list">
      <li><a href="#">Instrumentos</a></li>
      <li><a href="#">Marcas</a></li>
      <li><a href="#">Contacto</a></li>
    </ul>
  </nav>
</header>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Demo

CodePudding user response:

try adding !important

#check:checked ~ .nav-list{
    height: 100vh !important;
}

#check:checked ~ .nav-list li{
    display: block !important;
}
  • Related