[英]Toggle using only css - select the next element that has a specific class
I need when the Trigger is clicked(checkbox checked) that the content(ul) to appear as a horizontal list, below the wrapper(red and blue colors). 我需要单击“触发器”(选中复选框)时,内容(ul)以水平列表的形式出现在包装器的下方(红色和蓝色)。
Looks like the +
(for next) doesn't work because even if is the next element, it is in a different parent. 看起来
+
(下一个)不起作用,因为即使下一个元素也位于另一个父级中。
.wrapper { position: relative; height: 130px; background: red; } .lp { color: $white; position: absolute; height: 100%; top: 0; right: 0; background: blue; } .lp .toggle { position: relative; top: 50%; transform: translateY(-50%); padding: 20px; } .trigger { display:none;} .content { display: none; } .trigger:checked + .content { display: block; }
<div class="wrapper"> <div class="lp"> <div class="toggle"> <label for="menu-toggle"> <span>Trigger</span> </label> <input type="checkbox" class="trigger" id="menu-toggle"/> </div> <ul class="content"> <li><a href="#">First link</a></li> <li><a href="#">Second link</a></li> <li><a href="#">Third link</a></li> </ul> </div> </div>
Updated. 更新。 So I've made a quick demo.
所以我做了一个快速演示。 Hopefully this is enough to get you going :)
希望这足以使您前进:)
.checkbox:not(checked) { display: none; } .checkbox:not(checked) ~ .content { display: none; } .checkbox:checked ~ .content { display: block; }
<div class="wrapper"> <div class="lp"> <input type="checkbox" value="selected" id="1" class="checkbox"/> <label for="1" >Trigger</label> <ul class="content"> <li><a href="#">First link</a></li> <li><a href="#">Second link</a></li> <li><a href="#">Third link</a></li> </ul> </div> </div>
All you need to do, with the posted code of yours, is to move the input
so it becomes the previous sibling of the ul
. 使用您已发布的代码,所有您需要做的就是移动
input
,使其成为ul
的上一个兄弟。
With that the adjacent sibling selector +
will work properly and toggle the hidden menu 这样, 相邻的兄弟选择器
+
将可以正常工作并切换隐藏的菜单
Stack snippet 堆栈片段
.wrapper { position: relative; height: 150px; background: red; } section { position: relative; } section .content { background: yellow; padding: 20px; } .lp { color: $white; position: absolute; height: 100%; width: 100px; top: 0; right: 0; background: lightblue; } .lp .toggle { position: relative; top: 50%; transform: translateY(-50%); padding: 20px; } .lp .content { position: relative; transform: translateY(15px); padding-left: 18px; list-style: none; } .content, .trigger { display: none; } .trigger:checked+.content { display: block; }
<div class="wrapper"> <div class="lp"> <div class="toggle"> <label for="menu-toggle"> <span>Trigger</span> </label> </div> <input type="checkbox" class="trigger" id="menu-toggle" /> <ul class="content"> <li><a href="#">First link</a></li> <li><a href="#">Second link</a></li> <li><a href="#">Third link</a></li> </ul> </div> </div>
As mentioned in a comment, if you have content that is sibling of your .wrapper
and also needs to be toggled, you can instead use the general sibling selector ~
. 如评论中所述,如果您的内容与
.wrapper
兄弟姐妹.wrapper
并且也需要切换,则可以使用常规的兄弟姐妹选择器 ~
。
For the input
to be able to "see" eg both these elements, put it before the .wrapper
. 为了使
input
能够“看到”这两个元素,请将其放在.wrapper
之前。
Stack snippet 堆栈片段
.wrapper { position: relative; height: 150px; background: red; } section { position: relative; } section .content { background: yellow; padding: 20px; } .lp { color: $white; position: absolute; height: 100%; width: 100px; top: 0; right: 0; background: lightblue; } .lp .toggle { position: relative; top: 50%; transform: translateY(-50%); padding: 20px; } .lp .content { position: relative; transform: translateY(15px); padding-left: 18px; list-style: none; } .content, .trigger { display: none; } .trigger:checked ~ .wrapper .content, .trigger:checked ~ section .content { display: block; }
<input type="checkbox" class="trigger" id="menu-toggle" /> <div class="wrapper"> <div class="lp"> <div class="toggle"> <label for="menu-toggle"> <span>Trigger</span> </label> </div> <ul class="content"> <li><a href="#">First link</a></li> <li><a href="#">Second link</a></li> <li><a href="#">Third link</a></li> </ul> </div> </div> <section> <div class="content"> Some other dummy text that will show... </div> </section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.