我正在尝试创建一个带有内部选项的菜单。我只使用带有checkbox
和radio
输入的CSS。
通过更改选项之一,我也希望菜单关闭。我在label
内使用label
进行了尝试,但是它不起作用。
我的原型代码:
input {
display: none;
}
label {
cursor: pointer;
}
label span:hover {
font-weight: 600;
}
.opener .menu {
background-color: #f3f3f3;
display: flex;
flex-direction: column;
color: #4d4d4d;
padding: 12px 4px;
width: 270px;
}
#menu:checked~.opener .menu {
display: none;
}
#menu~.opener>span:nth-of-type(1) {
display: none;
}
#menu:~.opener>span:nth-of-type(2) {
display: block;
}
#menu:checked~.opener>span:nth-of-type(1) {
display: block;
}
#menu:checked~.opener>span:nth-of-type(2) {
display: none;
}
.box {
height: 50px;
width: 50px;
margin: 20px 0;
}
#red:checked~.box {
background-color: red;
}
#blue:checked~.box {
background-color: blue;
}
#green:checked~.box {
background-color: green;
}
<input id="menu" type="checkbox"></input>
<input id="red" type="radio" name="opcoes" checked></input>
<input id="blue" type="radio" name="opcoes"></input>
<input id="green" type="radio" name="opcoes"></input>
<label class="opener" for="menu"><span>Open</span><span>Close</span>
<div class="menu">
<label for="red"><span>red</span></label>
<label for="blue"><span>blue</span></label>
<label for="green"><span>green</span></label>
</div>
</label>
<div class="box"></div>
或者您可以在此处查看:https://codepen.io/anon/pen/JxzPKR
当您单击其中一个没有JavaScript的选项时,是否可以关闭菜单?
答案 0 :(得分:4)
有时候,与流行观点相反,使用Javascript对开发人员更友好。
对于纯CSS解决方案来说,有太多的条件逻辑。在保持样式级联的同时,您必须满足约3个if-then-else
条件。我认为要满足的最艰巨的任务是,除了要切换它的其他控件之外,还要有一个切换标题。
从本质上讲,这将使您添加的更多组件变得更加复杂和复杂。但是这里是一个使用:target
的示例。这是一种变通办法,可以解决当前的问题。您将无法以这种方式“切换”菜单,因此我不得不在元素下添加标题,以便可以通过某种同级选择器对其进行访问:
.menu {
position: relative;
width: 45%;
}
input[type="radio"] {
position: absolute;
opacity: 0;
height: 0;
width: 0;
}
a:any-link {
all: unset;
}
.menu-header {
position: absolute;
top: 0;
padding: 5px 10px;
color: white;
width: 100%;
background-color: cornflowerblue;
}
.menu-header a {
font-weight: bold;
cursor: pointer;
color: white;
font-size: 22px;
}
.menu-header .close {
display: none;
}
#menu-body {
display: none;
flex-flow: column nowrap;
position: absolute;
top: 34px;
background-color: rgba(220,220,220,1);
height: 100px;
color: black;
width: 100%;
padding: 10px;
}
.menu-header a,
#menu-body label {
cursor: pointer;
}
#menu-body:not(:target) {
display: none;
}
#menu-body:not(:target) + .menu-header > a:not(.close) {
display: inline-block;
}
#menu-body:target {
display: flex;
}
#menu-body:target + .menu-header > a {
display: none;
}
#menu-body:target + .menu-header > a.close {
display: inline-block;
}
<div class="menu">
<div id="menu-body">
<input id="red" type="radio" name="opcoes" checked/>
<label for="red"><a href="#">Red</a></label>
<input id="blue" type="radio" name="opcoes"/>
<label for="blue"><a href="#">Blue</a></label>
<input id="green" type="radio" name="opcoes"/>
<label for="green"><a href="#">Green</a></label>
</div>
<div class="menu-header"><a href="#menu-body">≡ Open</a><a href="#" class="close">≡ Close</a></div>
</div>
您应该考虑使用此方法的可访问性,或者至少考虑它如何影响网站导航。
编辑:有关评论中讨论的演示:
.menu {
position: relative;
width: 45%;
}
input[type="radio"] {
position: absolute;
opacity: 0;
height: 0;
width: 0;
}
a:any-link {
all: unset;
}
#menu-header {
position: absolute;
top: 0;
padding: 5px 10px;
color: white;
width: 100%;
background-color: cornflowerblue;
}
#menu-header a {
font-weight: bold;
cursor: pointer;
color: white;
font-size: 22px;
}
#menu-header .close {
display: none;
}
#menu-body {
display: none;
flex-flow: column nowrap;
position: absolute;
top: 34px;
background-color: rgba(220,220,220,1);
height: 100px;
color: black;
width: 100%;
padding: 10px;
}
.menu-header a,
#menu-body label {
cursor: pointer;
}
#menu-body:not(:target) {
display: none;
}
#menu-body:not(:target) ~ .menu-header > a:not(.close) {
display: inline-block;
}
#menu-body:target {
display: flex;
}
#menu-body:target ~ #menu-header > a {
display: none;
}
#menu-body:target ~ #menu-header > a.close {
display: inline-block;
}
#red:target ~ .box {
background-color: red;
}
#blue:target ~ .box {
background-color: blue;
}
#green:target ~ .box {
background-color: green;
}
.box {
background-color: black;
width: 75px; height : 75px;
}
<div class="menu">
<input id="red" type="radio" name="opcoes" checked/>
<input id="blue" type="radio" name="opcoes"/>
<input id="green" type="radio" name="opcoes"/>
<div id="menu-body">
<a href="#red">Red</a>
<a href="#blue">Blue</a>
<a href="#green">Green</a>
</div>
<div class="box"></div>
<div id="menu-header">
<a href="#menu-body">≡ Open</a>
<a href="#" class="close">≡ Close</a>
</div>
</div>