Skip to content

Magnetic Button Hover Effect

Buttons that pull toward the mouse cursor when it comes near and spring back on leave, driven by CSS custom properties and a tiny script.

Also known as: magnetic button effect, button follows mouse cursor, cursor attraction button, magnetic hover animation, interactive button hover

Magnetic Button Hover Effect live demo

Open in new tab

Pure CSS code (HTML, CSS, JS)

A magnetic button hover effect built with CSS custom properties and a few lines of vanilla JavaScript. When the mouse comes close, the button leans toward the cursor, its label follows a little further, and both spring back when the pointer moves away. Use it for hero calls to action, play buttons or portfolio links that should feel playful.

How it works

The root section.mgb-stage contains an intro and a .mgb-row of three .mgb-field wrappers. Each field holds one .mgb-btn (a solid pill, a ghost outline and a round play button) with an inner .mgb-label.

The CSS moves the button with transform: translate3d(var(--mgb-x), var(--mgb-y), 0) and moves the label by half of the same offset, which creates a subtle parallax inside the button. The script listens to pointermove on the document and batches work with requestAnimationFrame. For each field it measures the distance between the cursor and the field centre (the field never moves, so this is the button's resting position). If the cursor is within data-mgb-radius pixels of the button edge, it sets --mgb-x and --mgb-y to the distance multiplied by data-mgb-strength and adds .is-active, which shortens the transition so the button follows the pointer closely. Outside the radius the offsets return to zero and the --mgb-ease curve adds a small overshoot as the button snaps back.

The effect only runs for a mouse on devices with a fine pointer. Touch users, keyboard users and visitors without JavaScript get normal static buttons.

Customization

  • data-mgb-radius on .mgb-field: how many pixels around the button the pull starts, e.g. 150 for a wider field.
  • data-mgb-strength on .mgb-field: how far the button follows the cursor; 0.2 is subtle, 0.6 is strong.
  • --mgb-return on .mgb-stage: how long the button takes to spring back to its place.
  • --mgb-ease: the snap back curve; a value above 1 in the cubic bezier produces the overshoot.
  • --mgb-accent and --mgb-accent-ink: the lime color of the solid button and its text color.
  • Change the 0.5 multiplier in the .mgb-label transform to make the label parallax stronger or remove it.
  • Use .mgb-btn--solid, .mgb-btn--ghost or .mgb-btn--round on any link or button to pick a style.

Use cases

  • Hero sections: a call to action leans toward the cursor and springs back when the pointer leaves
  • Portfolio sites: ghost and solid pill links feel playful with a subtle label parallax
  • Video landing pages: a round play button pulls toward the mouse as it approaches
  • Creative agency sites: the effect runs only for fine pointers, so touch visitors see static buttons

Accessibility

The buttons are real a and button elements, so keyboard and screen reader behavior is unchanged. The icon-only play button has an aria-label, and its SVG is hidden from assistive technology. Every button shows a visible focus outline. Under prefers-reduced-motion: reduce the script skips the effect and the CSS removes all transforms.

Usage notes

Any number of fields can be placed on a page; the single document listener handles all of them. Leave some space around each button so the moving button does not overlap neighbors.

Magnetic Button Hover Effect at a glance

Framework
Pure CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
None
Layout
Responsive, works on mobile, dark design
Category
Hover Effects
License
Free for personal and commercial use

Questions about this template

Can I use the Magnetic Button Hover Effect code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

There is nothing to install. Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

No dependencies. Pure HTML, CSS and JavaScript.

Magnetic Button Hover Effect – free Pure CSS hover effect template
Magnetic Button Hover Effect · free Pure CSS hover effect template

More Hover Effects

Link Underline Effects – free Pure CSS hover effect template

Link Underline Effects

Six animated link underline styles in pure CSS (slide, center, highlighter, ink fill, line swap and wave) that also work on links that wrap.

Image Hover Effects – free Pure CSS hover effect template

Image Hover Effects

Six pure CSS image hover effects with captions: zoom, grayscale to color, sliding panel, tilt, blur and frame, also revealed by keyboard focus.