Spinners are the small element that indicates the loading state of web page components. Mostly, the loader GIF icon is used as a spinner in loading animation. If you want a simple solution for the spinner animation, CSS can be used to create snipper icons without JavaScript/jQuery.
The pure CSS spinner helps to reduce page load time and add user-friendly loading animation to the web page. In this example code snippet, we will create spinners with HTML and CSS.
<div class="spinner" role="status">
<span class="sr-only">Loading...</span>
</div>
@keyframes spinner-border {
to { transform: rotate(360deg); }
}
.spinner {
display: inline-block;
width: 2rem;
height: 2rem;
vertical-align: -0.125em;
color: #007bff;
border: 0.25em solid currentcolor;
border-right-color: transparent;
border-radius: 50%;
-webkit-animation: .75s linear infinite spinner-border;
animation: .75s linear infinite spinner-border;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
The following code snippet is used to add a loading spinner in the HTML button using CSS.
<button class="btn btn-primary" type="button" disabled>
<span class="spinner" role="status" aria-hidden="true"></span>
Loading...
</button>
@keyframes spinner-border {
to { transform: rotate(360deg); }
}
.spinner {
display: inline-block;
width: 2rem;
height: 2rem;
vertical-align: -0.125em;
color: #007bff;
border: 0.25em solid currentcolor;
border-right-color: transparent;
border-radius: 50%;
-webkit-animation: .75s linear infinite spinner-border;
animation: .75s linear infinite spinner-border;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
.btn {
display: inline-block;
font-weight: 400;
color: #212529;
text-align: center;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: 0.25rem;
}
.btn-primary {
color: #fff;
background-color: #007bff;
border-color: #007bff;
}
.btn:disabled {
opacity: .65;
}
.btn .spinner {
color: #fff;
width: 1rem;
height: 1rem;
vertical-align: middle;
}