纯CSS复选框切换菜单

问题描述 投票:1回答:2

我正在尝试创建一个菜单,当选中/取消选中复选框时会切换。我创建了以下html结构:

<header>
  <div>
    <label for="navmenu"><span class="icon-menu"></span></label>
    <input type="checkbox" id="navmenu">
    <nav>
      <ul>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </nav>
  </div>
</header>

[<span>元素包含通过css提供的图标字体中的图标。

这是我写的CSS:

header {
  position: fixed;
  width: 100%;
  text-align: center;
  font-size: 1.25em;
  letter-spacing: 1px;
  line-height: 2em;
  background: #299a97;
  color: white; }
header h1 {
  font-size: 1em;
  font-weight: normal; }
header div {
  width: 2em;
  position: fixed;
  top: 0;
  left: 0; }
header div input[type=checkbox] {
  position: absolute;
  top: 1em;
  left: 1em;
  opacity: 0; }

nav {
  width: 100%;
  position: fixed;
  top: 2.5em;
  display: none; }
nav li {
  height: 2em;
  line-height: 2em;
  padding: 0 1em;
  background: #2eaeab;
  border-bottom: 1px solid #299a97;
  color: white; }
nav li a {
  display: block;
  width: 100%;
  text-decoration: none;
  color: white; }
nav span {
  margin-right: 1em; }

header div input [type = checkbox]:已选中〜导航{显示:块; }

现在,由于某些原因,当我选中此复选框时,导航菜单不会出现。我做错了什么?

html css checkbox nav
2个回答
6
投票
移动navinput[type=checkbox]:checked ~ nav。否则,nav会用display: none覆盖之前的所有内容。

更新


0
投票
在这种类型的菜单上没有js的框外单击时,是否有隐藏隐藏下拉菜单的方法?
© www.soinside.com 2019 - 2024. All rights reserved.