Building a Progress Bar Component: A Complete Guide
Progress bar with animation, percentage display, and different types. Here is the complete guide.
Building a Progress Bar Component: A Complete Guide
A progress bar shows the completion status of a task. Here is the complete guide to building one.
Types of Progress Bars
- Linear: a horizontal bar that fills from left to right.
- Circular: a ring that fills in a circle.
- Stepped: discrete steps with labels.
- Striped: animated stripes for indeterminate progress.
Linear Progress Bar
<div class="progress-container"> <div class="progress-bar" id="bar"></div> </div> <span id="text">0%</span>
function setProgress(percent) { percent = Math.max(0, Math.min(100, percent)); document.getElementById("bar").style.width = percent + "%"; document.getElementById("text").textContent = percent + "%"; } `` ```css .progress-container { width: 100%; height: 20px; background: #e0e0e0; border-radius: 10px; overflow: hidden; } .progress-bar { height: 100%; background: #007bff; border-radius: 10px; transition: width 0.3s ease; } `` ### Circular Progress Bar (SVG) ```html <svg width="120" height="120"> <circle cx="60" cy="60" r="50" stroke="#e0e0e0" stroke-width="10" fill="none" /> <circle cx="60" cy="60" r="50" stroke="#007bff" stroke-width="10" fill="none" stroke-dasharray="314" stroke-dashoffset="314" id="circle" transform="rotate(-90 60 60)" /> </svg>
function setCircular(percent) { const circumference = 2 * Math.PI * 50; // 314 const offset = circumference - (percent / 100) * circumference; document.getElementById("circle").style.strokeDashoffset = offset; }
Auto-Increment
function autoIncrement() { let progress = 0; const interval = setInterval(() => { progress += 1; setProgress(progress); if (progress >= 100) clearInterval(interval); }, 100); }
Color-Based on Progress
function getColor(percent) { if (percent < 30) return "#ff4444"; // red if (percent < 70) return "#ffaa00"; // yellow return "#44aa44"; // green } bar.style.background = getColor(percent);
The Takeaway
Progress bar types: linear (width %), circular (SVG stroke-dashoffset), stepped (discrete steps), striped (animated). Use CSS transition for smooth animation. Clamp to 0-100. Color based on progress. Auto-increment with setInterval. The linear bar is the most common interview question.
Create a container div (background gray) and an inner fill div. Set the fill's width to a percentage. Use CSS transition: width 0.3s ease for smooth animation. Clamp the value to 0-100. Display the percentage.
Use SVG with two circles. The background circle has a gray stroke. The progress circle uses stroke-dasharray (circumference) and stroke-dashoffset (circumference - percent/100 * circumference). Rotate -90 degrees so it starts at the top.
Use CSS transition on the width property: transition: width 0.3s ease. When you change the width via JavaScript, the CSS transition animates the change smoothly. No requestAnimationFrame needed.
Use setInterval: const interval = setInterval(() => { progress += 1; setProgress(progress); if (progress >= 100) clearInterval(interval); }, 100). This increments by 1 every 100ms until it reaches 100.
Create a function that returns a color based on the percentage: < 30% red, < 70% yellow, >= 70% green. Set bar.style.background = getColor(percent). This gives visual feedback on progress status.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

