CSS 问题,选中状态不会改变样式
CSS Problem, checked state doesnt change style
所以为了简单起见,我想只使用 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;
}
如果你能帮助我,我将不胜感激,我只是一个对这些东西几乎一无所知的学生
尝试添加 !important
#check:checked ~ .nav-list{
height: 100vh !important;
}
#check:checked ~ .nav-list li{
display: block !important;
}
您的 CSS 正在尝试以 #check
为目标(这是一个 ID selector),但复选框没有 id
"check"
。
此外,general sibling combinator 仅适用于兄弟姐妹。所以 #check:checked~.nav-list
在 <input>
和 <label>
周围有 <div>
时不起作用,因为 #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>
所以为了简单起见,我想只使用 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;
}
如果你能帮助我,我将不胜感激,我只是一个对这些东西几乎一无所知的学生
尝试添加 !important
#check:checked ~ .nav-list{
height: 100vh !important;
}
#check:checked ~ .nav-list li{
display: block !important;
}
您的 CSS 正在尝试以 #check
为目标(这是一个 ID selector),但复选框没有 id
"check"
。
此外,general sibling combinator 仅适用于兄弟姐妹。所以 #check:checked~.nav-list
在 <input>
和 <label>
周围有 <div>
时不起作用,因为 #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>