Build a Customizable Progress Indicator with Pure CSS: Harvey Ball Example
Introduction
While developing a recent web application, I needed an intuitive and visual way to display task progress. After considering various options, I chose Harvey Balls: circular progress indicators that are commonly used in charts and reports. In this tutorial, I’ll walk you through how to create Harvey Balls with pure CSS, making them flexible and customizable for any use case.
We’ll use only CSS, with no external libraries, to create a progress indicator that’s not only easy to implement but also highly adaptable for different design needs.
Requirements
Here’s what we’re aiming for:
- A simple, clean solution using pure CSS.
- Customizable indicators that can be easily applied to any
div
orspan
element. - Flexibility to adjust the size, color, and progress value with minimal effort.
Step 1: Basic Harvey Ball Setup
To start, we’ll transform a div
or span
element into a circular progress indicator.
|
|
Apply this class in your HTML:
|
|
Check out the code on GitHub Try it out on CodePen
Step 2: Creating Progress States
Now, let’s use conic-gradient
function, which is ideal for creating circular progress bars, to define the different progress states: quarter, half, three-quarters, and full.
|
|
In your HTML, you can now apply these classes to visualize progress:
|
|
Here’s what the result looks like:
Check out the code on GitHub Try it out on CodePen
Step 3: Improving Customization with CSS Variables
To make the Harvey Ball more flexible, we can introduce CSS variables for the size, color, and progress value. This allows you to easily adjust these properties for different use cases.
|
|
This approach makes it easy to create progress indicators with custom values by overriding the --value
variable either in the CSS class or directly within the style
attribute.
For example:
|
|
Check out the code on GitHub Try it out on CodePen
Step 4: Adding Value Clamping for Extra Safety
To ensure the --value
variable stays within the 0% – 100% range, we can use the clamp()
function. This guarantees that any invalid values, such as those below 0% or above 100%, are automatically corrected to fit within the valid range.
|
|
Example in HTML:
|
|
Check out the code on GitHub Try it out on CodePen
The first div
has an invalid value of -50%, but thanks to the clamp()
function, it is corrected to 0%, showing no progress. Similarly, the second div
is set to 150%, but clamp()
ensures the value does not exceed 100%, resulting in a fully filled Harvey Ball.
Bonus: Future-Proofing with Experimental CSS attr()
Looking ahead, CSS might soon support the attr()
function
for properties other than content
. If browser support for attr()
expands, we could make the progress indicator even more dynamic by pulling the value directly from a custom HTML
attribute.
Future Compatibility OnlyThe technique described in this section using theattr()
function is currently experimental and not fully supported by all browsers. At the time of writing,attr()
can only be used for thecontent
property in CSS, but future support for other properties is being explored. Be sure to check browser compatibility before using this method in production environments.
Here’s an example of what that might look like:
|
|
Then, set the progress value directly in your HTML using the data attribute:
|
|
Conclusion
Creating a Harvey Ball progress indicator using pure CSS is not only easy but also incredibly useful for displaying progress in a clean, minimalist way. With the flexibility of CSS variables, you can easily customize the size, color, and percentage of completion.
Feel free to experiment with your own custom progress indicators! If you have any questions or feedback, don’t hesitate to reach out via my contact information in the footer.