A view count ticks up to 999 and then rolls over: the digits slide out the top and 1K views springs up from below. Useful for social posts and video stats. One integer, two pseudo-elements.
Also known as: view count animation, 1k views counter, social counter css.
<div class="txt-views-counter" aria-label="1K views"> <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7S2 12 2 12Z"/><circle cx="12" cy="12" r="3"/></svg> <span class="txt-views-counter-num" aria-hidden="true"></span> </div>
@property --txt-views-counter-n {
syntax: "<integer>";
initial-value: 940;
inherits: false;
}
.txt-views-counter {
--c: var(--txt-color, #3b82f6);
font-size: calc(var(--txt-size, 32px) * 1.2);
font-family: system-ui, sans-serif;
font-weight: 700;
line-height: 1.2;
display: inline-flex;
align-items: center;
gap: 0.3em;
color: var(--c);}
.txt-views-counter svg {
width: 1em;
height: 1em;
fill: none;
stroke: currentColor;
stroke-width: 2.2;
stroke-linejoin: round;
animation: txt-views-counter-blink 0.5s ease-in-out 1.6s;
}
.txt-views-counter-num {
display: inline-grid;
height: 1.2em;
overflow: hidden;
font-variant-numeric: tabular-nums;
counter-reset: txt-views-counter-v var(--txt-views-counter-n);
animation: txt-views-counter-count 1.5s cubic-bezier(0.55, 0, 0.9, 0.55) forwards;
}
.txt-views-counter-num::before,
.txt-views-counter-num::after {
grid-area: 1 / 1;
}
.txt-views-counter-num::before {
content: counter(txt-views-counter-v) " views";
animation: txt-views-counter-out 0.35s cubic-bezier(0.55, 0, 1, 0.45) 1.6s forwards;
}
.txt-views-counter-num::after {
content: "1K views";
transform: translateY(110%);
animation: txt-views-counter-in 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) 1.75s forwards;
}
@keyframes txt-views-counter-count {
to { --txt-views-counter-n: 999; }
}
@keyframes txt-views-counter-out {
to { transform: translateY(-110%); opacity: 0; }
}
@keyframes txt-views-counter-in {
to { transform: translateY(0); }
}
@keyframes txt-views-counter-blink {
50% { transform: scaleY(0.15); }
}
@media (prefers-reduced-motion: reduce) {
.txt-views-counter, .txt-views-counter *, .txt-views-counter::before, .txt-views-counter::after, .txt-views-counter *::before, .txt-views-counter *::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.