A hairline word swells to its heaviest weight on hover, smoothly with a variable font. A hidden bold copy in the same grid cell reserves the final width, so going bold never shifts the layout.
Also known as: bold on hover without layout shift, font weight transition, variable font hover.
<div class="hfx-text-weight-shift" data-text="Heavy"><span>Heavy</span></div>
.hfx-text-weight-shift {
--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;
display: inline-grid;
font-weight: 200;
transition: font-weight 0.45s cubic-bezier(0.22, 1, 0.36, 1), color 0.45s ease;}
.hfx-text-weight-shift > span,
.hfx-text-weight-shift::after {
grid-area: 1 / 1;
justify-self: center;
}
/* invisible bold copy: holds the final width so neighbors never shift */
.hfx-text-weight-shift::after {
content: attr(data-text);
font-weight: 900;
visibility: hidden;
}
.hfx-text-weight-shift:hover {
color: var(--c);
font-weight: 900;
}
@media (prefers-reduced-motion: reduce) {
.hfx-text-weight-shift, .hfx-text-weight-shift *, .hfx-text-weight-shift::before, .hfx-text-weight-shift::after, .hfx-text-weight-shift *::before, .hfx-text-weight-shift *::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.