Button With Loading Animation On Click with CSS and JavaScript

In this tutorial we will see how to create Button With Loading Animation On Click with CSS and JavaScript. Pure CSS and JavaScript is used for this purpose, complete code is given.

HTML Code

Take a look at the HTML code given below.

<button id="btn" class="login-button">Log In</button>

CSS Code

CSS code is given below.

<style>
*
{
    font-family: 'Barlow Condensed',sans-serif;
}
.login-button
{
    background-color: #0051A1;
    color: #fff;
    border: none;
    cursor: pointer;
    padding: 5px 25px;
    font-size: 16px;
    box-shadow: 0px 0px 2px 0px #000;
}
.icon
{
    margin-right: 5px;
    display: none;
}
.loading
{
    background-color: #014B94;
    color: #eee;
}
.loading .icon
{
    display: inline-block;
    color: #eee;
    animation: spin 2s linear infinite;
}
@keyframes spin
{
    0%
    {
    transform: rotate(0deg);
    }
    100%
    {
    transform: rotate(360deg);
    }
}
</style>           

JavaScript Code

Take a look at the JavaScript Code given below.

<script>
document.getElementById('btn').addEventListener("click", function(){
    this.classList.add("loading");
    this.innerHTML = "Logging In..";
});
</script>

Demo

Video Tutorial

Watch our video tutorial on Button With Loading Animation On Click with CSS and JavaScript.

Pure CSS Custom Radio Buttons Custom Emoji Radio Buttons with CSS Custom Radio Buttons with CSS Engraved HTML Buttons With CSS Neumorphic Buttons Design with CSS Cool Profile Card Design with HTML CSS Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Simple Price Range Slider With HTML CSS and JavaScript Circle With Two Colors using CSS Card With Ribbon using HTML and CSS Simple Loading Screen using HTML and CSS Disable Textarea Resizable Property using CSS Button like Links using CSS Simple Dropdown Menu Using HTML and CSS Background Color Transition using CSS Highlight Required Input Fields using CSS Disable HTML Link using CSS Difference Between Descendant Selector and Child Selector How To Change Color and Size of Hr Tag Using CSS How To Center Align HTML Table Convert Checkbox To Toggle Button Using CSS Drawing Stars using HTML and CSS Button Bounce Effect using CSS Image as Text Background using CSS background-clip Property Vertically Rotate HTML Element using CSS Vertical Align Middle using CSS Text Over Image Hover Effect - Css Hover Simple CSS Button Hover Effect using Before Selector
Get Heading Text using JavaScript Current Date in mm/dd/yyyy Format JavaScript Current Date in dd/mm/yyyy Format in JavaScript JavaScript Current Date in yyyy-mm-dd Format JavaScript Get Date Difference in Hours JavaScript Get Date Difference in Minutes JavaScript Get Date Difference in Seconds Get Last Character of String with JavaScript Trigger Button Click On Page Load JavaScript Get id Of Child Element JavaScript Get Id Of Parent Element in JavaScript Get Table Row Index onclick in JavaScript Remove specific option from Select Tag with JavaScript Change Selected Option of Select Tag with JavaScript Get Length of Text In Textarea with JavaScript Change Text Color of Select Tag Option using JavaScript Change Heading Text in JavaScript Get Input Value Length in JavaScript Toggle Button Text with JavaScript Change Background Color of Dropdown with Dropdown using JavaScript Emoji Mood Slider with HTML CSS and JavaScript Hide and Show Div with Radio Button using JavaScript Count Options In Select Tag with JavaScript Get Select Tag Text OnChange with JavaScript Change Background Color OnClick using Input Field