The button tips toward wherever you press it, like a card pushed at one corner. Nine invisible zones sit over the face and each :active zone sets the tilt through a sibling selector. No JavaScript.
Also known as: 3d tilt on click css, press tilt effect, ios style press button.
<button class="btn-press-tilt" type="button"> <i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i> <span class="btn-press-tilt-face">Press any corner</span> </button>
.btn-press-tilt {
--c: var(--btn-color, #3b82f6);
position: relative;
display: inline-grid;
padding: 0;
border: 0;
background: none;
font: 600 var(--btn-size, 18px)/1.1 system-ui, sans-serif;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.btn-press-tilt i {
position: absolute;
z-index: 1;
width: 33.34%;
height: 33.34%;
}
.btn-press-tilt i:nth-child(3n + 1) { left: 0; }
.btn-press-tilt i:nth-child(3n + 2) { left: 33.33%; }
.btn-press-tilt i:nth-child(3n) { left: 66.66%; }
.btn-press-tilt i:nth-child(-n + 3) { top: 0; }
.btn-press-tilt i:nth-child(n + 4):nth-child(-n + 6) { top: 33.33%; }
.btn-press-tilt i:nth-child(n + 7) { top: 66.66%; }
.btn-press-tilt-face {
--x: 0;
--y: 0;
--z: 1;
display: block;
padding: 1.1em 2.2em;
border-radius: 0.9em;
background: linear-gradient(135deg, color-mix(in srgb, var(--c) 78%, #fff), var(--c) 55%, color-mix(in srgb, var(--c) 80%, #000));
color: #fff;
box-shadow: calc(var(--x) * -0.25em) calc(0.5em - var(--y) * 0.25em) 1.1em -0.4em color-mix(in srgb, var(--c) 70%, #000);
transform: perspective(16em) rotateX(calc(var(--y) * -14deg)) rotateY(calc(var(--x) * 14deg)) scale(var(--z));
transition: transform 0.5s cubic-bezier(0.3, 1.6, 0.5, 1), box-shadow 0.5s ease;
}
.btn-press-tilt:active .btn-press-tilt-face {
--z: 0.97;
transition-duration: 0.12s;
transition-timing-function: ease-out;
}
.btn-press-tilt i:nth-child(1):active ~ .btn-press-tilt-face { --x: -1; --y: -1; }
.btn-press-tilt i:nth-child(2):active ~ .btn-press-tilt-face { --x: 0; --y: -1; }
.btn-press-tilt i:nth-child(3):active ~ .btn-press-tilt-face { --x: 1; --y: -1; }
.btn-press-tilt i:nth-child(4):active ~ .btn-press-tilt-face { --x: -1; --y: 0; }
.btn-press-tilt i:nth-child(5):active ~ .btn-press-tilt-face { --x: 0; --y: 0; --z: 0.94; }
.btn-press-tilt i:nth-child(6):active ~ .btn-press-tilt-face { --x: 1; --y: 0; }
.btn-press-tilt i:nth-child(7):active ~ .btn-press-tilt-face { --x: -1; --y: 1; }
.btn-press-tilt i:nth-child(8):active ~ .btn-press-tilt-face { --x: 0; --y: 1; }
.btn-press-tilt i:nth-child(9):active ~ .btn-press-tilt-face { --x: 1; --y: 1; }
.btn-press-tilt:focus-visible { outline: none; }
.btn-press-tilt:focus-visible .btn-press-tilt-face {
outline: 2px solid var(--c);
outline-offset: 0.2em;
}
@media (prefers-reduced-motion: reduce) {
.btn-press-tilt, .btn-press-tilt *, .btn-press-tilt::before, .btn-press-tilt::after, .btn-press-tilt *::before, .btn-press-tilt *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--btn-color on the element or a parent.
Currently previewing #3b82f6.--btn-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 18px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.