Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to make Magnetic Button Hover Effect using HTML & CSS

    6 October 2025

    How to make Animated Electric Card using HTML & CSS

    4 October 2025

    How to make Next Level Electric Button Animation using HTML & CSS

    2 October 2025
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - JavaScript - How to make Magnetic Button Hover Effect using HTML & CSS
    JavaScript

    How to make Magnetic Button Hover Effect using HTML & CSS

    Coding StellaBy Coding Stella6 October 2025No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    Let’s create a Magnetic Button Hover Effect using HTML, CSS, and JavaScript. This project will feature a button that reacts to the movement of your mouse – giving the illusion that it’s being “pulled” or “attracted” by the cursor like a magnet.

    We’ll use:

    • HTML to structure the button.
    • CSS to style it with smooth transitions and modern visuals.
    • JavaScript to detect mouse movement and create the magnetic attraction effect.

    This fun and interactive effect adds depth and engagement to any website button. Whether you’re a beginner or an experienced developer, this project is a great way to practice DOM interactions and animations. Let’s make your buttons come alive with magnet-like motion! ⚡🧲

    HTML :

    This HTML sets up a simple webpage for a magnetic button hover effect demo. It links to external CSS (style.css) and JavaScript (script.js) files and loads the GSAP animation library. The body contains two cursor elements (#cursor and #cursorPt) that replace the default mouse pointer and a centered button-like div (#target) with the text “Hover me.” When combined with the CSS and JS, this setup creates a custom animated cursor that reacts when hovering over the button.

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Magnetic Button Hover Effect | @coding.stella</title>
      <link rel="stylesheet" href="./style.css">
    </head>
        
    <body>
      <div id="cursor"></div>
      <div id="cursorPt"></div>
      <div id="target">Hover me</div>
    
      <script src="https://unpkg.com/gsap@3/dist/gsap.min.js"></script>
      <script src="./script.js"></script>
    </body>
      
    </html>

    CSS :

    This CSS creates a fullscreen dark gradient background with a custom glowing cursor and a centered interactive box. The default cursor is hidden (cursor: none), and two custom elements (#cursorPt and #cursor) act as the cursor — a small glowing dot and a decorative square outline that follows the mouse. The #target box is centered using flexbox, styled with borders and text, and smoothly changes color when hovered, giving a futuristic glowing hover effect.

    body {
      margin: 0;
      height: 100vh;
      overflow: hidden;
      background: linear-gradient(#111, #000);
      display: flex;
      cursor: none;
    }
    
    #cursorPt {
      position: fixed;
      width: 7px;
      height: 7px;
      pointer-events: none;
      z-index: 9999;
      background: #beffcb;
      border-radius: 50%;
      visibility: hidden;
    }
    
    #cursor {
      --color: #68fd85;
      position: fixed;
      width: 30px;
      height: 30px;
      pointer-events: none;
      z-index: 9999;
      visibility: hidden;
    
      background-image: linear-gradient(to right, var(--color) 10px, transparent 0),
        linear-gradient(to bottom, var(--color) 10px, transparent 0),
        linear-gradient(to left, var(--color) 10px, transparent 0),
        linear-gradient(to bottom, var(--color) 10px, transparent 0),
        linear-gradient(to right, var(--color) 10px, transparent 0),
        linear-gradient(to top, var(--color) 10px, transparent 0),
        linear-gradient(to left, var(--color) 10px, transparent 0),
        linear-gradient(to top, var(--color) 10px, transparent 0);
    
      background-repeat: no-repeat;
      background-position: top left, top left, top right, top right, bottom left,
        bottom left, bottom right, bottom right;
      background-size: 20px 2px, 2px 20px, 20px 2px, 2px 20px, 20px 2px, 2px 20px,
        20px 2px, 2px 20px;
    }
    
    #target {
      position: relative;
      width: 150px;
      height: 80px;
      margin: auto;
      border: 2px solid #888;
      transition: 300ms;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #888;
      text-transform: uppercase;
      font-family: sans-serif;
      user-select: none;
    }
    
    #target:hover {
      border-color: #ccc;
      color: #ddd;
    }

    JS :

    This JavaScript code creates an animated custom cursor using GSAP. It replaces the default cursor with two elements – a glowing dot (cursorPt) and a rotating square (cursor). The cursor follows the mouse smoothly, and when you hover over the #target box, the rotation stops, and the square expands to match the box size while changing color and position slightly based on mouse movement. When the mouse leaves the target, the cursor shrinks back and starts rotating again, creating a dynamic, interactive hover animation.

    const cursor = document.getElementById("cursor");
    const cursorPt = document.getElementById("cursorPt");
    const target = document.getElementById("target");
    
    const CURSOR_WIDTH = cursor.getBoundingClientRect().width;
    const CURSOR_PT_WIDTH = cursorPt.getBoundingClientRect().width;
    
    let isOverTarget = false;
    let rotationTween;
    let exitTween = null;
    let enterTween = null;
    
    function startRotation() {
      gsap.set(cursor, { rotation: 0 });
      rotationTween = gsap.to(cursor, {
        rotation: 180,
        duration: 1.2,
        repeat: -1,
        ease: "linear",
        transformOrigin: "center center"
      });
    }
    
    function stopRotation() {
      if (rotationTween) rotationTween.kill();
    }
    
    document.addEventListener("mousemove", (e) => {
      gsap.to(cursor, {autoAlpha: 1});
        gsap.to(cursorPt, {autoAlpha: 1});
      if (!isOverTarget) {
        gsap.to(cursor, {
          x: e.clientX - CURSOR_WIDTH / 2,
          y: e.clientY - CURSOR_WIDTH / 2,
          duration: 0.1,
          ease: "expo.out"
        });
      }
      gsap.to(cursorPt, {
        x: e.clientX - CURSOR_PT_WIDTH/2,
        y: e.clientY- CURSOR_PT_WIDTH/2,
        duration: 0.1,
        ease: "expo.out"
      });
    });
    
    target.addEventListener("mouseenter", () => {
      isOverTarget = true;
      stopRotation();
    
      const rect = target.getBoundingClientRect();
      
      if (exitTween) exitTween.kill();
      enterTween = gsap.to(cursor, {
        width: rect.width,
        height: rect.height,
        borderColor: "red",
        rotation: 360,
        duration: 0.2,
        ease: "easeOut"
      });
    });
    
    target.addEventListener("mousemove", (e) => {
      const rect = target.getBoundingClientRect();
    
      const targetWidth = rect.width;
      const targetHeight = rect.height;
    
      const cx = rect.left + targetWidth / 2;
      const cy = rect.top + targetHeight / 2;
    
      const dx = e.clientX - cx;
      const dy = e.clientY - cy;
    
      gsap.to(cursor, {
        x: rect.left + dx * 0.09,
        y: rect.top + dy * 0.09,
        scale: 1.1,
        duration: 0.1,
        ease: "power2.out"
      });
    });
    
    target.addEventListener("mouseleave", () => {
      isOverTarget = false;
      
      exitTween = gsap.to(cursor, {
        width: 30,
        height: 30,
        duration: 0.5,
        ease: "elastic.out(1, .9)"
      });
    
      startRotation();
    });
    
    startRotation();

    In conclusion, building a Magnetic Button Hover Effect using HTML, CSS, and JavaScript is a creative way to enhance user interaction and make your UI feel more dynamic. By combining simple styling with mouse movement logic, we’ve created a smooth, responsive, and eye-catching effect 💫

    If your project has problems, don’t worry. Just click to download the source code and face your coding challenges with excitement. Have fun coding!

    Button hover
    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleHow to make Animated Electric Card using HTML & CSS
    Coding Stella
    • Website

    Related Posts

    JavaScript

    How to create Heart and Star Animation using HTML CSS and JS

    27 September 2025
    JavaScript

    How to create Cards Beam Animation using HTML CSS and JS

    25 September 2025
    JavaScript

    How to create Order Button Animation using HTML CSS and JS

    22 September 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202429K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202426K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202421K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202414K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    How to make Awesome Search Bar using HTML & CSS

    14 January 2024

    How to make Glowing Animated Login Form using HTML & CSS

    25 February 2025

    How to create Smooth Image Slider using HTML CSS & JavaScript

    30 January 2025

    How to make QR Code Generator using HTML CSS & JavaScript

    14 January 2024
    Latest Post

    How to make Magnetic Button Hover Effect using HTML & CSS

    6 October 2025

    How to make Animated Electric Card using HTML & CSS

    4 October 2025

    How to make Next Level Electric Button Animation using HTML & CSS

    2 October 2025

    How to make Animated Chair Product Page using HTML & CSS

    30 September 2025
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2025 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood