The demo does not seem to work in Chrome problem is because the height of the from keyframe is 100%
but that means 100%
of the containing element (in this case <body>
) but there is no height set on the <body>
. Two solutions to this, either set:
body {
height:100px;
}
or
@-webkit-keyframes anim {
from {
height:100px;
}
to {
height:0;
}
}
The actual moving elements problem is because the elements are display:inline-block
but there is no vertical-align
set, so the browser is using the default vertical-align:baseline
to align the elements to. Since the animation properties of the cubic-bezier for each block are different, the baseline is moving and therefore the elements are too.
See my answer on Why does this inline-block element have content that is not vertically aligned for more detail on why the baseline moves.
So your problem would not occur if the animation properties on each block was the same.
For my demo, I have used vertical-align:top;
but there are other values depending on your needs. I also set the animation-fill-mode:forwards
so that "after the animation ends (as determined by its animation-iteration-count
), the animation will apply the property values for the time the animation ended" - from the CSS documentation.
CSS
@keyframes anim {
from {
height: 100px;
}
to {
height: 0;
}
}
@-webkit-keyframes anim {
from {
height:100px;
}
to {
height:0;
}
}
.box {
width: 50px;
display: inline-block;
background: #5f0;
height: 100px;
border: 1px solid #000;
vertical-align:top;
}
.box:first-of-type {
animation: anim 10s cubic-bezier(0, 1, .5, 1);
-webkit-animation: anim 10s cubic-bezier(0, 1, .5, 1) forwards;
}
.box:last-of-type {
animation: anim 10s cubic-bezier(0, .5, .5, 1);
-webkit-animation: anim 10s cubic-bezier(0, .5, .5, 1) forwards;
}
HTML
<div class=box></div>
<div class=box></div>