How to Create Loading Buttons With CSS

Creating engaging and user-friendly interfaces is an essential part of the job.

One of the elements that can enhance the user experience is the loading button, a button that displays the progress of a task in real-time.

In this tutorial, we will discuss how to create loading buttons using CSS, and provide code examples to help you understand the process better.


Introduction to Loading Buttons

A loading button is a button that displays the progress of a task, such as uploading a file, sending a message, or loading a page.

The purpose of a loading button is to keep the user informed of the status of the task and prevent them from performing any actions that might interfere with it.

Loading buttons come in various designs and styles, but the most common one is a spinning circle or progress bar displayed next to the text of the button.

CSS Properties for Creating Loading Buttons

To create a loading button with CSS, you will need to use several CSS properties, including:

  • Animation: The animation property is used to define the motion of an element. You can use it to create a spinning circle or a progress bar.
  • Border-radius: The border-radius property is used to create rounded corners for a button.
  • Box-shadow: The box-shadow property is used to create a drop shadow effect for a button.
  • Transform: The transform property is used to rotate an element. You can use it to rotate the spinning circle.
  • Transition: The transition property is used to create a smooth animation between two states. You can use it to animate the appearance of the loading button.

Code Examples of Loading Buttons

Here are some code examples of loading buttons created using CSS:

Spinning Circle Loading Button

<button class="loading-button">
  <span class="spinner"></span>
  Loading...
</button>

.loading-button {
  position: relative;
  display: inline-block;
  padding: 12px 24px;
  border: none;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
}

.loading-button .spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border: 2px solid #ddd;
  border-top-color: #333;
  border-radius: 50%;
  animation: spinner 0.6s linear infinite;
}

@keyframes spinner {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

Progress Bar Loading Button

<button class="loading-button">
  <span class="progress"></span>
  Loading...
</button>

.loading-button {
  position: relative;
  display: inline-block;
  padding: 12px 24px;
  border: none;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
}

.loading-button .progress {
  position: absolute;
  top: 0;
left: 0;
bottom: 0;
right: 0;
height: 4px;
background-color: #ddd;
border-radius: 4px;
transform: scaleX(0);
transform-origin: left;
transition: all 0.2s;
}

.loading-button[data-loading] .progress {
transform: scaleX(1);
}

Conclusion

In conclusion, loading buttons are an essential part of user-friendly interfaces, and they can be created using CSS properties such as animation, border-radius, box-shadow, transform, and transition.

We hope that this post has provided you with the knowledge and code examples necessary to create your own loading buttons.

As always, feel free to experiment and come up with your own unique designs.