Magnetic Gradient Button

Add an interactive magnetic gradient button to your website with smooth cursor-following movement, vibrant colors and ready-to-use CSS.
LIVE INTERACTIVE PREVIEW Interactive
Get Started ↗

Move your cursor over the button

MAKE IT YOURS

Get the Code

Copy the code below and add this animation to your website.

<div class="magnetic-demo">
  <a href="#" class="magnetic-button">
    <span>Get Started</span>
    <span class="magnetic-arrow">↗</span>
  </a>
</div>
.magnetic-demo {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 290px;
  background: #F0F5FF;
}

.magnetic-button {
  display: inline-flex;
  align-items: center;
  gap: 13px;
  padding: 17px 27px;
  border-radius: 10px;
  background: linear-gradient(
    110deg, #2563EB, #7C3AED, #2563EB
  );
  background-size: 200% 200%;
  color: #FFFFFF;
  text-decoration: none;
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 10px 25px rgba(37,99,235,.22);
  transition: box-shadow .25s ease,
              background-position .4s ease;
  will-change: transform;
}

.magnetic-button:hover {
  background-position: 100% 50%;
  box-shadow: 0 15px 32px rgba(79,70,229,.3);
}

.magnetic-arrow {
  font-size: 17px;
  transition: transform .25s ease;
}

.magnetic-button:hover .magnetic-arrow {
  transform: translate(3px,-3px);
}
const button = document.querySelector(
  '.magnetic-button'
);

const strength = 0.28;

button.addEventListener('pointermove', function(event) {
  if (event.pointerType === 'touch') return;

  const rect = button.getBoundingClientRect();

  const x = event.clientX - rect.left - rect.width / 2;
  const y = event.clientY - rect.top - rect.height / 2;

  button.style.transform =
    `translate(${x * strength}px, ${y * strength}px)`;
});

button.addEventListener('pointerleave', function() {
  button.style.transform = 'translate(0, 0)';
});
MAKE IT YOURS

Customize the Animation

Personalize the button by changing a few CSS and JavaScript values to match your website.

🎨

Change the Colors

Replace the gradient colors with your preferred brand colors.

background: linear-gradient(110deg, #2563EB, #7C3AED);
↔

Adjust Movement

Control how strongly the button follows your cursor.

const strength = 0.28;
â—»

Change the Shape

Adjust the corner roundness to suit your design.

border-radius: 10px;
✦

Adjust the Size

Change the button padding to make it larger or smaller.

padding: 17px 27px;

Magnetic Gradient Button

Share your experience to help other users make a better choice.

Elegantespace write review

Reviews are checked before being published.