I've been experimenting with a pure HTML/CSS countdown animation, but I'm facing challenges in getting it to run smoothly. Currently, I'm focusing on minutes and seconds only. One issue I encountered is that the digit representing tens of minutes is not transitioning synchronously with the other digits.
Below you'll find my code along with two CodePens - one showcasing the normal version with correct timings for regular behavior, and another running ten times faster so you can quickly identify the discrepancy.
- https://codepen.io/ecazorla/pen/WKdZpQ (normal version)
- https://codepen.io/ecazorla/pen/bjapeN (10 times faster version)
@keyframes tick6 {
0% { margin-top: 0; }
16% { margin-top: -2rem; }
33% { margin-top: -4rem; }
50% { margin-top: -6rem; }
66% { margin-top: -8rem; }
83% { margin-top: -10rem; }
100% { margin-top: -12rem; }
}
@keyframes tick10 {
0% { margin-top: 0; }
10% { margin-top: -2rem; }
20% { margin-top: -4rem; }
30% { margin-top: -6rem; }
40% { margin-top: -8rem; }
50% { margin-top: -10rem; }
60% { margin-top: -12rem; }
70% { margin-top: -14rem; }
80% { margin-top: -16rem; }
90% { margin-top: -18rem; }
100% { margin-top: -20rem; }
}
body {
background-color: black;
}
.container {
background-color: white;
}
.digit {
display: inline-block;
height: 2rem;
overflow: hidden;
width: 1ch;
}
.digit span {
display: block;
height: 2rem;
width: 100%;
}
.minutes-digit-one {
animation: tick6 3600s step-end;
animation-iteration-count: infinite;
animation-delay: -540s;
}
.minutes-digit-two {
animation: tick10 600s step-end;
animation-iteration-count: infinite;
animation-delay: -540s;
}
.seconds-digit-one {
animation: tick6 60s step-end;
animation-iteration-count: infinite;
animation-delay: -540s;
}
.seconds-digit-two {
animation: tick10 10s;
animation-iteration-count: infinite;
animation-delay: -540s;
}
<div class="container">
<div class="digit">
<span class="minutes-digit-one">5</span>
<span>4</span>
<span>3</span>
<span>2</span>
<span>1</span>
<span>0</span>
<span>5</span>
</div>
<div class="digit">
<span class="minutes-digit-two">9</span>
<span>8</span>
<span>7</span>
<span>6</span>
<span>5</span>
<span>4</span>
<span>3</span>
<span>2</span>
<span>1</span>
<span>0</span>
<span>9</span>
</div>
<div class="digit">
<span>:</span>
</div>
<div class="digit">
<span class="seconds-digit-one">5</span>
<span>4</span>
<span>3</span>
<span>2</span>
<span>1</span>
<span>0</span>
<span>5</span>
</div>
<div class="digit">
<span class="seconds-digit-two">9</span>
<span>8</span>
<span>7</span>
<span>6</span>
<span>5</span>
<span>4</span>
<span>3</span>
<span>2</span>
<span>1</span>
<span>0</span>
<span>9</span>
</div>
</div>