AI CSS Animations

Font Weight Text Hover

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.

Heavy

HTML

<div class="hfx-text-weight-shift" data-text="Heavy"><span>Heavy</span></div>

CSS

.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;
  }
}

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