AI CSS Animations

Glitch Image Hover

While hovered, color-shifted slices of the photo jump sideways at random-looking intervals, a digital glitch. Two copies with clip-path insets are stepped through keyframes with steps(1).

Also known as: rgb split image, glitch effect css image, distorted image hover.

HTML

<div class="hfx-image-glitch" role="img" aria-label="Mountains at dusk"></div>

CSS

.hfx-image-glitch {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  position: relative;
  width: 15em;
  height: 9.5em;
  border-radius: 0.75em;
  overflow: hidden;
}
.hfx-image-glitch,
.hfx-image-glitch::before,
.hfx-image-glitch::after {
  background:
    radial-gradient(70% 40% at 16% 110%, color-mix(in srgb, var(--c) 18%, #0b0a1f) 99%, transparent),
    radial-gradient(66% 38% at 88% 108%, color-mix(in srgb, var(--c) 24%, #0b0a1f) 99%, transparent),
    conic-gradient(from 128deg at 68% 44%, transparent 0, color-mix(in srgb, var(--c) 32%, #1e1b4b) 0.4deg 103.6deg, transparent 104deg),
    conic-gradient(from 119deg at 90% 52%, transparent 0, color-mix(in srgb, var(--c) 32%, #1e1b4b) 0.4deg 121.6deg, transparent 122deg),
    conic-gradient(from 123deg at 24% 40%, transparent 0, color-mix(in srgb, var(--c) 42%, #6b2159) 0.4deg 113.6deg, transparent 114deg),
    conic-gradient(from 125deg at 46% 54%, transparent 0, color-mix(in srgb, var(--c) 42%, #6b2159) 0.4deg 109.6deg, transparent 110deg),
    radial-gradient(circle at 38% 58%, #fef3c7 0 1em, rgb(254 215 170 / 0.55) 1.1em, transparent 4.2em),
    linear-gradient(color-mix(in srgb, var(--c) 58%, #1e1b4b), color-mix(in srgb, var(--c) 34%, #f472b6) 52%, color-mix(in srgb, var(--c) 12%, #fdba74) 76%);
}
.hfx-image-glitch::before,
.hfx-image-glitch::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0;
}
.hfx-image-glitch::before {
  filter: hue-rotate(-45deg) saturate(1.8) brightness(1.15);
}
.hfx-image-glitch::after {
  filter: hue-rotate(70deg) saturate(1.5);
}
.hfx-image-glitch:hover {
  animation: hfx-image-glitch-shake 0.8s steps(1, end) infinite;
}
.hfx-image-glitch:hover::before {
  opacity: 1;
  animation: hfx-image-glitch-a 0.8s steps(1, end) infinite;
}
.hfx-image-glitch:hover::after {
  opacity: 1;
  animation: hfx-image-glitch-b 0.8s steps(1, end) infinite;
}
@keyframes hfx-image-glitch-a {
  0%, 100% { clip-path: inset(30% 0 60% 0); transform: translateX(-0.6em); }
  20%      { clip-path: inset(58% 0 30% 0); transform: translateX(0.45em); }
  40%      { clip-path: inset(40% 0 52% 0); transform: translateX(-0.35em); }
  60%      { clip-path: inset(74% 0 12% 0); transform: translateX(0.6em); }
  80%      { clip-path: inset(18% 0 74% 0); transform: translateX(-0.4em); }
}
@keyframes hfx-image-glitch-b {
  0%, 100% { clip-path: inset(48% 0 42% 0); transform: translateX(0.5em); }
  25%      { clip-path: inset(24% 0 70% 0); transform: translateX(-0.6em); }
  50%      { clip-path: inset(64% 0 26% 0); transform: translateX(0.35em); }
  75%      { clip-path: inset(36% 0 58% 0); transform: translateX(-0.45em); }
}
@keyframes hfx-image-glitch-shake {
  0%, 100% { transform: none; }
  30%      { transform: translate(0.06em, -0.04em); }
  55%      { transform: translate(-0.06em, 0.04em); }
  80%      { transform: translate(0.04em, 0.06em); }
}

@media (prefers-reduced-motion: reduce) {
  .hfx-image-glitch, .hfx-image-glitch *, .hfx-image-glitch::before, .hfx-image-glitch::after, .hfx-image-glitch *::before, .hfx-image-glitch *::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