AI CSS Animations

Keyboard Shortcut Key Hover

A docs search button whose shortcut keys press down one after the other on hover, then stay lit, as if you had just typed the combo. The keycaps get their depth from a bottom shadow.

Also known as: kbd key press css, keycap hover effect, search button with shortcut.

HTML

<button class="hfx-shortcut-keycaps">
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="6.5"/><path d="m20 20-4.2-4.2"/></svg>
  <span>Search docs</span>
  <kbd>&#8984;</kbd><kbd>K</kbd>
</button>

CSS

.hfx-shortcut-keycaps {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  display: inline-flex;
  align-items: center;
  gap: 0.3em;
  width: 15em;
  padding: 0.55em 0.55em 0.55em 0.9em;
  border: 1px solid color-mix(in srgb, var(--c) 30%, transparent);
  border-radius: 0.7em;
  background: color-mix(in srgb, var(--c) 6%, transparent);
  color: color-mix(in srgb, currentColor 65%, transparent);
  font-family: system-ui, sans-serif;
  cursor: pointer;
  transition: border-color 0.25s ease, color 0.25s ease, background-color 0.25s ease;
}
.hfx-shortcut-keycaps svg {
  width: 1.1em;
  height: 1.1em;
  margin-right: 0.3em;
}
.hfx-shortcut-keycaps span {
  flex: 1;
  text-align: left;
}
.hfx-shortcut-keycaps kbd {
  display: grid;
  place-items: center;
  min-width: 1.75em;
  height: 1.75em;
  padding: 0 0.3em;
  border: 1px solid color-mix(in srgb, var(--c) 35%, transparent);
  border-radius: 0.4em;
  background: color-mix(in srgb, var(--c) 10%, transparent);
  box-shadow: 0 0.16em 0 color-mix(in srgb, var(--c) 35%, transparent);
  color: var(--c);
  font-family: inherit;
  font-size: 0.85em;
  font-weight: 600;
}
.hfx-shortcut-keycaps:is(:hover, :focus-visible) {
  border-color: var(--c);
  background: color-mix(in srgb, var(--c) 10%, transparent);
  color: inherit;
}
.hfx-shortcut-keycaps:is(:hover, :focus-visible) kbd {
  animation: hfx-shortcut-keycaps-press 0.5s cubic-bezier(0.3, 0.7, 0.4, 1) both;
}
.hfx-shortcut-keycaps:is(:hover, :focus-visible) kbd + kbd { animation-delay: 0.12s; }
@keyframes hfx-shortcut-keycaps-press {
  0% { transform: translateY(0); box-shadow: 0 0.16em 0 color-mix(in srgb, var(--c) 35%, transparent); }
  35%, 55% { transform: translateY(0.14em); box-shadow: 0 0.02em 0 color-mix(in srgb, var(--c) 35%, transparent); }
  100% {
    transform: translateY(0);
    border-color: var(--c);
    background: var(--c);
    box-shadow: 0 0.16em 0 color-mix(in srgb, var(--c) 60%, #000);
    color: #fff;
  }
}

@media (prefers-reduced-motion: reduce) {
  .hfx-shortcut-keycaps, .hfx-shortcut-keycaps *, .hfx-shortcut-keycaps::before, .hfx-shortcut-keycaps::after, .hfx-shortcut-keycaps *::before, .hfx-shortcut-keycaps *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --hfx-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --hfx-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar hover effects