日本語

How to Make a Press Ripple Button with CSS

​​CSS​HTML,JavaScript,Playwright

To make a button whose ripple spreads when pressed, draw a circle with ::after. Normally the circle is large and transparent. While the button is pressed (:active), set it to scale 0 and opaque. When the mouse button is released, :active no longer applies, and transition brings the circle back to its large, transparent state. This return motion looks like the ripple spreading. The animation runs in CSS alone. Adding a few lines of JavaScript lets the ripple spread from the pressed position.

Note

Test environment: Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the HTML and CSS page in Playwright and pressed the button with the mouse and keyboard. For the motion after release, I paused the animation at specific times and read the getComputedStyle() values and the screenshot pixels. I did not test Firefox, Safari, or touch operation on a real smartphone.

Three presses by a mouse pointer on a teal round button labeled "Send" (the label is in Japanese). Each press changes the text above the button to show the press count, for example "Click 1 of 3" (the label in the image is in Japanese), and the background turns pink. When the mouse is released, a whitish circle spreads from the pressed point and fades out, clipped to the button's shape. The first press is on the left, the second is at the lower right, and the third is in the center.
Three presses at different positions. After each release, the ripple spreads from the pressed point.

Full code

The CSS is as follows.

css
.ripple-button {
  position: relative;
  overflow: hidden; /* Clip the ripple to the button's shape */
  padding: 14px 32px;
  border: none;
  border-radius: 999px;
  background: #067a98;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
}

/* The ripple circle. Normally it is large and transparent */
.ripple-button::after {
  content: "";
  position: absolute;
  left: var(--x, 50%); /* Pressed position. Center if JavaScript is absent */
  top: var(--y, 50%);
  width: 250%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgb(255 255 255 / 0.5);
  transform: translate(-50%, -50%) scale(1);
  opacity: 0;
  transition: transform 0.6s ease-out, opacity 0.6s ease-out;
  pointer-events: none;
}

/* While pressed, scale it to 0 and make it opaque. No transition on this switch */
.ripple-button:active::after {
  transform: translate(-50%, -50%) scale(0);
  opacity: 1;
  transition: none;
}

/* No ripple for users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
  .ripple-button::after {
    display: none;
  }
}

The HTML only adds the class to the button. If you want the ripple to spread from the pressed position, also load the JavaScript below.

html
<button class="ripple-button">送信する</button>
<script src="ripple.js"></script>
js
document.querySelectorAll(".ripple-button").forEach((button) => {
  // Store the mouse or touch position in --x and --y
  button.addEventListener("pointerdown", (event) => {
    const rect = button.getBoundingClientRect();
    button.style.setProperty("--x", `${event.clientX - rect.left}px`);
    button.style.setProperty("--y", `${event.clientY - rect.top}px`);
  });
  // When pressed with the Space key, clear the position so it spreads from the center
  button.addEventListener("keydown", (event) => {
    if (event.key === " ") {
      button.style.removeProperty("--x");
      button.style.removeProperty("--y");
    }
  });
});

The circle is 250% of the button's width, and aspect-ratio: 1 keeps it perfectly round. On the button I tested (about 137px wide and 52px high), the diameter is about 342px and the radius is about 171px. The radius is longer than the button's diagonal (about 146px), so no matter where on the button you press, the circle reaches the opposite corner. pointer-events: none stops the circle from receiving clicks (explanation of pointer-events).

Spreading from scale 0 after release

:active is a pseudo-class that applies while the button is being pressed. With a mouse, it starts when the primary button is pressed down.

While the button is pressed, transition: none applies, so the circle immediately becomes scale 0 and opaque. When the button is released, :active no longer applies, and the circle returns to its normal large, transparent state over the transition (0.6 seconds). I released the button 100 milliseconds after pressing it and read the ::after values from the moment of release:

Time since release Size (scale) Opacity Pixels whose color differs from before pressing
0 s 0 1 0
0.15 s 0.378 0.622 4,525
0.3 s 0.685 0.315 6,465
0.45 s 0.907 0.093 6,302
0.6 s 1 0 0

The circle grew and faded, and at 0.6 seconds the button looked the same as before it was pressed. While the button is pressed, the circle's size is 0, so nothing is visible. At both 100 milliseconds and 500 milliseconds after pressing, zero pixels differed in color from before pressing. The ripple starts spreading from the moment of release.

Using @keyframes with :active makes the ripple disappear on a short click

You can also play a @keyframes animation when the button is pressed.

css
.ripple-button:active::after {
  animation: ripple 0.6s ease-out;
}

@keyframes ripple {
  from { transform: translate(-50%, -50%) scale(0); opacity: 1; }
  to { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

With this approach, the ripple spreads while the button is held down. 100 milliseconds after pressing, the size had grown to about 0.22, and 3,416 pixels differed in color. However, when the button is released there, :active no longer applies, and the animation itself is removed. Right after release, document.getAnimations() was empty, and zero pixels differed in color. With a short press, like an ordinary click, the ripple disappears before it finishes spreading.

If you use transition to return the circle to its normal state, the 0.6-second transition plays after release in the same way, whether the button was held for 100 milliseconds or 500 milliseconds.

To spread the ripple from the moment of press, re-add a class

If you want the ripple to be visible while the button is held, and to finish spreading even on a short click, use JavaScript to add a class and attach the animation to that class. Because :active is not used, the animation does not stop when the button is released.

css
.ripple-button.is-rippling::after {
  animation: ripple 0.6s ease-out;
}

@keyframes ripple {
  from {
    transform: translate(-50%, -50%) scale(0);
    opacity: 1;
  }
}

In JavaScript, write the following in place of the ripple.js shown above.

js
document.querySelectorAll(".ripple-button").forEach((button) => {
  button.addEventListener("pointerdown", (event) => {
    const rect = button.getBoundingClientRect();
    button.style.setProperty("--x", `${event.clientX - rect.left}px`);
    button.style.setProperty("--y", `${event.clientY - rect.top}px`);
    // Remove and re-add the class so the animation starts from the beginning
    button.classList.remove("is-rippling");
    void button.offsetWidth;
    button.classList.add("is-rippling");
  });
  button.addEventListener("animationend", () => {
    button.classList.remove("is-rippling");
  });
});

The normal ::after and .ripple-button rules are the first CSS with the transition and :active rules removed. As explained in the @keyframes documentation, if you omit to, the browser uses the element's original style as the end state. Here, the end state is the normal ::after values (scale 1, opacity 0).

100 milliseconds after pressing, the size had grown to about 0.26. 50 milliseconds after release, the animation was still running and had reached about 0.42. 750 milliseconds after release, the animation had finished and the class had been removed.

The line void button.offsetWidth; is there for repeated presses. I pressed the button a second time 200 milliseconds after the first press, and checked 30 milliseconds later. With this line, the animation restarted from the beginning, and the size was about 0.05. Without this line, the first animation continued, and the size was about 0.56. Removing the class and adding it back immediately, without this line, did not restart the animation.

Because this approach adds the class on pointerdown, pressing the Space key did not produce a ripple. 30 milliseconds after release, there were no running animations.

Without overflow: hidden, the ripple spills outside the button

The ::after circle is made much larger than the button. If the button does not have overflow: hidden, the circle is drawn outside the button.

Two panels stacked vertically. Both show a teal round button labeled "Send" (the label is in Japanese) on a dark navy background. In the top panel, "overflow: hidden" is not set. A large whitish circle centered on the left side of the button extends beyond the button and onto the background. In the bottom panel, "overflow: hidden" is set. The circle is clipped to the button's shape, and only the left side of the button is lit.
0.15 seconds after release. Without overflow: hidden, the circle extends outside the button (top).

On a dark background (#1f2a30) at 0.15 seconds after release, 0 pixels outside the button changed color with overflow: hidden, while 8,618 pixels changed without it.

The spillover is not only a visual problem. On a 240px-wide screen with the button not pressed, scrollWidth of the page was 240px with overflow: hidden, the same as the screen. Without it, the page was 270px wide. The invisible, transparent circle made the page 30px wider than the screen, so the page could be scrolled 30px to the right.

According to the overflow documentation, with hidden, overflowing content is clipped at the element's padding box. As shown in the comparison image above, the circle was clipped along the rounded edge set by border-radius: 999px.

Passing the pressed position with custom properties

The circle's center is set by left: var(--x, 50%) and top: var(--y, 50%). As described in the var() documentation, the second value is a fallback used when the custom property is not defined.

When the JavaScript is loaded, the position pressed is stored as the distance from the button's top-left corner, using pointerdown. Pressing at (30, 20) from the top-left corner set left and top on ::after to 30px and 20px. Pressing at (120, 30) set them to 120px and 30px. On a page without the JavaScript, pressing at (30, 20) gave 68.4219px and 26px, so the ripple spread from the center of the button.

The keydown handler is for keyboard presses. With only the pointerdown handler, if I first pressed near the left edge of the button with the mouse (10px from the left) and then pressed the Space key, left stayed at 10px, so the ripple spread from the left edge even though the press came from the keyboard. Clearing the position on keydown changed left to 68.4219px, and the ripple spread from the center.

With the keyboard, :active applied while the Space key was held down, and the ripple spread after release. With the Enter key, :active did not apply. The click fired, but no ripple appeared.

No ripple when reduced motion is set

For users who have set their OS to reduce motion, the circle itself is removed with prefers-reduced-motion. When I opened the page in Playwright with reducedMotion: 'reduce', display on ::after was none, and no animations were created when the button was pressed and released. When there is no preference (no-preference), two transitions, on transform and opacity, were created.

Browser support

I checked the main CSS and DOM features used in the code in this article against MDN compatibility data (@mdn/browser-compat-data 8.1.8). The feature that required the newest versions was aspect-ratio, supported from Chrome 88, Firefox 89, and Safari 15. Space-separated values such as rgb(255 255 255 / 0.5) are supported from Chrome 65, Firefox 52, and Safari 12.1. prefers-reduced-motion is supported from Chrome 74, Firefox 63, and Safari 10.1. pointerdown is supported from Chrome 55, Firefox 59, and Safari 13.

​Posted by 86