HomeCSSButton Animation3D Button With HTML, CSS & Javascript

3D Button With HTML, CSS & Javascript

Introduction

Looking to add some flair to your website’s buttons? In this tutorial, we’ll create a 3D button that’s visually appealing, interactive, and responds dynamically to user clicks. This project combines HTML, CSS, and JavaScript to produce a button with a realistic pressed effect when clicked.

This is a great beginner-friendly project to help you practice CSS transitions, pseudo-classes, and basic JavaScript event handling.

Video Tutorial:

I’ve created a step-by-step video tutorial to guide you through building this 3D button. The video explains:

  • How to use box-shadow for a 3D effect.
  • Create smooth animations with CSS transitions.
  • How to write JavaScript for real-time interactivity.

What You Will Learn:

  • Styling a button to appear 3D using CSS.
  • Adding a pressed effect with CSS transitions and JavaScript.
  • Writing simple JavaScript to change the button text dynamically.

    The Code

    HTML

    The HTML is straightforward, featuring a single <button> element styled to look like a 3D button.

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>3D Button</title>
        <!-- Stylesheet -->
        <link rel="stylesheet" href="style.css" />
      </head>
      <body>
        <button class="button-3d">Click Me</button>
        <script src="script.js"></script>
      </body>
    </html>
    

    CSS

    The CSS is where the magic happens. We use box-shadow and transitions to create the 3D effect and simulate a pressed button when clicked.

    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #f3f3f3;
      margin: 0;
    }
    .button-3d {
      background-color: #3d91f5;
      color: #ffffff;
      font-size: 1.3rem;
      font-weight: bold;
      padding: 30px 60px;
      border: none;
      outline: none;
      border-radius: 8px;
      cursor: pointer;
      box-shadow: 0 14px 0px #013472, 0 8px 15px rgba(0, 0, 0, 0.2);
      transition: transform 0.2s ease, box-shadow 0.2s ease;
    }
    .button-3d:active {
      transform: translateY(2px);
      box-shadow: 0px 6px 0px #013472, 0 6px 10px rgba(0, 0, 0, 0.2);
    }
    


    What You Will Learn:

    • Styling a button to appear 3D using CSS.
    • Adding a pressed effect with CSS transitions and JavaScript.
    • Writing simple JavaScript to change the button text dynamically.

    Explanation of Key CSS Features:

    1. Box Shadow:
      • The box-shadow property creates the illusion of depth, making the button look raised.
      • Two shadows are used: one for the solid base color and another for a subtle, blurred shadow to enhance realism.
    2. Transition:
      • Smooth animations for the button press effect are achieved using transition.
    3. Active State:
      • The :active pseudo-class reduces the shadow size and slightly moves the button down using transform: translateY(2px). Therefore this mimics the effect of physically pressing a button.

        JavaScript

        With JavaScript, we add interactivity by dynamically changing the button text when it’s clicked.

        const button = document.querySelector(".button-3d");
        
        button.addEventListener("click", () => {
          button.textContent = "Clicked!";
          setTimeout(() => (button.textContent = "Click Me"), 1000); // Revert text after 1 second
        });
        

        How It Works:

        • Event Listener:
          • A click event listener is added to the button.
          • When clicked, the button’s text changes to “Clicked!”
        • Timeout Function:
          • After 1 second, the button text reverts to “Click Me” using setTimeout.

        How It Works Together

        1. Initial State:
          • The button is styled with a raised 3D effect using box-shadow.
        2. Click Action:
          • When clicked, the :active pseudo-class is triggered, creating a pressed effect.
          • Simultaneously, JavaScript updates the button text.
        3. Smooth Transition:
          • CSS transitions ensure the press and release actions appear smooth and natural.

        Customization Ideas

        Here are some ways to make this project your own:

        • Change Colors: Customize the button and shadow colors to match your website’s theme.
        • Add Hover Effects: Enhance interactivity by changing the background or shadow on hover.
        • Use Icons: Add an icon inside the button for extra flair.
        • Sound Effects: Play a sound when the button is clicked for a fun touch.

    Conclusion

    This 3D button project is a fantastic way to level up your CSS and JavaScript skills. It’s easy to build, visually stunning, and a perfect addition to any website or project.

    Try it out today, and don’t forget to check out the video tutorial for hands-on learning!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4 × one =

Most Popular