我正在尝试制作一个响应式标题,当屏幕尺寸小于 992 时,会出现一个汉堡菜单,使用复选框控制菜单。 我试图解决的问题是即使选中复选框,菜单也不会显示。
到目前为止我所拥有的如下。
@import url("https://fonts.googleapis.com/css2?family=Comfortaa:wght@400;700&family=Roboto&display=swap");
html {
height: 100%;
}
body {
height: 100%;
background-color: #fcddc6;
margin: 0;
padding: 0;
}
body::-webkit-scrollbar {
display: none;
}
.header:not(.menu) {
display: flex;
align-items: center;
justify-content: space-between;
}
.logo-image {
margin: 15px;
}
.header-middle ul {
margin: 0;
padding: 0;
display: inline;
justify-content: space-between;
text-transform: uppercase;
list-style: none;
display: flex;
align-items: center;
justify-content: space-between;
}
.header-middle ul a {
color: rgba(42, 19, 19, 1);
text-decoration: none;
margin-left: 25px;
}
.header-middle ul a li {
font-family: 'Comfortaa';
font-weight: 400;
font-size: 25px;
}
#header-right-links {
font-size: 25px;
margin-right: 15px;
}
.hamburger,
.menu-toggle {
display: none;
}
.hamburger {
margin: 0;
position: relative;
width: 25px;
height: 4px;
right: 20px;
background-color: rgba(42, 19, 19, 1);
border-radius: 3px;
cursor: pointer;
z-index: 100;
transition: .3s ease;
}
.hamburger::before,
.hamburger::after {
content: "";
border-radius: 10px;
position: absolute;
height: 4px;
right: 0;
background-color: rgba(42, 19, 19, 1);
transition: .3s ease;
}
.hamburger::before {
top: -10px;
width: 20px;
}
.hamburger::after {
top: 10px;
width: 30px;
}
.menu-toggle {
top: 34px;
right: 20px;
width: 25px;
height: 20px;
position: absolute;
z-index: 1000;
cursor: pointer;
opacity: 0;
}
.menu-toggle:checked+.hamburger {
background: transparent;
}
.menu-toggle:checked+.hamburger:before {
top: 0;
transform: rotate(-45deg);
width: 30px;
}
.menu-toggle:checked+.hamburger:after {
top: 0;
transform: rotate(45deg);
width: 30px;
}
.header .menu-toggle:checked+.header-middle,
.menu-toggle:checked~.menu,
.menu-toggle:checked+.header-right {
right: 0;
opacity: 1;
}
@media (max-width:992px) {
.menu-toggle,
.hamburger {
display: block;
}
.header-middle,
.header-right {
display: flex;
flex-direction: column;
align-items: center;
justify-content: start;
position: fixed;
top: 85px;
width: 300px;
height: 800px;
right: -300px;
padding-top: 30px;
background-color: #dc9a7f;
border-width: 1px;
border-color: #dc9a7f;
border-style: solid;
}
.menu {
display: flex;
flex-direction: column;
align-items: center;
justify-content: start;
position: fixed;
}
.header-middle li {
width: 100%;
}
.menu a {
margin: 0;
display: flex;
align-items: center;
justify-content: flex-start;
padding: 25px;
font-size: 20px;
border-width: 1px;
box-shadow: 0 1px 0 0 rgba(255, 255, 245, .5) inset;
}
.header-middle ul li:hover {
color: white;
}
.header-right {
background: transparent;
height: auto;
top: 455px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
}
}
<script src="https://kit.fontawesome.com/d92fc73161.js" crossorigin="anonymous"></script>
<div class="header">
<div class="header-left">
<a class="logo-link" href="https://www.example.com/"><img class="logo-image" src="https://picsum.photos/30/30" alt="" /></a>
</div>
<div class="menu">
<div class="header-middle">
<ul class="menu">
<a href="https://www.example.com/">
<li>Home</li>
</a>
<a href="https://www.example.com/">
<li>Products</li>
</a>
<a href="https://www.example.com/">
<li>About Us</li>
</a>
<a href="https://www.example.com/">
<li>Contact Us</li>
</a>
</ul>
</div>
</div>
<div class="header-right">
<i id="header-right-links" class="fa-solid fa-magnifying-glass"></i>
<i id="header-right-links" class="fa-regular fa-heart"></i>
<i id="header-right-links" class="fa-solid fa-cart-shopping"></i>
</div>
<input type="checkbox" class="menu-toggle">
<div class="hamburger"></div>
</div>
我正在努力寻找解决方案,并且不知道如何继续。
错误是由于 CSS 中元素的定位和 z-index 导致的
.header-middle,
.header-right {
display: flex;
flex-direction: column;
align-items: center;
justify-content: start;
position: fixed;
top: 85px;
width: 300px;
height: 800px;
right: -300px;
padding-top: 30px;
background-color: #dc9a7f;
border-width: 1px;
border-color: #dc9a7f;
border-style: solid;
z-index: 10;
transition: right 0.3s ease;
}
.menu {
display: flex;
flex-direction: column;
align-items: center;
justify-content: start;
position: fixed;
transition: transform 0.3s ease;
transform: translateX(100%);
z-index: 5;
}
.header-middle li {
width: 100%;
}
.menu a {
margin: 0;
display: flex;
align-items: center;
justify-content: flex-start;
padding: 25px;
font-size: 20px;
border-width: 1px;
box-shadow: 0 1px 0 0 rgba(255, 255, 245, .5) inset;
}
.header-middle ul li:hover {
color: white;
}
.header-right {
background: transparent;
height: auto;
top: 455px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
}
}
试试这个...可能会有帮助