[英]How to display another div when clicked on a div using CSS only
I want to use CSS only to show a div when clicked on another div 我只想在单击另一个div时使用CSS来显示div
Here is my HTML code 这是我的HTML代码
<div id="contentmenu">
<div class="show">
<a class="show">
My Student
</a>
</div>
<div class="hidden">
<a href="#">Link 1</a><br/>
<a href="#">Link 2</a><br/>
<a href="#">Link 3</a><br/>
</div>
</div>
My CSS code is 我的CSS代码是
#contentmenu{
margin-top: 79px;
background-color: #E9D4B5;
width: 25%;
height: 100%;
}
.show{
margin-top: 2%;
background-color: #CE9127;
width: 96%;
height: 10%;
padding-left: 4%;
color: white;
text-decoration: none;
}
.hidden{
display: none;
padding-left: 8%;
margin-top: 1%;
}
So how to display div (class = hidden) when clicked on the div (class = show) using CSS only 那么如何仅使用CSS在div(class = show)上单击时如何显示div(class = hidden)
You could do it with the Checkbox Hack , though be sure to make sure browser support is sufficient. 您可以使用Checkbox Hack来做到这一点,尽管一定要确保浏览器支持足够。 It would look like this: 它看起来像这样:
#contentmenu{
margin-top: 79px;
background-color: #E9D4B5;
width: 25%;
height: 100%;
}
.show{
margin-top: 2%;
background-color: #CE9127;
width: 96%;
height: 10%;
padding-left: 4%;
color: white;
text-decoration: none;
display: block;
}
.hidden{
display: none;
padding-left: 8%;
margin-top: 1%;
}
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}
input[type=checkbox]:checked ~ .hidden {
display: block;
}
with this HTML: 与此HTML:
<div id="contentmenu">
<label for="toggle-1"><a class="show">
My Student
</a></label>
<input type="checkbox" id="toggle-1">
<div class="hidden">
<a href="#">Link 1</a><br/>
<a href="#">Link 2</a><br/>
<a href="#">Link 3</a><br/>
</div>
</div>
Here is a demo: http://jsbin.com/exuvez/1/edit 这是一个演示: http : //jsbin.com/exuvez/1/edit
For no hacks, and no javascript you can achieve what you want with a hover event. 没有技巧,也没有JavaScript,您可以使用悬停事件来实现所需的功能。 Just a small change and addition to the css: 只是对CSS进行了少量更改和添加:
#contentmenu{
margin-top: 79px;
background-color: #E9D4B5;
width: 25%;
height: 100%;
}
.show{
margin-top: 2%;
background-color: #CE9127;
width: 96%;
height: 10%;
padding-left: 4%;
color: white;
text-decoration: none;
}
.hidden{
display: none;
padding-left: 8%;
margin-top: 1%;
width:100%;
}
.show:hover + .hidden {
display:block;
}
.hidden:hover {
display:block;
}
So width:100% has been added to .hidden, and two rules for the hover event. 因此,width:100%已添加到.hidden中,并且有两个用于悬停事件的规则。
http://codepen.io/anon/pen/eCmox http://codepen.io/anon/pen/eCmox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.