DEV Community

Cover image for How to Style Progress Bar with Tailwind CSS?
Aditya Barde
Aditya Barde

Posted on

How to Style Progress Bar with Tailwind CSS?

Introduction:
Progress bars are a visual representation of the completion status of a task or process. They provide valuable feedback to users, indicating how much progress has been made. In this tutorial, we will explore how to style progress bars using Tailwind CSS, a popular utility-first CSS framework that offers a straightforward way to create beautiful and responsive user interfaces.

Setting up the Environment:
Before we dive into styling progress bars, let's make sure we have the necessary tools in place. To follow along with this tutorial, you'll need:

Tailwind CSS: Install Tailwind CSS by following the official documentation at Tailwindcss official docs.

HTML File: Create an HTML file. Add the Play CDN script tag to the

of your HTML file, and start using Tailwind’s utility classes to style your content.
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
  <h1 class="text-3xl font-bold underline">
    Hello world!
  </h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Styling the Progress Bar:
Now that we have our environment set up, let's get into the actual styling of the progress bar using Tailwind CSS classes.

Define the Progress Bar:
Start by adding the element to your HTML file. This element serves as the foundation of our progress bar. Here's an example:

<progress
  class="w-56 [&::-webkit-progress-bar]:rounded-lg [&::-webkit-progress-value]:rounded-lg [&::-webkit-progress-bar]:bg-slate-300 [&::-webkit-progress-value]:bg-green-400 [&::-moz-progress-bar]:bg-green-400"
  value="5"
  max="5"
></progress>
Enter fullscreen mode Exit fullscreen mode

Progress bar with tailwindcss
The &::-webkit-progress-bar selector is used to target and style the background of the progress bar in WebKit-based browsers like Chrome and Safari.

When you apply styles to the ::-webkit-progress-bar pseudo-element, you are targeting the specific part of the progress bar that represents the overall bar's background. By using the & symbol within the Tailwind CSS class, it indicates that the pseudo-element is part of the same selector.

The ::-webkit-progress-bar pseudo-element allows you to customize the appearance of the background of the progress bar, such as its color, size, or shape. In the code example you provided, the bg-slate-300 class is applied to ::-webkit-progress-bar, setting the background color of the progress bar to a shade of slate gray.

It's worth noting that the ::-webkit-progress-bar selector is specific to WebKit-based browsers and will not affect the appearance of progress bars in other browsers like Firefox or Microsoft Edge. To ensure cross-browser compatibility, you can use additional selectors like ::-moz-progress-bar (for Firefox) or ::-ms-fill (for Microsoft Edge) to target and style progress bars in those respective browsers.

By leveraging these pseudo-elements and their corresponding selectors, you can customize different parts of the progress bar and create unique styles tailored to your design preferences.

Customize the Width:
To set the width of the progress bar, use the w- class followed by a number. In our example, w-56 sets the width to 56 units. Feel free to adjust this value according to your design requirements.

Rounded Corners:
Tailwind CSS makes it easy to add rounded corners to elements. To give our progress bar rounded corners, we use the rounded-lg class. We apply it to both the ::webkit-progress-bar and ::webkit-progress-value pseudo-elements.

Background Colors:
Tailwind CSS provides an extensive set of utility classes for background colors. In our example, we use bg-slate-300 to style the ::webkit-progress-bar element with a shade of slate gray. The bg-green-400 class is applied to the ::webkit-progress-value and ::moz-progress-bar pseudo-elements, giving them a vibrant green color. You can explore other color options in the Tailwind CSS documentation.

Value and Maximum:
To regulate the progress shown by the bar, adjust the value attribute to reflect the current progress and set the max attribute to the maximum value. In our instance, with value="3" and max="5", it signifies that the progress bar is filled up to 3 units, representing a partial completion.

Conclusion:
Tailwind CSS simplifies the process of styling progress bars, allowing you to create visually appealing UI elements with minimal effort. By leveraging the utility classes provided by Tailwind CSS, you can easily customize the width, rounded corners, and background colors of progress bars to match your design preferences.

In this tutorial, I covered the basics of styling progress bars using Tailwind CSS, providing you with a solid foundation to explore further customization options. Experiment with different class combinations, try out various color schemes, and make your progress bars truly stand out in your web applications.

Remember to refer to the official Tailwind CSS documentation for more in-depth information on styling elements and exploring additional features.

Thank you for reading. I hope you liked it. For more information and developement tips, tricks and resources follow me on twitter.
Happy coding!

Top comments (0)