WEB LAB 04 · CSS transform / JavaScript / Web Animations API

Create a squishy button with CSS. Minimal code that can be copied and pasted and 3 demos

How to create a squishy button that sinks when pressed using CSS transform/box-shadow. You can save the completed code in one HTML file and try it out immediately. We will explain everything from changing the color and roundness to 3 types of demos that can be made bouncy using JavaScript.

First of all, you can create it using only CSS.We have prepared a completed sample of one HTML file. Comparing the three types of pressing feel below,Minimal code with no installation requiredPlease try it from.

First, try touching it.

Squish
Jiggle
Click

Try holding it down and releasing it. You can also press the Space key.

Recipe for how to play

Even with the same settings, the deformation differs depending on the material. There is no sound.

The device's "Reduce parallax effect" setting suppresses movement when displaying images.

See the code for this trick
// 押下中は沈む。離すと素材ごとの形で戻る
button.style.transform = pressedShape;
button.animate([
  { transform: pressedShape },
  { transform: overshootShape },
  { transform: 'none' }
], { duration, easing: 'ease-out' });

Here is an excerpt showing how it works. Click here for the entire process including drawing, input operations, and stop processing.

View the entire implementation on GitHub ↗

First, try running it with a single HTML file.

"I just want to make the buttons a little prettier, but it's hard to start by building the environment..." In such a case, please try this small CSS version. You can try the same thing as the button below without installing it.

What you can do:The button sinks and widens slightly while pressed, then returns when released. Unlike the three demos above, this version does not add a pronounced bounce after release.

  1. Click Save sample HTML.
  2. Open the saved puni-button.html file in Chrome, Safari, Edge or another browser.
  3. Press and hold "Press" and then release. If it sinks and returns, it's a success.

Results to check:A light purple button will appear. The button in this example is just for experimenting with the look and does not send or navigate when clicked.

Copy and use: All HTML/CSS code

Alternatively, copy all the code below into a plain-text file and save it as puni-button.html with UTF-8 encoding. It works the same way.

<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex, follow">
<meta name="description" content="CSSだけで押すと沈むボタンを作る、単体で動くHTMLサンプル。">
<link rel="canonical" href="https://ryu-koda.com/examples/puni-button.html">
<meta property="og:url" content="https://ryu-koda.com/examples/puni-button.html">
<title>ぷにぷにボタンの最小サンプル</title>
<style>
  body {
    margin: 0;
    min-height: 280px;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 24px;
    background: #fffdf6;
    color: #303d48;
    font-family: system-ui, sans-serif;
  }
  .puni-button {
    --button-color: #e8dff5;
    padding: 20px 36px;
    min-height: 48px;
    border: 2px solid #354550;
    border-radius: 24px;
    background: var(--button-color);
    color: #303d48;
    box-shadow: 0 8px 0 #354550;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    touch-action: manipulation;
    transform-origin: center bottom;
    transition: transform 180ms ease-out, box-shadow 180ms ease-out;
  }
  .puni-button:hover { background: #ded0f1; }
  .puni-button:focus-visible {
    outline: 3px solid #2458a6;
    outline-offset: 6px;
  }
  .puni-button:active {
    transform: translateY(6px) scale(1.06, .92);
    box-shadow: 0 2px 0 #354550;
  }
  @media (prefers-reduced-motion: reduce) {
    .puni-button { transition: none; }
    .puni-button:active {
      transform: none;
      background: #d4c2ee;
      box-shadow: 0 8px 0 #354550;
    }
  }
</style>
</head>
<body>
  <button class="puni-button" type="button">押してみて</button>
  <p>押すと沈み、離すと戻ります。</p>
</body>
</html>
If you want to save and open in terminal
curl -fL https://ryu-koda.com/examples/puni-button.html -o puni-button.html

Result: This creates puni-button.html in the current folder. It overwrites an existing file with that name, so run it in a separate folder.

On macOS, use open puni-button.html; in Windows PowerShell, use Start-Process ./puni-button.html. You can also simply double-click the file.

There are only three reasons why it looks plump.

  1. Create thickness with shadows. The box-shadow: 0 8px 0 declaration adds an 8px shadow beneath the button.
  2. Changes shape only when pressed. In :active, move the button down 6px, scale its width to 1.06 times and its height to 0.92 times, and reduce the shadow to 2px.
  3. Once released, it returns to its original shape. Use transition to animate the change over 180ms. The layout width and height stay unchanged, so nearby text is not pushed aside.

The CSS :active state applies while the control is being activated. It is different from :hover, which applies when the pointer is over it. See Explanation of :active on MDN for details.

Try changing the color, roundness, and feel.

Open the saved HTML in a text editor, change just one place, and save. Reload your browser and compare the changes.

  • Mint color: Change --button-color: #e8dff5 to #e3eee6. This changes the default color. To match the hover color, also change the background in :hover.
  • Less rounded corners: Change border-radius: 24px to 12px for a more rectangular button.
  • A shallower press: Change translateY(6px) to translateY(3px). Changing the shadow from 2px to 5px helps keep the apparent thickness consistent.
  • Return slowly: Change both occurrences of 180ms to 300ms. Both the press and the return become slower.

How can I add it to my site?

  1. Place the sample's button element where you want the button to appear.
  2. Add the rules for .puni-button and @media to your site's CSS. You do not need the sample's body rule, which centers the entire page.
  3. Change the wording of the button to suit your purpose. Use links instead of buttons for page navigation, and adjust the submission process to match the form implementation.

Decorations alone do not increase post-click functionality. Try using it for playful demos or trial feature buttons. For payments and important transmissions, give priority to the easy-to-understand results of pressing the button.

When things don't work properly.

code is displayed as text

Check that the extension is not .html.txt. Save as plain text, not a Word document or another rich-text format.

The color changes but it doesn't move

The device's "Reduce parallax effect" settings intentionally omit the transformation. Also, check if another CSS is overwriting the transform or transition on the existing site.

What about your smartphone or keyboard?

It also responds while you press it with your finger. On your keyboard, use Tab to select a button, press and release Space. The appearance of the Enter press state differs depending on the browser. The focus blue frame is left to convey the position of the operation.

I want it to bounce like jelly even after I release it.

Once you understand how the CSS version works, move on to the three types of demos at the top of the page. This uses JavaScript's Web Animations API to play the "pressed shape → slightly stretched shape → original shape" in this order. The next step is to run the entire demo at hand.

Try them in this order and you'll see the difference.

  1. Press and hold the rubber, jelly and keycap buttons in turn, then release them. Compare their appearance while pressed and after release.
  2. Change the sinking and returning times. Compare a button that deforms significantly with a button that responds briefly.
  3. Select the button with Tab and try pressing Space or Enter in the same way.

Application: Run three types of JavaScript demos at hand.

From here on, we will show you the demo procedure for comparing rubber, jelly, and keys at the top of the page. This environment configuration is not required for the CSS version sample.

1. Check the execution environment

Open a terminal and run it line by line. Requires Git and Node.js.

git --version
node --version
npm --version

Results to check: Each command should display a version number. This project requires Node.js 22.12.0 or later. If a command is missing, use Git official and Node.js official to set up your environment.

2. Get the code and launch

Move to the folder you want to save it to, then execute the following line by line. If a folder with the same name already exists, choose a different location.

git clone https://github.com/R-Koda/ryu-koda.com.git
cd ryu-koda.com
npm ci
npm run dev -- --host 127.0.0.1 --port 4321

Results to check:Finally, the local server URL will be displayed and the terminal will remain open. If an error occurs, do not proceed and check the output of the previous command.

3. Open the experiment page

Open http://127.0.0.1:4321/playground/buttons/ in your browser. If the same demo appears and responds as described above, it is running successfully. If Terminal shows a different port, use that port instead. Press Ctrl+C in Terminal to stop the server.

Which file should I read?
  • src/components/Experiment.astro:SVG, HTML, CSS for appearance.
  • src/scripts/experiments.ts contains the experiment interactions. Look for if(kind==='buttons').
  • src/pages/playground/[slug].astro: Common layout for this explanation page.

If the screen is white, check your terminal for errors and your browser's developer tools. If the movement stops, also check the "Reduce parallax effect" setting on your device. This demo will suppress movement according to that setting.

How does it work?

Point 1

Create the shape, color, and shadow with CSS, and use translateY and scale to express sinking and horizontal expansion while pressed. The width and height on the layout will not change.

Point 2

Once released, pass the three keyframes to animate(): the pushed shape → the slightly overdone shape → the original shape. This excess creates a bouncy impression.

Point 3

Release the pressed state on pointercancel, lostpointercapture and blur as well as pointerup. This prevents the button from getting stuck when a gesture is interrupted or focus moves elsewhere.

Try changing one thing and compare.

Open src/scripts/experiments.ts in your editor and find the branch for this experiment.

What to change:In the buttons experiment's up handler, change the jelly return transform from translateY(-5px) scale(.94,1.1) to translateY(-2px) scale(.98,1.04).

Result changes:The amount that the jelly stretches after it is released will be smaller, resulting in a calmer return. The amount of sinking does not change, so compare the moment you release it.

Once saved, return to your browser and reload if necessary. Make a note of the original values ​​so you can quickly go back and compare.

before incorporating it into your site.

Keep the deformation and return time modest for commonly used send buttons. Device settings that reduce movement eliminate deformation animations and convey operations using color changes.

Instead of conveying information through movement alone, it can also be used with keyboard operations and settings that reduce movement. First, incorporate it into one part and make sure that it does not interfere with the readability of articles and menus.

Implementation confirmation:Full interaction source code / HTML/SVG/CSS / MDN:Element.animate()

SMALL INTERACTIONS

Small reactions make the site more fun.

Please click on the one that interests you. Here are four small experiments you can incorporate into your buttons and menus.

01 / Convey the status

You can immediately see that the switch has been made.

Tips to incorporate

Use this for an on/off setting. Update the label and aria-pressed along with the color, rather than relying on color alone.

button[aria-pressed="true"] { background: #fff0bc; }
02 / Create a comfortable pushing feeling

It sinks, bounces, and comes back.

Tips to incorporate

For a playful CTA. If you transform using transform, the position of the surrounding text will not change. Even if you hit repeatedly, the animation will not overlap.

Button demo and how to make →
03 / Create the joy of opening
open a small box

Thank you for finding me.
Even small reactions are part of the site's personality.

Take out the explanation when you need it.

Tips to incorporate

Useful for FAQs and extra details. HTML's details and summary elements open and close without JavaScript and support keyboard interaction.

How to make a drawer menu →
04 / Reply to operations

Still blank. Please give me a push.

Tips to incorporate

For feedback on successful operations, such as copy completion. This model only increases the number of marks when you press it, but does not save or send it. There can be up to 6 marks.

status.textContent = 'コピーしました';

None of these demos plays sound. Animations are skipped when your device requests reduced motion.