Code
<div class="loading"></div>
.loading {
width: 100px;
height: 100px;
border-radius: 50%;
border: 10px solid #ddd;
border-top-color: orange;
animation: loading 1s linear infinite;
}
@keyframes loading {
to {
transform: rotate(360deg);
}
}
Explanation
The loading spinner is a circle with a border. One part of the border is colored to stand out from the rest of the border. The spinner rotates using CSS animations.
- The
borderproperty adds a border around the element to create the circle. - The
border-radiusproperty makes the border a circle. - We color one part of the border orange using the
border-top-colorproperty so that when the spinner rotates, the orange part stands out from the rest of the border, giving the illusion of a spinner. - The
animationproperty is used to rotate the spinner.
Adding an overlay to the loading spinner
Adding an overlay to the loading spinner allows you to create a loading spinner that covers the entire page. This is useful when you want to show a loading spinner while a page is loading or when a form is submitting. This prevents the user from interacting with the page while the loading spinner shows.
If you want to add an overlay to the loading spinner, you can do so by wrapping it in a div and adding some styles.
<div class="loading-state">
<div class="loading"></div>
</div>
.loading-state {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
}
.loading {
width: 100px;
height: 100px;
border-radius: 50%;
border: 10px solid #ddd;
border-top-color: orange;
animation: loading 1s linear infinite;
}
@keyframes loading {
to {
transform: rotate(360deg);
}
}
View the loading spinner on CodePen: https://codepen.io/timonwa/pen/dyaEVbp
Usage
Instances, where you might use a loading spinner, are
- When a page is loading
- When a form is submitting
- When a component is loading
