Ripple Button Effect in HTML CSS & JavaScript

When web developers create user interfaces, they often use CSS and JavaScript to add special effects. One popular effect is the ripple button effect, which gives buttons a modern and sleek look with dynamic animation. This article will show you how to create a ripple button effect using HTML, CSS, and JavaScript.

Today in this blog you’ll learn how to create Button Ripple Effect in HTML CSS & JavaScript.

Before we dive into the code, it’s important to understand the concept behind the ripple button effect. This effect mimics the animation of ripples on the surface of water, where a small disturbance creates a ripple that gradually fades away. Similarly, when a user clicks on a button with this effect, a ripple-like animation will appear at the point of the click and then gradually fade away, creating a visually appealing interaction.

Create Button Ripple Effect in HTML CSS & JavaScript

Create a Folder: Start by creating a folder for your project. You can name it anything you like. Inside this folder, you’ll need to create the following files:

  • index.html (for the structure of your Ripple Effect Button)
  • style.css (for styling the Ripple Effect Button)

Create the HTML File:

  • Name your HTML file index.html.

Create the CSS File:

  • Name your CSS file style.css.

Create the JavaScript File:

  • Name your JavaScript file script.js.

So, let’s get started with the code. First, we need to create a HTML structure for our button.

				
					<!DOCTYPE html>
<!-- NotArena || www.NotArena.com -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Button Ripple Effect</title>
    <link rel="stylesheet" href="style.css">
<style id="wpr-lazyload-bg-container"></style><style id="wpr-lazyload-bg-exclusion"></style>
<noscript>
<style id="wpr-lazyload-bg-nostyle">.ultp-block-wrapper .slick-loading .slick-list{--wpr-bg-3204cc1c-ffec-4d1d-ae21-161e102a1c76: url('https://notarena.com/wp-content/plugins/ultimate-post/assets/img/loading.gif');}.elementor-1911 .elementor-element.elementor-element-2e43d051::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-background-video-container::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .e-con-inner > .elementor-background-video-container::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-background-slideshow::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .e-con-inner > .elementor-background-slideshow::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-motion-effects-container > .elementor-motion-effects-layer::before{--wpr-bg-904a2669-cbd6-4a2a-9920-2736234a7416: url('https://i0.wp.com/notarena.com/wp-content/uploads/2024/10/BG.png?fit=1200%2C726&ssl=1');}.rll-youtube-player .play{--wpr-bg-92badcf5-f618-433a-bdbd-ee8ca7b57c1c: url('https://notarena.com/wp-content/plugins/wp-rocket/assets/img/youtube.png');}</style>
</noscript>
<script type="application/javascript">const rocket_pairs = [{"selector":".ultp-block-wrapper .slick-loading .slick-list","style":".ultp-block-wrapper .slick-loading .slick-list{--wpr-bg-3204cc1c-ffec-4d1d-ae21-161e102a1c76: url('https:\/\/notarena.com\/wp-content\/plugins\/ultimate-post\/assets\/img\/loading.gif');}","hash":"3204cc1c-ffec-4d1d-ae21-161e102a1c76","url":"https:\/\/notarena.com\/wp-content\/plugins\/ultimate-post\/assets\/img\/loading.gif"},{"selector":".elementor-1911 .elementor-element.elementor-element-2e43d051, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-background-video-container, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .e-con-inner > .elementor-background-video-container, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-background-slideshow, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .e-con-inner > .elementor-background-slideshow, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-motion-effects-container > .elementor-motion-effects-layer","style":".elementor-1911 .elementor-element.elementor-element-2e43d051::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-background-video-container::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .e-con-inner > .elementor-background-video-container::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-background-slideshow::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .e-con-inner > .elementor-background-slideshow::before, .elementor-1911 .elementor-element.elementor-element-2e43d051 > .elementor-motion-effects-container > .elementor-motion-effects-layer::before{--wpr-bg-904a2669-cbd6-4a2a-9920-2736234a7416: url('https:\/\/i0.wp.com\/notarena.com\/wp-content\/uploads\/2024\/10\/BG.png?fit=1200%2C726&ssl=1');}","hash":"904a2669-cbd6-4a2a-9920-2736234a7416","url":"https:\/\/i0.wp.com\/notarena.com\/wp-content\/uploads\/2024\/10\/BG.png?fit=1200%2C726&ssl=1"},{"selector":".rll-youtube-player .play","style":".rll-youtube-player .play{--wpr-bg-92badcf5-f618-433a-bdbd-ee8ca7b57c1c: url('https:\/\/notarena.com\/wp-content\/plugins\/wp-rocket\/assets\/img\/youtube.png');}","hash":"92badcf5-f618-433a-bdbd-ee8ca7b57c1c","url":"https:\/\/notarena.com\/wp-content\/plugins\/wp-rocket\/assets\/img\/youtube.png"}]; const rocket_excluded_pairs = [];</script></head>
<body>
    <div class="container">
        <button class="ripple-button">Click Me</button>
    </div>
    <script src="script.js" data-rocket-defer defer></script>
<script>var rocket_beacon_data = {"ajax_url":"https:\/\/notarena.com\/wp-admin\/admin-ajax.php","nonce":"21aa70768f","url":"https:\/\/notarena.com\/code-library\/ripple-button-effect-in-html-css-javascript","is_mobile":false,"width_threshold":1600,"height_threshold":700,"delay":500,"debug":null,"status":{"atf":true,"lrc":true},"elements":"img, video, picture, p, main, div, li, svg, section, header, span","lrc_threshold":1800}</script><script data-name="wpr-wpr-beacon" src='http://notarena.com/wp-content/plugins/wp-rocket/assets/js/wpr-beacon.min.js' async></script></body>
</html>
				
			

Second, we will add some CSS properties to give our button a basic styling. We can add a background colour, border, and padding to give it a solid colour and shape. We can also add a “transition“ property to make the animation smooth and gradual. Now comes the fun part – adding the ripple effect.

				
					* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #e7e7e7;
    font-family: Arial, sans-serif;
}

.container {
    display: flex;
    justify-content: center;
    align-items: center;
}.ripple {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.6);
    animation: ripple-animation 0.6s linear;
    transform: scale(0);
}

@keyframes ripple-animation {
    to {
        transform: scale(4);
        opacity: 0;
    }
}



.ripple-button {
    position: relative;
    padding: 15px 40px;
    font-size: 18px;
    color: white;
    background-color: #8400cb;
    border: none;
    border-radius: 100px;
    outline: none;
    cursor: pointer;
    overflow: hidden;
    transition: background-color 0.3s ease;
}

.ripple-button:hover {
    background-color: #8400cb;
}

.ripple-button::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    opacity: 1;
}

@media (max-width: 768px) {
    .ripple-button {
        font-size: 16px;
        padding: 12px 30px;
    }
}

				
			

Now that we have created the basic structure and animation, we can add a JavaScript event listener to trigger the ripple effect when the button is clicked. We can achieve this by selecting the button element and adding an event listener for the “click“ event. Within the event handler, we will add a class to the button element, which will trigger the CSS animation.

				
					 document.querySelector('.ripple-button').addEventListener('click', function(e) {
const button = e.currentTarget;
const circle = document.createElement('span');
const diameter = Math.max(button.clientWidth, button.clientHeight);
const radius = diameter / 2;

circle.style.width = circle.style.height = `${diameter}px`;
circle.style.left = `${e.clientX - button.offsetLeft - radius}px`;
circle.style.top = `${e.clientY - button.offsetTop - radius}px`;
circle.classList.add('ripple');

const ripple = button.getElementsByClassName('ripple')[0];

if (ripple) {
    ripple.remove();
}

button.appendChild(circle);
});

				
			

That’s all, now you’ve successfully created a  Ripple Button Effect in HTML CSS & JavaScript. the ripple button effect is a simple yet effective way to add a touch of interactivity to your web design. It’s a great way to make your buttons stand out and provide a more engaging experience for users. With the combination of HTML, CSS, and JavaScript, you can easily implement this effect into your projects and take your UI design to the next level. 

If the code doesn’t work or if you run into any issues, please leave a comment below or reach out to us through the contact page.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Plugins

Send this to a friend