这是我到目前为止的摘要:
#expand {
height: 0px;
width: 0px;
overflow: hidden;
transition: height 0.5s, width 0.5s ease-in-out;
background: #000000;
color: #FFF;
}
input {
display: none;
visibility: hidden;
}
input:checked+label+#expand {
height: 150px;
width: 600px;
}
#toggle:checked~label::before {
content: "-";
}
/* useless styling */
main {
background: #EEE;
width: 100px;
margin: 20px auto;
padding: 10px 0;
box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}
section {
padding: 0 20px;
}
label {
display: block;
padding: 0.5em;
text-align: center;
border-bottom: 1px solid #CCC;
color: #666;
}
label:hover {
color: #000;
}
label::before {
font-family: Consolas, monaco, monospace;
font-weight: bold;
font-size: 15px;
content: "+";
vertical-align: text-top;
display: inline-block;
width: 20px;
height: 20px;
margin-right: 3px;
background: radial-gradient(ellipse at center, #CCC 50%, transparent 50%);
}
<main>
<input id="toggle" type="checkbox" />
<label for="toggle">Hidden Kitten</label>
<div id="expand">
<section>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>
</div>
<input id="toggle1" type="checkbox" />
<label for="toggle1">Hidden Kitten</label>
<div id="expand">
<section>
<p>mew</p>
</section>
</div>
</main>
您可以在input:checked + label + #expand
选择器中看到宽度和高度是固定的。
我可以将宽度设置为一个很好的百分比,但是如果我尝试将高度设置为一个百分比,则会破坏动画。我该如何解决?我事先不知道文字的高度。我只想为此使用CSS。
答案 0 :(得分:1)
您可以尝试使用max-height
代替height
。
#expand {
max-height: 0px;
width: 0px;
overflow: hidden;
transition: max-height 0.5s, width 0.5s ease-in-out;
background: #000000;
color: #FFF;
}
input {
display: none;
visibility: hidden;
}
input:checked+label+#expand {
max-height: 150px;
width: 600px;
}
#toggle:checked~label::before {
content: "-";
}
/* useless styling */
main {
background: #EEE;
width: 100px;
margin: 20px auto;
padding: 10px 0;
box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}
section {
padding: 0 20px;
}
label {
display: block;
padding: 0.5em;
text-align: center;
border-bottom: 1px solid #CCC;
color: #666;
}
label:hover {
color: #000;
}
label::before {
font-family: Consolas, monaco, monospace;
font-weight: bold;
font-size: 15px;
content: "+";
vertical-align: text-top;
display: inline-block;
width: 20px;
height: 20px;
margin-right: 3px;
background: radial-gradient(ellipse at center, #CCC 50%, transparent 50%);
}
<main>
<input id="toggle" type="checkbox" />
<label for="toggle">Hidden Kitten</label>
<div id="expand">
<section>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>
</div>
<input id="toggle1" type="checkbox" />
<label for="toggle1">Hidden Kitten</label>
<div id="expand">
<section>
<p>mew</p>
</section>
</div>
</main>