Drawer Image

CodingIndo

Dibuat Oleh Devis Wisley

Make a Ripple Effect on a Button with a Gradient Background in HTML, CSS & JavaScript

Sunday, September 01, 2024

Currency Converter Preview

Create an interactive Button with a Ripple Effect when clicked, which has a dynamic color gradient background. The ripple effect provides interesting visual feedback to users when they interact with the button.

In this blog post, I will walk you through creating an interactive button with an attractive ripple effect and gradient background using HTML, CSS, and JavaScript. We will create a button with a dynamic gradient background that will create a ripple effect when clicked.

  • HTML: Basic structure for buttons and containers.
  • CSS: Visual styles for the button, including a changing gradient background and an animated ripple effect.
  • JavaScript: Logic to handle the ripple effect when the button is clicked, including dynamic creation and removal of ripple elements.
By following this tutorial, you will be able to add interactive and aesthetic elements to your user interface, enhancing the user experience by providing responsive visual feedback. We will touch on aspects such as gradient transitions when the button is hovered and ripple animations that give it a modern and attractive look.

Join us to create buttons that are not only functional but also captivating with stunning visual effects!

As you work on this project, you’ll learn how to structure and style websites effectively to ensure they look great on all devices.

Key Features:

  1. Interactive Ripple Effect: The button displays a subtle and engaging ripple effect every time it is clicked, providing responsive visual feedback to the user.
  2. Dynamic Gradient Background: The button comes with a multi-colored gradient background that changes direction on hover, creating an eye-catching and modern visual effect.
  3. Smooth Transitions: Using CSS transitions makes the background changes and ripple effects feel more natural and blend in with the overall design of the button.
  4. Responsive Design: The ripple effect automatically adjusts its size and position based on where the user clicks on the button, making it an interactive and responsive element.
  5. Easy Customization: Background colors, button sizes, and animation durations are easily customizable, so you can adapt the design to fit the look of your project or website.
  6. Minimal Use of JavaScript: Simple and efficient logic using JavaScript to manage the ripple effect, without the need for external libraries or additional frameworks.

With these features, the ripple button project with gradient background can give a professional touch to your user interface!

Why Make a Ripple Effect on a Button with a Gradient Background in HTML, CSS & JavaScript?

    By creating a ripple effect project on a button with a gradient background using HTML, CSS, and JavaScript, you will gain the following skills:

    • HTML: Understanding how to structure the markup needed to apply visual effects to interactive elements such as buttons.
    • CSS: Learn how to apply complex visual styles, including the use of gradient backgrounds, transitions, and animations. You'll learn how to create a ripple effect with CSS and how to customize a button design for a more engaging look.
    • JavaScript: Hone skills in adding interactivity with JavaScript, including logic to create and manage dynamic ripple elements based on user interactions.
    • Interactive Design: Understand how to integrate responsive and dynamic visual effects to enhance the user experience and provide engaging visual feedback when buttons are clicked.
    • Transitions and Animations: Learn to apply CSS transitions and animations to create smooth and engaging ripple effects, and manage animation duration and effects with JavaScript.

    This project will not only improve your technical skills in web development, but also give you a better understanding of how to combine HTML, CSS, and JavaScript to create professional and aesthetic user interface elements.

Steps to Make a Ripple Effect on a Button with a Gradient Background in HTML, CSS & JavaScript

    Here are the steps to create a ripple effect on a button with a gradient background using HTML, CSS, and JavaScript:

    1. Create HTML File

    • Create an HTML file with a button element inside the container. Add attributes and classes to mark the button that will be given the ripple effect.
index.html
                            
<!DOCTYPE html>
<!-- Coding By CodingIndo - https://codingindo.vercel.app/ -->
<html lang="en">
<head>
    <!-- Meta charset to define character encoding -->
    <meta charset="UTF-8">
    <!-- Meta viewport to set responsive display -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Title for page title -->
    <title>Button Ripple Effect</title>
    <!-- Stylesheet link to link CSS files -->
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- Container for ripple effect button -->
    <div class="button-container">
        <!-- Button with ripple effect -->
        <button class="ripple-button">Click Me</button>
    </div>
    <!-- Script to link JavaScript files -->
    <script src="script.js"></script>
</body>
</html>
                            
                        

Explanation

Number Line of Code Explanation
1 <!DOCTYPE html> Declares the document type and version of HTML being used, in this case, HTML5.
2 <!-- Coding By CodingIndo - https://codingindo.vercel.app/ --> A comment specifying the author and their website.
3 <html lang="en"> The opening tag for the HTML document with the lang attribute set to English.
4 <head> The opening tag for the document head section.
5 <!-- Meta charset to define character encoding --> A comment explaining the purpose of the following meta tag.
6 <meta charset="UTF-8"> Sets the character encoding to UTF-8, ensuring that the page displays special characters correctly.
7 <!-- Meta viewport to set responsive display --> A comment explaining the next meta tag.
8 <meta name="viewport" content="width=device-width, initial-scale=1.0"> Ensures the page is responsive and scales properly on different devices.
9 <!-- Title for page title --> A comment explaining the purpose of the title tag.
10 <title>Button Ripple Effect</title> Sets the title of the web page as shown on the browser tab.
11 <!-- Stylesheet link to link CSS files --> A comment explaining the purpose of the link tag.
12 <link rel="stylesheet" href="styles.css"> Links an external CSS file called styles.css to the document.
13 </head> Closes the head section.
14 <body> The opening tag for the body section, which contains the content displayed on the page.
15 <!-- Container for ripple effect button --> A comment describing the container for the button.
16 <div class="button-container"> Creates a div element with a class named button-container, which can be used for styling or layout purposes.
17 <!-- Button with ripple effect --> A comment explaining the button’s function.
18 <button class="ripple-button">Click Me</button> Creates a button element with the text "Click Me" and the class ripple-button for adding custom styling or JavaScript functionality.
19 </div> Closes the div element.
20 <!-- Script to link JavaScript files --> A comment explaining the purpose of the script tag.
21 <script src="script.js"></script> Links an external JavaScript file named script.js to add interactivity or logic.
22 </body> Closes the body section.
23 </html> Closes the HTML document.

    2. Create CSS File

    • Style the button with a dynamic gradient background and a ripple effect that is visible when the button is clicked.
    • The background gradient is expanded with background-size to create a smooth transition effect when the button is hovered.
    • The ripple effect is added using a pseudo span element which will animate to provide a visual effect when the button is clicked.
style.css
                            
/* Sets the body display to be centered and have a background color */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

/* Sets the relative position for the button container */
.button-container {
    position: relative;
}

/* Set the appearance of the ripple button */
.ripple-button {
    position: relative;
    overflow: hidden;
    background: linear-gradient(45deg, #ff6b6b, #f94d6a, #ffca3a, #6a4df9, #3ab7ff);
    background-size: 300% 300%;
    border: none;
    border-radius: 8px;
    padding: 15px 30px;
    color: white;
    font-size: 18px;
    cursor: pointer;
    outline: none;
    transition: background-position 1s;
}

/* Sets the hover effect on the ripple button */
.ripple-button:hover {
    background-position: right center;
}

/* Sets the after effect on the ripple button */
.ripple-button::after {
    content: '';
    position: absolute;
    border-radius: 50%;
    width: 100px;
    height: 100px;
    background-color: rgba(255, 255, 255, 0.6);
    opacity: 0;
    transform: scale(1);
    transition: transform 0.5s, opacity 1s;
}

/* Sets the ripple span display on the button */
.ripple-button span.ripple {
    position: absolute;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.5);
    transform: scale(0);
    animation: ripple-animation 0.6s linear;
    pointer-events: none;
}

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

Explanation

Number Line of Code Explanation
1 /* Sets the body display to be centered and have a background color */ A comment explaining the styles applied to the body.
2 body { Starts the body styling block.
3 display: flex; Sets the body display mode to flex, enabling flexbox properties.
4 justify-content: center; Horizontally centers the content within the body.
5 align-items: center; Vertically centers the content within the body.
6 height: 100vh; Sets the body height to the full viewport height.
7 margin: 0; Removes any default margin from the body.
8 background-color: #f0f0f0; Sets the background color of the body to a light gray (#f0f0f0).
9 } Ends the body styling block.
10 /* Sets the relative position for the button container */ A comment describing the purpose of .button-container.
11 .button-container { Starts the .button-container styling block.
12 position: relative; Sets the position of the .button-container to relative for positioning child elements.
13 } Ends the .button-container styling block.
14 /* Set the appearance of the ripple button */ A comment explaining the styling for .ripple-button.
15 .ripple-button { Starts the .ripple-button styling block.
16 position: relative; Sets the position to relative for containing the pseudo-element (::after).
17 overflow: hidden; Hides any content that overflows the button boundaries.
18 background: linear-gradient(45deg, #ff6b6b, #f94d6a, #ffca3a, #6a4df9, #3ab7ff); Sets a multi-color gradient background at a 45-degree angle.
19 background-size: 300% 300%; Enlarges the gradient background for smoother animation.
20 border: none; Removes the default border from the button.
21 border-radius: 8px; Rounds the corners of the button.
22 padding: 15px 30px; Adds padding inside the button.
23 color: white; Sets the button text color to white.
24 font-size: 18px; Sets the font size of the button text.
25 cursor: pointer; Changes the cursor to a pointer when hovered over the button.
26 outline: none; Removes the outline when the button is focused.
27 transition: background-position 1s; Adds a smooth transition effect for the background position.
28 } Ends the .ripple-button styling block.
29 /* Sets the hover effect on the ripple button */ A comment explaining the hover effect.
30 .ripple-button:hover { Starts the hover effect styling block.
31 background-position: right center; Moves the background position when hovered to create an animation.
32 } Ends the hover effect styling block.
33 /* Sets the after effect on the ripple button */ A comment explaining the pseudo-element styling.
34 .ripple-button::after { Starts the ::after pseudo-element styling block for .ripple-button.
35 content: ''; Inserts an empty content string.
36 position: absolute; Positions the element absolutely within the button.
37 border-radius: 50%; Gives the element a circular shape.
38 width: 100px; Sets the width of the pseudo-element.
39 height: 100px; Sets the height of the pseudo-element.
40 background-color: rgba(255, 255, 255, 0.6); Sets the background color with transparency.
41 opacity: 0; Makes the element initially transparent.
42 transform: scale(1); Sets the initial scale to normal size.
43 transition: transform 0.5s, opacity 1s; Adds transitions for scale and opacity.
44 } Ends the pseudo-element styling block.
45 /* Sets the ripple span display on the button */ A comment explaining the styling for the ripple span.
46 .ripple-button span.ripple { Starts the .ripple span styling block.
47 position: absolute; Positions the span absolutely within the button.
48 border-radius: 50%; Gives the span a circular shape.
49 background-color: rgba(255, 255, 255, 0.5); Sets a semi-transparent white background.
50 transform: scale(0); Sets the initial scale to 0 for animation.
51 animation: ripple-animation 0.6s linear; Applies the ripple-animation for 0.6s in a linear fashion.
52 pointer-events: none; Prevents the span from capturing pointer events.
53 } Ends the .ripple span styling block.
54 /* Set the ripple animation */ A comment explaining the keyframe animation.
55 @keyframes ripple-animation { Starts the keyframes block for the ripple-animation.
56 to { Specifies the end state of the animation.
57 transform: scale(4); Expands the element 4 times its original size.
58 opacity: 0; Makes the element fully transparent by the end of the animation.
59 } Ends the to keyframe block.
60 } Ends the @keyframes block.

    3. Create JavaScript File

    • Add a JavaScript script to handle the ripple effect logic. When the button is clicked, a span element with the class .ripple.
    • Once the animation is complete, the ripple element is removed from the DOM to prevent stacking of elements.
script.js
                            
// Add an event listener for clicks on elements with class 'ripple-button'
document.querySelector('.ripple-button').addEventListener('click', function (e) {
    // Create a new 'span' element for the ripple effect
    const ripple = document.createElement('span');
    // Get the clicked button element
    const button = e.currentTarget;
    // Get the size and position of the button
    const rect = button.getBoundingClientRect();
    // Calculate ripple size based on button size
    const size = Math.max(rect.width, rect.height);
    // Calculate the x position of the ripple
    const x = e.clientX - rect.left - size / 2;
    // Calculate the y position of the ripple
    const y = e.clientY - rect.top - size / 2;
    // Set the ripple size
    ripple.style.width = ripple.style.height = `${size}px`;
    // Set the left position of the ripple
    ripple.style.left = `${x}px`;
    // Set the top position of the ripple
    ripple.style.top = `${y}px`;
    // Add 'ripple' class to span element
    ripple.classList.add('ripple');
    // Add ripple element to the button
    button.appendChild(ripple);
    // Remove the ripple element after the animation is complete
    ripple.addEventListener('animationend', () => {
        ripple.remove();
    });
});
                            
                        

Explanation

Number Line of Code Explanation
1 // Add an event listener for clicks on elements with class 'ripple-button' A comment explaining that an event listener will be added to handle click events on elements with the class ripple-button.
2 document.querySelector('.ripple-button').addEventListener('click', function (e) { Selects the first element with the class ripple-button and adds a click event listener that triggers an anonymous function when the button is clicked.
3 // Create a new 'span' element for the ripple effect A comment explaining the creation of a new span element for the ripple effect.
4 const ripple = document.createElement('span'); Creates a new span element and assigns it to the variable ripple.
5 // Get the clicked button element A comment explaining that the current button being clicked is retrieved.
6 const button = e.currentTarget; Assigns the button that was clicked (e.currentTarget) to the variable button.
7 // Get the size and position of the button A comment explaining that the button's size and position are being retrieved.
8 const rect = button.getBoundingClientRect(); Retrieves the size and position of the button relative to the viewport and assigns it to rect.
9 // Calculate ripple size based on button size A comment explaining how the size of the ripple is calculated.
10 const size = Math.max(rect.width, rect.height); Calculates the ripple's size as the larger of the button's width or height.
11 // Calculate the x position of the ripple A comment explaining how the x-coordinate for the ripple's position is calculated.
12 const x = e.clientX - rect.left - size / 2; Calculates the x-coordinate for the ripple's position relative to the button.
13 // Calculate the y position of the ripple A comment explaining how the y-coordinate for the ripple's position is calculated.
14 const y = e.clientY - rect.top - size / 2; Calculates the y-coordinate for the ripple's position relative to the button.
15 // Set the ripple size A comment explaining that the size of the ripple element is being set.
16 ripple.style.width = ripple.style.height = `${size}px`; Sets both the width and height of the ripple element to the calculated size.
17 // Set the left position of the ripple A comment explaining that the left position of the ripple is being set.
18 ripple.style.left = `${x}px`; Sets the left CSS property of the ripple element.
19 // Set the top position of the ripple A comment explaining that the top position of the ripple is being set.
20 ripple.style.top = `${y}px`; Sets the top CSS property of the ripple element.
21 // Add 'ripple' class to span element A comment explaining that the ripple class is added to the element for styling and animation.
22 ripple.classList.add('ripple'); Adds the ripple class to the ripple element.
23 // Add ripple element to the button A comment explaining that the ripple element is appended to the button.
24 button.appendChild(ripple); Appends the ripple element to the button as a child.
25 // Remove the ripple element after the animation is complete A comment explaining that the ripple element will be removed after the animation finishes.
26 ripple.addEventListener('animationend', () => { Adds an event listener to the ripple element that listens for the animationend event.
27 ripple.remove(); Removes the ripple element from the DOM once the animation ends.
28 }); Closes the event listener's function block.
29 }); Closes the click event listener function block.

    5. Run Your Project

    • Open the "index.html" file in a browser.
    • Click the button to see the ripple effect with gradient background in action.

    Additional Notes

    1. Color Customization: You can customize the gradient background color according to your project's color scheme to create a consistent and attractive look.
    2. Performance Optimization: Make sure the ripple element is removed from the DOM after the animation is complete to maintain application performance and prevent unnecessary element buildup.
    3. Browser Compatibility: Test the ripple effect in various browsers to ensure compatibility, as some CSS and JavaScript features may behave differently depending on the browser used.
    4. Responsiveness: If the button will be used on devices with smaller screens, consider changing the button size and responsiveness to accommodate different screen sizes.
    5. Transitions and Animations: You can experiment with transition durations and ripple animations to create more unique effects or customize them to your design needs.

    By keeping these additional notes in mind, you can ensure that the ripple effect on your buttons is not only visually appealing but also optimal for user experience.

Conclusion and final words

In this tutorial, we have learned how to create a button with an attractive ripple effect and gradient background using HTML, CSS, and JavaScript. By following the steps outlined, you can enhance the user experience on your app or website by providing dynamic visual feedback.

  • HTML Provides the basic structure for the button to be given an effect.
  • CSS Sets the visual style of the button, including gradient backgrounds and ripple effects with animation.
  • JavaScript Handles the logic for creating and removing ripple effects based on user clicks.
The ripple effect with a gradient background not only makes the button look more attractive but also provides an interactive response that increases user engagement. Experimenting with different colors and gradients can further personalize the look of the button according to the overall design of your site or app.

Applying visual effects like ripples to buttons can make the user interface more lively and engaging. Feel free to explore and customize the design according to your project needs. Hopefully this tutorial helps and inspires you in creating more creative and interactive web designs.

If you have any further questions or need additional help, don’t hesitate to ask. Good luck and success with your web design project!

If you are having trouble creating a ripple effect on a button with a gradient background using HTML, CSS, and JavaScript, you can download the source code file for this project for free by clicking the “Download” button. You can also view a live demo of this ripple effect by clicking the “View Live” button to see how the button design and interactivity work in practice.

By accessing the source code file and the live demo, you can easily understand how the code implementation works and customize it to suit your needs. Feel free to explore the code, experiment with different styles, and modify features to deepen your skills in HTML, CSS, and JavaScript.

This is a great opportunity to learn more about creating CSS animations and transitions, as well as integrating JavaScript into interactive design. Use this project to enhance your understanding of dynamic visual elements and modern web design. Happy learning and experimenting!

View Live Demo
Devis Wisley

DevCode

Pendiri & Pengembang CodingIndo

Devis Wisley atau yang dikenal sebagai DevCode merupakan seorang Founder & Developer CodingIndo, sebuah komunitas dan platform edukasi teknologi yang berfokus pada pengembangan keterampilan pemrograman. Dengan passion-nya di dunia coding, DevCode berkomitmen untuk membantu para developer lokal meningkatkan kemampuan teknis melalui konten edukatif, tutorial, dan kolaborasi open-source.

Dibawah kepemimpinannya, CodingIndo terus berkembang menjadi wadah bagi programmer pemula maupun profesional untuk saling berbagi ilmu, mengikuti tren teknologi terbaru, dan membangun jaringan di industri IT. Selain aktif mengembangkan proyek-proyek inovatif, DevCode juga sering membagikan insight seputar pengembangan software, tips karir tech, serta best practices dalam dunia pemrograman.

Dengan semangat "Collaborate, Learn, and Grow Together", DevCode dan CodingIndo berupaya menciptakan ekosistem teknologi Indonesia yang lebih inklusif dan kompetitif di kancah global.
Web Development Education Open Source Tech Community Programming
0 Comment

LEAVE A REPLY

Buy Me A Coffee
Like my content? Support with a coffee!