Ripple effect is a part of the modern design trend. You have seen it on many websites specially on Google’s material design language. It gives a button pressing effect. We can make a ripple effect by adding and animating a child element to the button. We can also position it according to the position of the cursor on the button using Javascript.
- Basic styling: Add basic styling to the button with a
position:relativeattribute to position the inner span tag andoverflow:hiddento prevent span going outside of button.<!DOCTYPE html><html><head><title>Button Ripple Effect - GFG</title><style>/* Adding styles to button */.btn {padding: 12px 50px;border: none;border-radius: 5px;background-color: #1abc9c;color: #fff;font-size: 18px;outline: none;cursor: pointer;/* We need this to positionspan inside button */position: relative;overflow: hidden;box-shadow: 6px 7px 40px -4pxrgba(0, 0, 0, 0.2);}</style></head><body><buttonclass="btn">Enter GeeksforGeeks</button></body></html>chevron_rightfilter_noneOutput:
- Add styling to the span element: Now adding the style for the span element that will show up on the click of a button.
<style>.btn span {position:absolute;border-radius:50%;/* To make it round */background-color: rgba(0,0,0,0.3);width:100px;height:100px;margin-top:-50px;/* for positioning */margin-left:-50px;animation: ripple1s;opacity:0;}/* Add animation */@keyframes ripple {from {opacity:1;transform: scale(0);}to {opacity:0;transform: scale(10);}}</style>chevron_rightfilter_none - Adding JavaScript: Now we’ll add the span element on button click with position according to the mouse click. On button click we have to do the following:
- Create
spanelement and add ripple class to it. - Get the clicked position of cursor using
eventvariable. - Set the position of the span element.
- Remove the span element to avoid spamming of span elements in button.
<script>const btn = document.querySelector(".btn");// Listen for click eventbtn.onclick =function(e) {// Create span elementlet ripple = document.createElement("span");// Add ripple class to spanripple.classList.add("ripple");// Add span to the buttonthis.appendChild(ripple);// Get position of Xlet x = e.clientX - e.target.offsetLeft;// Get position of Ylet y = e.clientY - e.target.offsetTop;// Position the span elementripple.style.left = `${x}px`;ripple.style.top = `${y}px`;// Remove span after 0.3ssetTimeout(() => {ripple.remove();}, 300);};</script>chevron_rightfilter_none - Create
The final output will be look something like below:

Recommended Posts:
- How to add a pressed effect on button click in CSS?
- How to set radio button checked by button click in AngularJS ?
- How to create button dynamically with click event in Angular ?
- How to Create an Effect to Change Button Color using HTML and CSS ?
- How to create a radio button similar to toggle button using Bootstrap ?
- How to call PHP function on the click of a Button ?
- How to load notification alert on top right corner without click of button in bootstrap ?
- How to click a button on webpage using selenium ?
- How to change the href value of <a> tag after click on button using JavaScript ?
- How to add input fields dynamically on button click in AngularJS ?
- How to set checkbox checked on button click in AngularJS ?
- How to Design Stitched Glowing Effect for Button using HTML and CSS ?
- How to create Glowing Star effect using HTML and CSS?
- How to create slide left and right toggle effect using jQuery?
- How to create fade-in effect on page load using CSS ?
- How to Create Paradoxical Effect using CSS ?
- How to Create Liquid Filing Effect on Text using HTML and CSS ?
- How to create shock wave or explosion effect using HTML and CSS ?
- How to Create Skeleton Screen Loading Effect using CSS ?
- How to Create Loading Blur Text Animation Effect using HTML and CSS ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


