Open In App

Create a Button Loading Animation in HTML CSS & JavaScript

Last Updated : 19 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A "Button Loading Animation" in HTML, CSS, and JavaScript is a user interface element that temporarily transforms a button into a loading state with a spinner or animation to indicate ongoing processing or data retrieval, providing feedback to users during asynchronous tasks.

loder-animation

Approach:

  • HTML page with a user form, including username, password fields, and a "Submit" button that triggers a loading animation.
  • CSS styles define the page's visual layout, including button styling, loader appearance, and form container formatting.
  • JavaScript adds a "loading" class to the button and displays the loader when the "Submit" button is clicked.
  • Simulated async operation using setTimeout for 2 seconds before restoring the button's normal state and resetting the form.
  • Interactive form with a loading animation to improve user experience during form submission or other async actions.

Example:

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Button Loading Animation</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="form-container">
        <h1>GeeksforGeeks</h1>
        <form id="my-form">
            <label for="username">Username:</label>
            <input type="text" id="username" 
                   name="username" required>
            <br><br>
            <label for="password">Password:</label>
            <input type="password" id="password" 
                   name="password" required>
            <br><br>
            <div class="btn-container">
                <button id="loadButton" class="btn">
                    Submit
                    <div class="loader" id="loader">

                    </div>
                </button>
            </div>
        </form>
    </div>
    <script src="script.js"></script>
</body>
</html>
CSS JavaScript

Output:

loder-animation


Next Article

Similar Reads