Progress bars are essential UI elements that visually represent the completion status of a task, such as file uploads, form submissions, or project tracking. Bootstrap Progress Bars provide a flexible and stylish way to implement these indicators in your projects.
In this tutorial, TheCodingCollege.com will guide you through the fundamentals of Bootstrap progress bars, customization options, and practical use cases.
What Are Bootstrap Progress Bars?
Progress bars in Bootstrap are built using simple HTML and CSS, making them lightweight and easy to integrate into any project. They can be used to display:
- Task completion percentages
- Upload/download progress
- Loading indicators
Key features include:
- Pre-defined styles for simplicity
- Fully responsive design
- Customizable sizes, colors, and animations
Basic Structure of a Bootstrap Progress Bar
A progress bar in Bootstrap is implemented using the progress
class. Inside it, the progress-bar
class defines the actual bar, with a width to represent the percentage completed.
Basic Syntax:
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 50%;">
50%
</div>
</div>
Explanation:
progress
: Wraps the entire progress bar.progress-bar
: Defines the visible bar.style="width: 50%;"
: Sets the bar to 50% completion.
Output:
A progress bar that’s halfway filled and displays the percentage “50%”.
Adding Contextual Colors
Bootstrap provides built-in contextual classes to add colors to progress bars. These include:
progress-bar-success
: Green, for successful tasks.progress-bar-info
: Blue, for informational tasks.progress-bar-warning
: Orange, for warnings or alerts.progress-bar-danger
: Red, for errors or critical issues.
Example – Colored Progress Bars:
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: 25%;">25%</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-info" style="width: 50%;">50%</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-warning" style="width: 75%;">75%</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-danger" style="width: 100%;">100%</div>
</div>
Output:
Progress bars with green, blue, orange, and red colors to represent different statuses.
Striped Progress Bars
To create a striped effect on your progress bar, add the progress-bar-striped
class.
Example – Striped Progress Bar:
<div class="progress">
<div class="progress-bar progress-bar-striped" style="width: 60%;">60%</div>
</div>
Output:
A progress bar with a striped texture.
Animated Progress Bars
For a dynamic look, combine the progress-bar-striped
class with the active
class to add an animation.
Example – Animated Progress Bar:
<div class="progress">
<div class="progress-bar progress-bar-striped active" style="width: 80%;">80%</div>
</div>
Output:
A striped progress bar with an animated motion effect.
Multiple Progress Bars in One Container
Bootstrap allows you to place multiple progress bars in the same container to represent different stages or categories.
Example – Multiple Progress Bars:
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: 35%;">35%</div>
<div class="progress-bar progress-bar-warning" style="width: 20%;">20%</div>
<div class="progress-bar progress-bar-danger" style="width: 10%;">10%</div>
</div>
Output:
A single progress container with three separate progress bars, each styled differently.
Customizing Progress Bars with CSS
While Bootstrap provides default styles, you can easily customize progress bars with CSS.
Example – Custom Width and Colors:
<style>
.custom-progress-bar {
background-color: #28a745;
height: 30px;
font-size: 16px;
}
</style>
<div class="progress">
<div class="progress-bar custom-progress-bar" style="width: 70%;">70%</div>
</div>
Output:
A progress bar with a custom height, green color, and larger text.
Progress Bars with Labels
Adding text inside the progress bar helps users understand the completion status.
Example – Progress Bar with Labels:
<div class="progress">
<div class="progress-bar" style="width: 40%;">40% Complete</div>
</div>
Output:
A progress bar that displays the “40% Complete” label inside the bar.
Responsive Design with Progress Bars
Progress bars are inherently responsive and scale based on their container. Use Bootstrap’s grid system to align progress bars for better layout control.
Example – Responsive Progress Bars:
<div class="row">
<div class="col-sm-6">
<div class="progress">
<div class="progress-bar progress-bar-info" style="width: 50%;">50%</div>
</div>
</div>
<div class="col-sm-6">
<div class="progress">
<div class="progress-bar progress-bar-danger" style="width: 30%;">30%</div>
</div>
</div>
</div>
Output:
Two progress bars aligned side-by-side on larger screens and stacked on smaller screens.
Use Cases for Bootstrap Progress Bars
- File Uploads: Indicate the percentage of a file being uploaded.
- Task Completion: Show how far a user has progressed through a process or form.
- Performance Tracking: Display progress in achieving goals, such as project milestones.
- Health Meters: Represent health, energy, or other metrics in a game or app.
Best Practices for Using Progress Bars
- Keep It Simple: Avoid overloading the bar with too much text or information.
- Add Context: Use colors and labels to indicate the type of progress (e.g., success, warning, or error).
- Test Responsiveness: Ensure progress bars look good on both small and large screens.
- Ensure Accessibility: Add
aria-valuenow
,aria-valuemin
, andaria-valuemax
attributes for screen readers.
Example – Accessible Progress Bar:
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width: 75%;">
75%
</div>
</div>
Advantages of Bootstrap Progress Bars
- Ease of Implementation: Simple syntax for quick integration.
- Customizable: Easily styled to fit any design.
- Responsive: Works across all device sizes.
- Dynamic Visuals: Supports animation and multiple bars.
Conclusion
Bootstrap Progress Bars are a versatile component for displaying task completion, progress tracking, and status updates in your web projects. Whether you need a simple percentage tracker or a complex multi-bar indicator, Bootstrap makes it easy to implement and customize.