Cod*_*nds 1 css checkbox accordion display
我有一个我喜欢的纯 CSS 手风琴,但我想添加一个功能:我希望标签根据复选框状态而改变,并且在没有 JavaScript 的情况下实现这一点。
所以,
我不在乎你使用什么样的元素本身......你不必让label元素成为改变的元素。但是,解决方案继续允许用户通过单击label元素中的文本或类中的加号plus触发切换来打开手风琴,这一点很重要。答案还需要没有 JavaScript。
一个注意事项:避免将问题文本与伪元素放在一起的解决方案将比这样做的解决方案更受欢迎。原因:我想用first-line样式设置问题文本的第一行,并且因为您不能组合伪元素,所以使用 PE 来放置问题文本会阻止我使用first-line.
我很想看看这是否可以做到!谢谢!这是代码笔:http ://codepen.io/ihatecoding/pen/oZjVPG
这是片段:
@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);
body {
font-family: 'Open Sans';
font-size: 1.5em;
background: #eee;
}
.question-content {
width: 80%;
padding: 20px;
margin: 0 auto;
padding: 0 60px 0 0;
}
.question {
position: relative;
background: #8FBC8F;
margin: 0;
padding: 10px 10px 10px 50px;
display: block;
width:100%;
cursor: pointer;
}
.answers {
background: #999;
padding: 0px 15px;
margin: 5px 0;
height: 0;
overflow: hidden;
z-index: -1;
position: relative;
opacity: 0;
-webkit-transition: .7s ease;
-moz-transition: .7s ease;
-o-transition: .7s ease;
transition: .7s ease;
}
.questions:checked ~ .answers{
height: auto;
opacity: 1;
padding: 15px;
}
.plus {
position: absolute;
margin-left: 10px;
z-index: 5;
font-size: 2em;
line-height: 100%;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
-webkit-transition: .3s ease;
-moz-transition: .3s ease;
-o-transition: .3s ease;
transition: .3s ease;
pointer-events: none;
}
.questions:checked ~ .plus {
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
.questions {
display: none;
}Run Code Online (Sandbox Code Playgroud)
<div class="question-content">
<div>
<input type="checkbox" id="question1" name="q" class="questions">
<div class="plus">+</div>
<label for="question1" class="question">
More Info Q1
</label>
<div class="answers">
What if the answer is really long and wraps the whole page and you never want to finish it but you have to because its the answer!
</div>
</div>
<div>
<input type="checkbox" id="question2" name="q" class="questions">
<div class="plus">+</div>
<label for="question2" class="question">
More Info Q2
</label>
<div class="answers">
short!
</div>
</div>
<div>
<input type="checkbox" id="question3" name="q" class="questions">
<div class="plus">+</div>
<label for="question3" class="question">
More Info Q3
</label>
<div class="answers">
This is the answer!
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
您可以通过使用content如下所示的 CSS属性并删除现有的“更多信息”文本来完成此操作:
.questions ~ .question::before{
content: 'More Info '
}
.questions:checked ~ .question::before{
content: 'Less Info '
}
Run Code Online (Sandbox Code Playgroud)
::before伪元素的使用将确保内容放置在您现有的“Qx”之前,以便您获得所需的外观。
例子
.questions ~ .question::before{
content: 'More Info '
}
.questions:checked ~ .question::before{
content: 'Less Info '
}
Run Code Online (Sandbox Code Playgroud)
@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);
body {
font-family: 'Open Sans';
font-size: 1.5em;
background: #eee;
}
.question-content {
width: 80%;
padding: 20px;
margin: 0 auto;
padding: 0 60px 0 0;
}
.question {
position: relative;
background: #8FBC8F;
margin: 0;
padding: 10px 10px 10px 50px;
display: block;
width: 100%;
cursor: pointer;
}
.answers {
background: #999;
padding: 0px 15px;
margin: 5px 0;
height: 0;
overflow: hidden;
z-index: -1;
position: relative;
opacity: 0;
-webkit-transition: .7s ease;
-moz-transition: .7s ease;
-o-transition: .7s ease;
transition: .7s ease;
}
.questions:checked~.answers {
height: auto;
opacity: 1;
padding: 15px;
}
.questions~.question::before {
content: 'More Info '
}
.questions:checked~.question::before {
content: 'Less Info '
}
.plus {
position: absolute;
margin-left: 10px;
z-index: 5;
font-size: 2em;
line-height: 100%;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
-webkit-transition: .3s ease;
-moz-transition: .3s ease;
-o-transition: .3s ease;
transition: .3s ease;
pointer-events: none;
}
.questions:checked~.plus {
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
.questions {
display: none;
}Run Code Online (Sandbox Code Playgroud)