Step progress bar with pure CSS
In this short blog post, I will show you how to create a custom step progress bar with CSS, which can be easily integrated into every application.
Let’s first start with the easy part – the HTML.
<div class="stepper-wrapper">
<div class="stepper-item completed">
<div class="step-counter">1</div>
<div class="step-name">First</div>
</div>
<div class="stepper-item completed">
<div class="step-counter">2</div>
<div class="step-name">Second</div>
</div>
<div class="stepper-item active">
<div class="step-counter">3</div>
<div class="step-name">Third</div>
</div>
<div class="stepper-item">
<div class="step-counter">4</div>
<div class="step-name">Forth</div>
</div>
</div>
As you see, the stepper-item
element has 2 additional classes – active and completed. In our application, you would most probably set these classes dynamically. Now let’s focus on the harder part – the styling:
.stepper-wrapper {
margin-top: auto;
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.stepper-item {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
flex: 1;
@media (max-width: 768px) {
font-size: 12px;
}
}
.stepper-item::before {
position: absolute;
content: "";
border-bottom: 2px solid #ccc;
width: 100%;
top: 20px;
left: -50%;
z-index: 2;
}
.stepper-item::after {
position: absolute;
content: "";
border-bottom: 2px solid #ccc;
width: 100%;
top: 20px;
left: 50%;
z-index: 2;
}
.stepper-item .step-counter {
position: relative;
z-index: 5;
display: flex;
justify-content: center;
align-items: center;
width: 40px;
height: 40px;
border-radius: 50%;
background: #ccc;
margin-bottom: 6px;
}
.stepper-item.active {
font-weight: bold;
}
.stepper-item.completed .step-counter {
background-color: #4bb543;
}
.stepper-item.completed::after {
position: absolute;
content: "";
border-bottom: 2px solid #4bb543;
width: 100%;
top: 20px;
left: 50%;
z-index: 3;
}
.stepper-item:first-child::before {
content: none;
}
.stepper-item:last-child::after {
content: none;
}
Voila! With this CSS, you get a nice-looking progress stepper component, which is easily customizable at the same time. The lines that connect the different steps are represented as ::before
and ::after
pseudo-elements.
Keep in mind that if you decide to scale the element containing the step, you should also change the top value of the pseudo-elements. If you use SASS or CSS variables, you can easily adjust this, by making the top
value half of the circle’s height
.
Below you will find the code in action:
Enjoy!