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.
<div class="hfx-text-selection" data-text="Select me">Select me</div>
.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;
}
}--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.