First of all, please note that once the checkbox is checked, you are re-setting the height
, line-height
and overflow
properties. Hence the animation doesn't have any effect in this case.
Also, height: 100%
doesn't affect the element as its parent doesn't have an explicit height
. So you'll need to use a fixed value for height property.
In addition, CSS animation is unable to animate the overflow
property, and line-height
properly (specially by inherit
value).
In order to stop the animation at the end keyframe, you should use animation-fill-mode
property with forwards
value.
Here is the shorthand version:
.accordeon-checkbox:checked ~ .accordeon-content {
-webkit-animation: slide-up 1s ease forwards;
/* Safari 4+ */
-moz-animation: slide-up 1s ease forwards;
/* Fx 5+ */
-o-animation: slide-up 1s ease forwards;
/* Opera 12+ */
animation: slide-up 1s ease forwards;
/* IE 10+ */
}
Demo.
An issue with closing the panel
But there's an issue with using animations. Since the animation is applied to the element only when the checkbox is checked, Once you uncheck the checkbox by the second click, the panel will be closed immediately without any animations.
Hence, it's better to use transitions to achieve the desired effect.
Here you go:
.accordeon-content {
height: 0;
overflow:hidden;
background-color: gold;
/* other vendor-prefixes here... */
transition: height 1s ease;
}
.accordeon-checkbox:checked ~ .accordeon-content {
height: 400px;
}