gpt4 book ai didi

css - 尝试制作响应式菜单,但在::before 和::after 方面遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:23:09 25 4
gpt4 key购买 nike

我有这支笔:https://codepen.io/cesarnascimento/pen/qvLmPG

他有一个红色框,当您单击时,它应该会显示菜单。它工作得很好,我尝试使用 ::before::after 为菜单制作一行,但它不起作用。

这是我的代码:

header {
position: fixed;
width: 100%;
z-index: 999;
}
header .logo {
margin: 0;
}
header .nav-toggle {
display: none;
}
header .nav-toggle:checked ~ nav {
display: block;
}
header nav {
position: absolute;
width: 100%;
text-align: left;
top: 100%;
left: 0;
display: none;
}
header nav ul {
margin: 0;
padding: 0;
list-style: none;
}
header nav ul li {
margin-bottom: 1em;
margin-left: 1em;
}
header nav ul li a {
color: cyan;
text-decoration: none;
font-size: 1.2rem;
text-transform: uppercase;
}
header nav ul li a:hover {
color: #000;
}
header .nav-toggle-label {
position: absolute;
top: 0;
left: 0;
margin-left: 1em;
border: 1px solid red;
height: 100%;
display: flex;
align-items: center;
}
header .nav-toggle-label span {
display: block;
width: 2em;
height: 2px;
border-radius: 2px;
position: relative;
}
header .nav-toggle-label span::before {
content: "";
display: block;
width: 2em;
height: 2px;
border-radius: 2px;
position: absolute;
}
header .nav-toggle-label span::after {
content: "";
display: block;
width: 2em;
height: 2px;
border-radius: 2px;
position: absolute;
}
<header>
<h1 class="logo">Logoe</h1>
<input type="checkbox" id="nav-toggle" class="nav-toggle" />
<nav>
<ul>
<li>
<a href="#">Monitoria</a>
</li>
<li>
<a href="#">Fale comigo</a>
</li>
</ul>
</nav>
<label for="nav-toggle" class="nav-toggle-label">
<span></span>
</label>
</header>

enter image description here

enter image description here

使用 Chrome Developer,我可以看到该行,但它不会出现在我的前面。我在这里缺少什么?

最佳答案

您需要为 ::before::after 元素设置一个 background-color。如果你不添加 background-color 背景是透明的,元素是不可见的:

header {
position: fixed;
width: 100%;
z-index: 999;
}
header .logo {
margin: 0;
}
header .nav-toggle {
display: none;
}
header .nav-toggle:checked ~ nav {
display: block;
}
header nav {
position: absolute;
width: 100%;
text-align: left;
top: 100%;
left: 0;
display: none;
}
header nav ul {
margin: 0;
padding: 0;
list-style: none;
}
header nav ul li {
margin-bottom: 1em;
margin-left: 1em;
}
header nav ul li a {
color: cyan;
text-decoration: none;
font-size: 1.2rem;
text-transform: uppercase;
}
header nav ul li a:hover {
color: #000;
}
header .nav-toggle-label {
position: absolute;
top: 0;
left: 0;
margin-left: 1em;
border: 1px solid red;
height: 100%;
display: flex;
align-items: center;
}
header .nav-toggle-label span {
display: block;
width: 2em;
height: 2px;
border-radius: 2px;
position: relative;
background:#000;
}
header .nav-toggle-label span::before {
content: "";
background:#000;
display: block;
width: 2em;
height: 2px;
border-radius: 2px;
position: absolute;
top:-6px;
}
header .nav-toggle-label span::after {
content: "";
background:#000;
display: block;
width: 2em;
height: 2px;
border-radius: 2px;
position: absolute;
top:6px;
}
<header>
<h1 class="logo">Logoe</h1>
<input type="checkbox" id="nav-toggle" class="nav-toggle" />
<nav>
<ul>
<li>
<a href="#">Monitoria</a>
</li>
<li>
<a href="#">Fale comigo</a>
</li>
</ul>
</nav>
<label for="nav-toggle" class="nav-toggle-label">
<span></span>
</label>
</header>

关于css - 尝试制作响应式菜单,但在::before 和::after 方面遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com