AI CSS Animations

Text Selection Hover Effect

Hovering drags a selection across the word: a solid block with inverted text grows behind a moving cursor bar. The block is a copy of the text in a pseudo-element, unclipped left to right.

Also known as: highlight block on hover, inverted text wipe, selected text animation.

Select me

HTML

<div class="hfx-text-selection" data-text="Select me">Select me</div>

CSS

.hfx-text-selection {
  --c: var(--hfx-color, #3b82f6);
  font-size: calc(var(--hfx-size, 16px) * 2.5);
  font-family: system-ui, sans-serif;
  font-weight: 800;
  line-height: 1.15;
  cursor: default;
  position: relative;
  display: inline-block;
  padding: 0 0.08em;}
.hfx-text-selection::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  padding: 0 0.08em;
  background: var(--c);
  color: #fff;
  clip-path: inset(0 100% 0 0);
  transition: clip-path 0.5s cubic-bezier(0.65, 0, 0.35, 1);
}
.hfx-text-selection::before {
  content: "";
  position: absolute;
  z-index: 1;
  inset: -0.1em 0;
  background: linear-gradient(var(--c), var(--c)) 100% 0 / 0.05em 100% no-repeat;
  opacity: 0;
  transform: translateX(-100%);
  transition: transform 0.5s cubic-bezier(0.65, 0, 0.35, 1), opacity 0.2s ease;
}
.hfx-text-selection:hover::after { clip-path: inset(0); }
.hfx-text-selection:hover::before {
  opacity: 1;
  transform: translateX(0.05em);
}

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