尝试制作响应菜单,但:: before和:: after出现问题

时间:2019-03-22 14:22:17

标签: css

我有一支笔: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,我可以看到该行,但是它不会出现在我的前面。我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

您需要为background-color::before元素设置::after。如果您不添加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>