A battery fills from 8% to 100% while the percentage climbs beside it, red turning to your color, with a light sweep across the charge and Charged at the top. One registered integer drives all of it.
Also known as: percentage animation css, battery charging animation css, percentage counter with bar, number animation css.
<div class="txt-battery-charging-percent" aria-label="Battery charging">
<span class="txt-battery-charging-percent-cell" aria-hidden="true"><span class="txt-battery-charging-percent-fill"></span><svg viewBox="0 0 24 24"><path d="M13 2 4.5 13.5H11L10 22l8.5-11.5H12z"/></svg></span>
<span class="txt-battery-charging-percent-read" aria-hidden="true">
<span class="txt-battery-charging-percent-num"></span>
<span class="txt-battery-charging-percent-state"><span>Charging</span><span>Charged</span></span>
</span>
</div>@property --txt-battery-charging-percent-p {
syntax: "<integer>";
initial-value: 100;
inherits: true;
}
.txt-battery-charging-percent {
--c: var(--txt-color, #3b82f6);
font-size: calc(var(--txt-size, 32px) * 0.9);
font-family: system-ui, sans-serif;
font-weight: 700;
line-height: 1.2;
display: inline-flex;
align-items: center;
gap: 0.55em;
--t: 6.4s;
--low: #e5484d;
animation: txt-battery-charging-percent-p var(--t) infinite;}
.txt-battery-charging-percent-cell {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 2.5em;
height: 1.2em;
padding: 0.12em;
border: 0.08em solid color-mix(in srgb, currentColor 35%, transparent);
border-radius: 0.34em;
box-sizing: border-box;
}
/* the cap */
.txt-battery-charging-percent-cell::after {
content: "";
position: absolute;
right: -0.22em;
width: 0.12em;
height: 0.4em;
border-radius: 0 0.1em 0.1em 0;
background: color-mix(in srgb, currentColor 35%, transparent);
}
.txt-battery-charging-percent-fill {
position: absolute;
inset: 0.1em auto 0.1em 0.1em;
width: calc((100% - 0.2em) * var(--txt-battery-charging-percent-p) / 100);
border-radius: 0.2em;
background: var(--c);
overflow: hidden;
animation: txt-battery-charging-percent-color var(--t) infinite;
}
.txt-battery-charging-percent-fill::after {
content: "";
position: absolute;
inset: 0;
background: linear-gradient(100deg, transparent 30%, rgb(255 255 255 / 0.45) 50%, transparent 70%);
transform: translateX(-100%);
animation: txt-battery-charging-percent-shine 1.6s cubic-bezier(0.16, 1, 0.3, 1) infinite;
}
.txt-battery-charging-percent-cell svg {
position: relative;
width: 0.75em;
height: 0.75em;
fill: #fff;
stroke: rgb(0 0 0 / 0.35);
stroke-width: 1.2;
stroke-linejoin: round;
}
.txt-battery-charging-percent-read {
display: grid;
line-height: 1.1;
}
.txt-battery-charging-percent-num {
font-size: 1.15em;
font-weight: 700;
font-variant-numeric: tabular-nums;
letter-spacing: -0.02em;
counter-reset: txt-battery-charging-percent-n var(--txt-battery-charging-percent-p);
}
.txt-battery-charging-percent-num::before {
content: counter(txt-battery-charging-percent-n) "%";
}
.txt-battery-charging-percent-state {
display: grid;
font-size: 0.45em;
font-weight: 500;
opacity: 0.6;
}
.txt-battery-charging-percent-state span {
grid-area: 1 / 1;
animation: txt-battery-charging-percent-swap var(--t) infinite;
}
.txt-battery-charging-percent-num { animation: txt-battery-charging-percent-dip var(--t) infinite; }
.txt-battery-charging-percent-state span:last-child { animation-name: txt-battery-charging-percent-swap-end; }
@keyframes txt-battery-charging-percent-p {
0%, 6% { --txt-battery-charging-percent-p: 8; animation-timing-function: cubic-bezier(0.45, 0, 0.25, 1); }
72% { --txt-battery-charging-percent-p: 100; }
100% { --txt-battery-charging-percent-p: 100; }
}
@keyframes txt-battery-charging-percent-color {
0% { background-color: var(--low); opacity: 0; }
4%, 14% { background-color: var(--low); opacity: 1; }
24%, 93% { background-color: var(--c); opacity: 1; }
99%, 100% { background-color: var(--c); opacity: 0; }
}
@keyframes txt-battery-charging-percent-dip {
0% { opacity: 0; }
4%, 93% { opacity: 1; }
99%, 100% { opacity: 0; }
}
@keyframes txt-battery-charging-percent-shine {
to { transform: translateX(100%); }
}
@keyframes txt-battery-charging-percent-swap {
0%, 72% { opacity: 1; transform: none; }
76%, 100% { opacity: 0; transform: translateY(-0.4em); }
}
@keyframes txt-battery-charging-percent-swap-end {
0%, 72% { opacity: 0; transform: translateY(0.4em); }
76%, 93% { opacity: 1; transform: none; }
99%, 100% { opacity: 0; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
.txt-battery-charging-percent, .txt-battery-charging-percent *, .txt-battery-charging-percent *::before, .txt-battery-charging-percent *::after { animation: none !important; }
.txt-battery-charging-percent-state span:first-child { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
.txt-battery-charging-percent, .txt-battery-charging-percent *, .txt-battery-charging-percent::before, .txt-battery-charging-percent::after, .txt-battery-charging-percent *::before, .txt-battery-charging-percent *::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.