菜单仅使用CSS内部的选项切换



我正在尝试创建一个带有选项的菜单。我仅使用checkboxradio输入的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的选项之一时,是否可以关闭菜单?

有时与大众见解相反,使用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">&equiv; Open</a><a href="#" class="close">&equiv; 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">&equiv; Open</a>
    <a href="#" class="close">&equiv; Close</a>
  </div>   
</div>

最新更新