ddu*_*upp 1 html css checkbox nav
我正在尝试创建一个菜单,在选中/取消选中复选框时切换.我创建了以下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>
Run Code Online (Sandbox Code Playgroud)
该<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; }
Run Code Online (Sandbox Code Playgroud)
header div输入[type = checkbox]:checked~nav {display:block; }
现在,出于某种原因,当我选中复选框时,导航菜单不会出现.我做错了什么?
移动nav
和input[type=checkbox]:checked ~ nav
左右.否则nav
覆盖之前的所有内容display: none
.
更新
只是详细说明.出于某种原因,当绝对定位时,复选框不会取消选中.这没关系,因为您无论如何都不需要查看复选框,因此我们根本无法显示它并让您的标签检查并取消选中我们隐藏的复选框.
有一个小提琴 - 小提琴链接!
更新了隐藏的复选框
CSS
input[type=checkbox] {
display: none;
}
nav {
position: fixed;
display: none;
}
input[type=checkbox]:checked ~ nav {
display: block;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7795 次 |
最近记录: |