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

Aza*_*zin 5 html css

我正在尝试创建一个带有选项的菜单。我只使用 CSScheckboxradio输入。

通过更改选项之一,我还希望关闭菜单。我尝试使用labelinside 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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

或者你可以在这里查看:https : //codepen.io/anon/pen/JxzPKR

当您单击没有 JavaScript 的选项之一时,有没有办法关闭菜单?

sou*_*ned 4

有时,与流行的观点相反,使用 Javascript 对开发人员更友好。

这是一个纯 CSS 解决方案,有太多的条件逻辑。if-then-else在保持样式层叠的同时,您必须满足大约 3 个条件。我认为要满足的最艰巨的任务是除了切换标题的其他控件之外,还有一个切换标题。

添加的组件越多,这本质上就会变得更加复杂和复杂。但这里有一个使用的例子: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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

您应该考虑使用此方法的可访问性,或者至少考虑这如何影响网站导航。


编辑:关于评论中讨论的演示:

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)