The basic HTML structure of a stepper consists of a container element and individual step elements. Each step usually contains a number or an icon to represent its position and a label to describe the step.
<div class="stepper">
<div class="step active">
<span class="step-number">1</span>
<span class="step-label">Step One</span>
</div>
<div class="step">
<span class="step-number">2</span>
<span class="step-label">Step Two</span>
</div>
<div class="step">
<span class="step-number">3</span>
<span class="step-label">Step Three</span>
</div>
</div>
CSS is used to style the stepper and its steps. We can set the layout, colors, borders, and other visual properties. For example, we can use flexbox to arrange the steps horizontally.
.stepper {
display: flex;
justify-content: space-around;
align-items: center;
}
.step {
display: flex;
flex-direction: column;
align-items: center;
}
.step-number {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: #ccc;
text-align: center;
line-height: 30px;
}
.step-label {
margin-top: 5px;
}
.active .step-number {
background-color: #007BFF;
color: white;
}
To use the stepper, you first need to include the HTML and CSS code in your project. You can then modify the content of the step labels according to your specific process. To indicate the current step, you can add the active
class to the corresponding step element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.stepper {
display: flex;
justify-content: space-around;
align-items: center;
}
.step {
display: flex;
flex-direction: column;
align-items: center;
}
.step-number {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: #ccc;
text-align: center;
line-height: 30px;
}
.step-label {
margin-top: 5px;
}
.active .step-number {
background-color: #007BFF;
color: white;
}
</style>
<title>HTML CSS Stepper</title>
</head>
<body>
<div class="stepper">
<div class="step active">
<span class="step-number">1</span>
<span class="step-label">Registration</span>
</div>
<div class="step">
<span class="step-number">2</span>
<span class="step-label">Payment</span>
</div>
<div class="step">
<span class="step-number">3</span>
<span class="step-label">Confirmation</span>
</div>
</div>
</body>
</html>
If you want to create an interactive stepper, you can integrate it with JavaScript. For example, you can use JavaScript to add or remove the active
class when the user clicks on a step or moves to the next/previous step.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.stepper {
display: flex;
justify-content: space-around;
align-items: center;
}
.step {
display: flex;
flex-direction: column;
align-items: center;
cursor: pointer;
}
.step-number {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: #ccc;
text-align: center;
line-height: 30px;
}
.step-label {
margin-top: 5px;
}
.active .step-number {
background-color: #007BFF;
color: white;
}
</style>
<title>Interactive HTML CSS Stepper</title>
</head>
<body>
<div class="stepper">
<div class="step active">
<span class="step-number">1</span>
<span class="step-label">Step One</span>
</div>
<div class="step">
<span class="step-number">2</span>
<span class="step-label">Step Two</span>
</div>
<div class="step">
<span class="step-number">3</span>
<span class="step-label">Step Three</span>
</div>
</div>
<script>
const steps = document.querySelectorAll('.step');
steps.forEach(step => {
step.addEventListener('click', () => {
steps.forEach(s => s.classList.remove('active'));
step.classList.add('active');
});
});
</script>
</body>
</html>
It is important to make the stepper responsive so that it can adapt to different screen sizes. You can use media queries in CSS to adjust the layout of the stepper.
@media (max - width: 768px) {
.stepper {
flex-direction: column;
}
}
Ensure that the stepper is accessible to all users. You can add appropriate ARIA (Accessible Rich Internet Applications) roles and attributes to the HTML elements. For example, you can add role="tablist"
to the stepper container and role="tab"
to each step element.
<div class="stepper" role="tablist">
<div class="step active" role="tab" aria - selected="true">
<span class="step-number">1</span>
<span class="step-label">Step One</span>
</div>
<div class="step" role="tab" aria - selected="false">
<span class="step-number">2</span>
<span class="step-label">Step Two</span>
</div>
<div class="step" role="tab" aria - selected="false">
<span class="step-number">3</span>
<span class="step-label">Step Three</span>
</div>
</div>
Avoid over - complicating the design of the stepper. Use clean and simple colors and shapes. A cluttered stepper can be confusing for users.
When the user interacts with the stepper, provide visual feedback. For example, change the color or opacity of the step when the user hovers over it.
.step:hover .step-number {
background-color: #999;
}
Test the stepper on different devices and browsers to ensure that it looks and functions correctly. This helps to provide a consistent user experience.
HTML CSS steppers are a powerful and versatile UI component that can greatly enhance the user experience of your web applications. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can create custom steppers that are not only functional but also visually appealing. Whether you are building a simple form or a complex multi - step wizard, steppers can guide your users through the process smoothly.