Recreating Material Design's ripple with CSS and vanilla JavaScript

Material Design's ripple animation has one detail that makes it more than a decorative flourish: the ripple originates wherever the user clicks, not from a fixed point. The effect can be rebuilt without extra HTML markup, leaving the button element itself as the only required structure.

<button>Find out more</button>

The two CSS properties that matter

JavaScript will set a few values dynamically, but nearly everything else belongs in CSS. Only two properties are strictly required on the button:

button {
  position: relative;
  overflow: hidden;
}

position: relative lets the ripple be absolutely positioned so its coordinates can be controlled, while overflow: hidden keeps it clipped to the button's bounds. Styling beyond that is optional; a more modern-looking button can serve as the starting point.

/* Roboto is Material's default font */
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

button {
  position: relative;
  overflow: hidden;
  transition: background 400ms;
  color: #fff;
  background-color: #6200ee;
  padding: 1rem 2rem;
  font-family: 'Roboto', sans-serif;
  font-size: 1.5rem;
  outline: 0;
  border: 0;
  border-radius: 0.25rem;
  box-shadow: 0 0 0.5rem rgba(0, 0, 0, 0.3);
  cursor: pointer;
}

Preparing the ripple style

The JavaScript will insert span elements carrying a .ripple class, so their style can be defined up front:

span.ripple {
  position: absolute; /* The absolute position we mentioned earlier */
  border-radius: 50%;
  transform: scale(0);
  animation: ripple 600ms linear;
  background-color: rgba(255, 255, 255, 0.7);
}

A border-radius of 50% makes the ripples circular, and a default scale of 0 makes them grow from nothing. Nothing is visible yet, since top, left, width and height have no values — those arrive from JavaScript. The final CSS piece is the animation's end state:

@keyframes ripple {
  to {
    transform: scale(4);
    opacity: 0;
  }
}

There is no from block. Omitting it is valid: CSS fills in the missing values from the animated element itself, whether they are explicitly declared, as with transform: scale(0), or default, as with opacity: 1.

Computing size and position

The script's job is to size each ripple relative to the button and place it relative to both the button and the cursor. It begins as a function taking a click event:

function createRipple(event) {
  //
}

The button is reached through the event's currentTarget:

const button = event.currentTarget;

Then a span is created, and the button's width and height determine the ripple's diameter and radius:

const circle = document.createElement("span");
const diameter = Math.max(button.clientWidth, button.clientHeight);
const radius = diameter / 2;

The geometric properties follow from those values:

circle.style.width = circle.style.height = `${diameter}px`;
circle.style.left = `${event.clientX - (button.offsetLeft + radius)}px`;
circle.style.top = `${event.clientY - (button.offsetTop + radius)}px`;
circle.classList.add("ripple"); 

Before inserting the span, any ripples left behind by earlier clicks should be removed — checking first is good practice:

const ripple = button.getElementsByClassName("ripple")[0];

if (ripple) {
  ripple.remove();
}

Appending the span to the button element places the ripple inside the button:

button.appendChild(circle);

The function is now complete and only needs to be wired up, which can be done in several ways. To cover every button on the page:

const buttons = document.getElementsByTagName("button");
for (const button of buttons) {
  button.addEventListener("click", createRipple);
}

Extending the function: a magnet effect

The real advantage of building the effect yourself is that it can be combined with other movement. One test of that flexibility is a "magnet" effect, in which the button shifts toward the cursor when the cursor enters a certain area.

A magnet shares variables with the ripple function, so they should live somewhere both can reach while remaining scoped to each individual button — a class achieves this. Because the magnet must track the cursor on every move, the ripple no longer computes the cursor position itself and can instead read cursorX and cursorY. Two new variables, magneticPullX and magneticPullY, set how strongly the button is drawn toward the cursor, so the ripple's center must offset for both the button's new position (x and y) and the magnetic pull:

const offsetLeft = this.left + this.x * this.magneticPullX;
const offsetTop = this.top + this.y * this.magneticPullY;

Applying both effects to every button means instantiating the class once per button:

const buttons = document.getElementsByTagName("button");
for (const button of buttons) {
  new Button(button);
}

Alternative implementations

The vanilla approach above is one option among several. Complete CSS offers a close approximation using the :active pseudo-class to react to clicks, and it works when JavaScript is unavailable — but the ripple can only emerge from a single fixed spot, typically the button's center, rather than following the click. Pre-ES6 JavaScript versions of the same logic exist for older environments, and a jQuery variant can trim a few lines if the library is already a dependency.

React is also viable without leaning on state or refs. Ripple position and size are recalculated on every click, so there is no benefit to storing them in state, and the button element is available from the click event, so refs aren't needed either. A createRipple function identical to the first implementation above can sit as a method of a Button component, scoped to it, with the listener declared in JSX via onClick.