Back to buttons

Live preview

Preview settings

Changes appear as you type. Use Tab to test keyboard focus.

⌘ / Ctrl + S saves locally

Interactive 3D button with :has

HTML, CSS or Tailwind, and a little JavaScript. One button or a group of up to six.

By @Nishusk

Remix button

Button submission checks

Checks will appear after the preview updates.

Buttons only: no full pages, forms, cards or navigation. Put scripts in the JavaScript tab. Tailwind uses utilities from HTML and JavaScript; use complete class names. Custom CSS supports plain CSS, without @apply, imports or plugins.

Accessibility quick check

Waiting for the rendered preview…

Try Tab, Enter and Space in the preview. Check the focus ring, hover and disabled states, then enable Reduce motion. These checks are guidance, not an accessibility certification.

Explore freely here. Remix to publish your version with credit to the creator.

About Interactive 3D button with :has

Interactive 3D button with :has is an animated CSS button by @Nishusk. Preview the interaction, customize its HTML, styles and JavaScript, or download the files with creator credit.

Effects and categories
Hover effects, Toggle, 3D
Using this example
Copy the HTML or download index.html, style.css and app.js. Test keyboard focus, motion and contrast in your own project.

Usage, Tailwind and licensing questions

Read the source files

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Interactive 3D button with :has</title>
    <link rel="stylesheet" href="styles.css"/>  
</head>
<body>
    <div class="container">
        <button>
            <div class="inner">
                CSS Magic 3D button
                <div class="events">
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                    <div class="ev"></div>
                </div>
            </div>
            <div class="shadow"></div>
            <div class="shimmer"></div>
        </button>
    </div>
    <div class="switch">
        <label for="switch">Dev Mode</label>
        <input type="checkbox" id="switch">
    </div>
</body>
</html>

CSS


body {
    display: grid;
    place-items: center;
    height: 100vh;
    margin: 0;
    padding: 0;
    background: lightgrey;
}

button {
    --x: 0;
    --y: 0;
    --z: 1px;
    --deg: 0deg;
    --d: 0px;
    --sx: 0;
    --sy: 0;
    perspective: 200px;
    position: relative;
    width: 12rem;
    height: 4rem;
    background: none;
    border-radius: .5rem;
    border: none;
    padding: 0;
    margin: 0;
    cursor: pointer;
}

.shimmer {
    transform-style: preserve-3d;
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle at center, rgba(255, 255, 255, .2), rgba(255, 255, 255, .01) 60%, transparent);
    opacity: 0;
    background-repeat: no-repeat;
    z-index: -1;
    transition: transform .2s linear, opacity .05s ease-in;
}

.shadow {
    --d: 4px;
    transform-style: preserve-3d;
    position: absolute;
    top: 20%;
    left: 5%;
    width: 90%;
    height: 90%;
    filter: blur(6px);
    transform: translate3d(calc(var(--d) * var(--y) * -1.2), calc(var(--d) * var(--x) * 1.2), -1px) skew(calc(var(--sx) * 5deg), calc(var(--sy) * 5deg)) scale(1);
    background: rgba(206, 30, 34, .3);
    transform: translate3d(0.01px, 0.01px, -0.01px);
    transition: transform .2s linear;
}

.inner,
.events {
    position: absolute;
    transform-style: preserve-3d;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: grid;
}

.inner {
    text-rendering: optimizeLegibility;
    overflow: hidden;
    place-items: center;
    background: #ce1e22;
    color: white;
    border-radius: inherit;
    transform: rotate3d(var(--x), var(--y), 0, var(--deg)) translate3d(calc(var(--d) * var(--y) * -1), calc(var(--d) * var(--x)), var(--z));
    transition: transform .2s linear;
}

.events {
    grid-template: repeat(3, 1fr) / repeat(3, 1fr);
    transform: translateZ(1px);
}

/* events triggered by :has */

button:hover {
    --z: 20px;
}

button:hover .shadow {
    transform: translate3d(calc(var(--d) * var(--y) * -2), calc(var(--d) * var(--x) * 1.2), -1px) skew(calc(var(--sx) * 5deg), calc(var(--sy) * 5deg)) scale(1.1, 1.25);
}

button:hover .shimmer {
    opacity: 1;
    transform: translate(calc(-10% * var(--y)), calc(10% * var(--x)));
}

button:hover .events {
    transform: translateZ(calc(var(--z) + 5px));
}

button:has(.ev:hover) {
    --deg: 3deg;
}

button:has(.ev:active) {
    --deg: 6deg;
}

/* top left */
button:has(.ev:nth-child(1):hover) {
    --x: -1;
    --y: 1;
}

button:has(.ev:nth-child(1):active) {
    --x: 1;
    --y: -1;
    --sx: 1;
    --sy: 1;
}

/* top middle */
button:has(.ev:nth-child(2):hover) {
    --x: -1;
}

button:has(.ev:nth-child(2):active) {
    --deg: 15deg;
    --x: 1;
}

/* top right */
button:has(.ev:nth-child(3):hover) {
    --x: -1;
    --y: -1;
}

button:has(.ev:nth-child(3):active) {
    --x: 1;
    --y: 1;
    --sx: -1;
    --sy: -1.5;
}

/* middle left */
button:has(.ev:nth-child(4):hover) {
    --y: 1;
}

button:has(.ev:nth-child(4):active) {
    --y: -1;
    .shadow {
        transform: translate3d(15px, 0, -1px) scale(1.25, 1.25);
    }
}

/* middle middle */
button:has(.ev:nth-child(5):hover) {
    --z: 22px;
}

button:has(.ev:nth-child(5):active) {
    --z: 5px;
    .shadow {
        transform: translate3d(0, 0, -1px);
    }
}

/* middle right */
button:has(.ev:nth-child(6):hover) {
    --y: -1;
}

button:has(.ev:nth-child(6):active) {
    --y: 1;
    .shadow {
        transform: translate3d(-15px, 0, -1px) scale(1.25, 1.25);
    }
}

/* bottom left */
button:has(.ev:nth-child(7):hover) {
    --x: 1;
    --y: 1;
}

button:has(.ev:nth-child(7):active) {
    .shadow {
        transform-origin: center left;
    }
    --x: -1;
    --y: -1;
    --sy: -1;
    --sx: 1.5;
}

/* bottom middle */
button:has(.ev:nth-child(8):hover) {
    --x: 1;
}

button:has(.ev:nth-child(8):active) {
    --deg: 15deg;
    --x: -1;
}

/* bottom right */
button:has(.ev:nth-child(9):hover) {
    --x: 1;
    --y: -1;
}

button:has(.ev:nth-child(9):active) {
    .shadow {
        transform-origin: center right;
    }
    --x: -1;
    --y: 1;
    --sy: 1;
    --sx: 1.5;
}

/* Dev Mode */

.switch {
    position: absolute;
    bottom: 20px;
    right: 20px;
    display: flex;
    align-items: center;
}

.switch input {
    border: none;
    padding: 5px;
    width: 20px;
    height: 20px;
    margin-left: 5px;
}

body:has(#switch:checked) {
    background: white;
}

body:has(#switch:checked) .shadow {
    background: rgba(0, 0, 255, .2);
    border: 1px solid rgba(0, 0, 255, .2);
    filter: blur(0);
}

body:has(#switch:checked) .inner {
    border: 1px solid rgba(0, 0, 255, .2);
    background: rgba(0, 0, 255, .6);
}

body:has(#switch:checked) .shimmer {
    background: radial-gradient(circle at center, rgba(0, 0, 255, 0.1), rgba(0, 0, 255, 0.2) 60%, transparent);
}