The pill leans toward the pointer as it approaches, and the label drifts a little further for depth. Nine invisible cells around it set --x and --y through sibling selectors, so no JavaScript runs.
Also known as: magnetic button css, button follows cursor, magnetic hover without javascript.
<button class="hfx-magnetic-button"> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-cell" aria-hidden="true"></span> <span class="hfx-magnetic-button-face"><span class="hfx-magnetic-button-label">Let's talk</span></span> </button>
.hfx-magnetic-button {
--c: var(--hfx-color, #3b82f6);
position: relative;
display: inline-grid;
place-items: center;
padding: 1.2em 1.8em; /* the magnetic field around the visible pill */
border: 0;
background: none;
font: 600 var(--hfx-size, 16px) system-ui, sans-serif;
cursor: pointer;
}
.hfx-magnetic-button-cell {
position: absolute;
z-index: 3;
top: 0;
left: 0;
width: calc(100% / 3);
height: calc(100% / 3);
}
.hfx-magnetic-button-cell:nth-child(3n + 2) { left: calc(100% / 3); }
.hfx-magnetic-button-cell:nth-child(3n) { left: calc(200% / 3); }
.hfx-magnetic-button-cell:nth-child(n + 4) { top: calc(100% / 3); }
.hfx-magnetic-button-cell:nth-child(n + 7) { top: calc(200% / 3); }
.hfx-magnetic-button-face {
--x: 0;
--y: 0;
display: block;
padding: 0.85em 1.8em;
border-radius: 2em;
background: var(--c);
color: #fff;
box-shadow: 0 0.5em 1.1em -0.5em color-mix(in srgb, var(--c) 75%, transparent);
transform: translate(calc(var(--x) * 0.7em), calc(var(--y) * 0.5em));
transition: transform 0.45s cubic-bezier(0.25, 0.8, 0.25, 1), box-shadow 0.3s ease;
}
.hfx-magnetic-button-label {
display: block;
transform: translate(calc(var(--x) * 0.3em), calc(var(--y) * 0.2em));
transition: transform 0.45s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.hfx-magnetic-button:is(:hover, :focus-visible) .hfx-magnetic-button-face {
box-shadow: 0 0.9em 1.5em -0.6em color-mix(in srgb, var(--c) 75%, transparent);
}
.hfx-magnetic-button:focus-visible {
outline: none;
}
.hfx-magnetic-button:focus-visible .hfx-magnetic-button-face {
outline: 2px solid var(--c);
outline-offset: 0.25em;
}
.hfx-magnetic-button-cell:nth-child(1):hover ~ .hfx-magnetic-button-face { --x: -1; --y: -1; }
.hfx-magnetic-button-cell:nth-child(2):hover ~ .hfx-magnetic-button-face { --x: 0; --y: -1; }
.hfx-magnetic-button-cell:nth-child(3):hover ~ .hfx-magnetic-button-face { --x: 1; --y: -1; }
.hfx-magnetic-button-cell:nth-child(4):hover ~ .hfx-magnetic-button-face { --x: -1; --y: 0; }
.hfx-magnetic-button-cell:nth-child(5):hover ~ .hfx-magnetic-button-face { --x: 0; --y: 0; }
.hfx-magnetic-button-cell:nth-child(6):hover ~ .hfx-magnetic-button-face { --x: 1; --y: 0; }
.hfx-magnetic-button-cell:nth-child(7):hover ~ .hfx-magnetic-button-face { --x: -1; --y: 1; }
.hfx-magnetic-button-cell:nth-child(8):hover ~ .hfx-magnetic-button-face { --x: 0; --y: 1; }
.hfx-magnetic-button-cell:nth-child(9):hover ~ .hfx-magnetic-button-face { --x: 1; --y: 1; }
@media (prefers-reduced-motion: reduce) {
.hfx-magnetic-button, .hfx-magnetic-button *, .hfx-magnetic-button::before, .hfx-magnetic-button::after, .hfx-magnetic-button *::before, .hfx-magnetic-button *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.