A ring drains over ten seconds while the number inside counts down, and both turn red for the final three. The ring is an SVG circle with pathLength 100, so the dash math is just 0 to 100.
Also known as: countdown timer css, seconds ring timer, circular timer animation.
<div class="txt-circular-countdown-timer" aria-label="Ten second timer">
<svg viewBox="0 0 100 100" aria-hidden="true">
<circle cx="50" cy="50" r="44" pathLength="100"/>
<circle class="txt-circular-countdown-timer-arc" cx="50" cy="50" r="44" pathLength="100"/>
</svg>
</div>@property --txt-circular-countdown-timer-n {
syntax: "<integer>";
initial-value: 10;
inherits: false;
}
.txt-circular-countdown-timer {
--c: var(--txt-color, #3b82f6);
font-size: calc(var(--txt-size, 32px) * 1.3);
font-family: system-ui, sans-serif;
font-weight: 700;
line-height: 1.2;
position: relative;
display: grid;
place-items: center;
width: 2.8em;
height: 2.8em;
color: var(--c);
font-variant-numeric: tabular-nums;
counter-reset: txt-circular-countdown-timer-v var(--txt-circular-countdown-timer-n);
animation:
txt-circular-countdown-timer-count 10s steps(10) infinite,
txt-circular-countdown-timer-warn 10s step-end infinite;}
.txt-circular-countdown-timer::after {
content: counter(txt-circular-countdown-timer-v);
}
.txt-circular-countdown-timer svg {
position: absolute;
inset: 0;
transform: rotate(-90deg);
fill: none;
stroke-width: 7;
}
.txt-circular-countdown-timer circle {
stroke: color-mix(in srgb, var(--c) 18%, transparent);
}
.txt-circular-countdown-timer circle.txt-circular-countdown-timer-arc {
stroke: currentColor;
stroke-linecap: round;
stroke-dasharray: 100;
animation: txt-circular-countdown-timer-drain 10s linear infinite;
}
@keyframes txt-circular-countdown-timer-count {
from { --txt-circular-countdown-timer-n: 10; }
to { --txt-circular-countdown-timer-n: 0; }
}
@keyframes txt-circular-countdown-timer-drain {
to { stroke-dashoffset: 100; }
}
@keyframes txt-circular-countdown-timer-warn {
0% { color: var(--c); }
70% { color: #ef4444; }
}
@media (prefers-reduced-motion: reduce) {
.txt-circular-countdown-timer, .txt-circular-countdown-timer *, .txt-circular-countdown-timer::before, .txt-circular-countdown-timer::after, .txt-circular-countdown-timer *::before, .txt-circular-countdown-timer *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--txt-color on the element or a parent.
Currently previewing #3b82f6.--txt-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 40px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.