How do I build an animated bar chart with CSS only?
A simple chart does not need a chart library. HTML elements and a few CSS variables are enough. Each bar reads its value from a variable and grows from the bottom with a scaleY animation.
Steps
- Write the data as an HTML list and put each bar's value in a custom property, for example style="--v: 70".
- Make the list flex with a fixed height, and align the bars to the bottom with align-items: end.
- Set the bar height with height: calc(var(--v) * 1%) and make the animation start at the bottom: transform-origin: bottom.
- Add @keyframes from scaleY(0) to scaleY(1) and stagger each bar with animation-delay.
- Print each name and value as visible text so screen readers and users get the data. With reduced motion, remove the animation and show the bars at once. Vertical bars work in RTL and LTR and the order flips automatically.
The code
HTML
<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>CSS Bar Chart</title>
<style>
body { font-family: system-ui, "Segoe UI", Tahoma, sans-serif; padding: 2rem; }
.chart { display: flex; align-items: end; gap: 1rem; block-size: 240px; margin: 0; padding: 0; list-style: none;
border-block-end: 2px solid #999; }
.chart li { flex: 1; display: flex; flex-direction: column; justify-content: end; align-items: center; block-size: 100%; }
.bar { inline-size: 100%; block-size: calc(var(--v) * 1%); background: #6c5ce7; border-radius: .3rem .3rem 0 0;
transform-origin: bottom; animation: grow .8s cubic-bezier(.2,.8,.2,1) both; animation-delay: calc(var(--i) * 120ms); }
.val { font-weight: 700; }
.name { margin-block-start: .4rem; }
@keyframes grow { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@media (prefers-reduced-motion: reduce) { .bar { animation: none; } }
</style>
<figure>
<figcaption>مثال: بيانات تجريبية</figcaption>
<ul class="chart">
<li style="--v:40;--i:0"><span class="val">40</span><span class="bar"></span><span class="name">أ</span></li>
<li style="--v:75;--i:1"><span class="val">75</span><span class="bar"></span><span class="name">ب</span></li>
<li style="--v:55;--i:2"><span class="val">55</span><span class="bar"></span><span class="name">ج</span></li>
<li style="--v:90;--i:3"><span class="val">90</span><span class="bar"></span><span class="name">د</span></li>
</ul>
</figure>
</html>