【问题标题】:CSS Problem, checked state doesnt change styleCSS 问题,选中状态不会改变样式
【发布时间】:2021-12-19 23:05:25
【问题描述】:

所以为了简单起见,我想只使用 html 和 css 制作一个响应式导航栏,但我遇到了一个问题,我尝试使用一个复选框来制作一个汉堡菜单,所以当我点击它时,它会显示导航栏在它下面,但是当我单击选中状态时显然不起作用

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;
}

如果你能帮助我,我将不胜感激,我只是一个几乎不知道这些东西的学生

【问题讨论】:

  • 嗨。请将相关 HTML 添加到问题中的 sn-p 以完成问题的重现。
  • 用 HTML 更新了问题

标签: html css checkbox


【解决方案1】:

尝试添加 !important

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

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

【讨论】:

    【解决方案2】:

    您的 CSS 正在尝试定位 #check(这是一个 ID selector),但该复选框没有 id"check"

    另外,general sibling combinator 仅适用于兄弟姐妹。所以#check:checked~.nav-list&lt;input&gt;&lt;label&gt; 周围有一个&lt;div&gt; 时不起作用,因为#check.nav-list 不是兄弟姐妹。

    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>

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-21
      • 2018-08-08
      • 2013-08-30
      • 1970-01-01
      • 2017-11-13
      • 2018-10-16
      • 1970-01-01
      • 2019-11-15
      相关资源
      最近更新 更多