Skip to content
animorio
IT
Back to the library

Fluid headline resize

Text, HoverGSAP, CSS onlyMediumCompressed weight: Demo 7.2 KBGSAP 28 KBFreeDownload the zip

The hero preview in a made-up site builder, on warm white: dragging the right edge of the window keeps the headline on its two lines, first by narrowing its letters along the variable font’s width axis, from wide to condensed and a little bolder, and only when that is not enough by getting smaller. Below, the Desktop, Tablet and Phone formats snap the preview on a short spring, and next to them the width in pixels, the wdth axis, the weight and the size are read out live.

The code

A folder that runs on its own: index.html, style.css, script.js and the libraries from a CDN. The React and Vue tabs hold the same demo as a ready component, which switches its animations off when it leaves the page.

<!doctype html>
<!--
  "Fluid headline resize" from Animorio · https://animorio.com
  License: free on your own sites and your clients’ sites, paid work included.
  Not for resale, redistribution, templates or code collections · https://animorio.com/en/license/
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>The fluid headline that narrows as you drag the preview's edge</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main class="stage" data-stage>
    <!-- the preview of a made-up site builder: drag the right edge and the hero headline keeps fitting, first narrowing its letters, then its size -->
    <section class="banco" data-banco aria-label="Lattice site builder, hero preview">
      <header class="barra">
        <span class="marchio">
          <svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3 3.5h10M3 8h10M3 12.5h10M5.5 2v12M10.5 2v12"></path></svg>
          Lattice
        </span>
        <span class="barra__pagina">Home <span class="barra__sep">/</span> Hero</span>
        <span class="barra__px" aria-live="off"><span class="rullo" data-px>1280</span> px</span>
      </header>

      <div class="area" data-area>
        <div class="cornice" data-cornice>
          <div class="hero" data-hero>
            <p class="occhiello">Spring launch</p>
            <h1 class="titolo" data-titolo>
              <span class="riga" data-riga>Every word</span>
              <span class="riga" data-riga>fits the frame.</span>
            </h1>
            <span class="guida" data-guida aria-hidden="true"><i></i><i></i></span>
            <p class="sotto">Type that breathes with every screen.</p>
            <span class="cta" data-cta>Start building
              <svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.5 8h9M8.5 4l4 4-4 4"></path></svg>
            </span>
          </div>
          <div class="maniglia" data-maniglia role="slider" tabindex="0" aria-label="Preview width" aria-valuemin="390" aria-valuemax="1280" aria-valuenow="1280">
            <span class="maniglia__presa"></span>
          </div>
        </div>
      </div>

      <footer class="piede">
        <div class="scelte" data-scelte role="group" aria-label="Breakpoints">
          <span class="scelte__fondo" data-scelte-fondo aria-hidden="true"></span>
          <button class="scelta" type="button" data-scelta data-larga="1280">
            <svg viewBox="0 0 16 16" aria-hidden="true"><rect x="1.5" y="3" width="13" height="8.5" rx="1.5"></rect><path d="M5.5 13.5h5"></path></svg>
            <span>Desktop</span>
          </button>
          <button class="scelta" type="button" data-scelta data-larga="768">
            <svg viewBox="0 0 16 16" aria-hidden="true"><rect x="3" y="1.5" width="10" height="13" rx="1.6"></rect><path d="M7 12.5h2"></path></svg>
            <span>Tablet</span>
          </button>
          <button class="scelta" type="button" data-scelta data-larga="390">
            <svg viewBox="0 0 16 16" aria-hidden="true"><rect x="4.5" y="1.5" width="7" height="13" rx="1.6"></rect><path d="M7.2 12.6h1.6"></path></svg>
            <span>Phone</span>
          </button>
        </div>
        <p class="assi" data-assi aria-hidden="true">
          <span>wdth <b data-wdth>125</b></span>
          <span>wght <b data-wght>560</b></span>
          <span><b data-size>64</b> px</span>
        </p>
      </footer>
    </section>

    <span class="titolo misura" data-misura aria-hidden="true"></span>
    <svg class="ghost" data-ghost viewBox="0 0 20 20" aria-hidden="true"><path d="M4 2.5v13.2l3.5-3.3 2.4 5.3 2.3-1-2.4-5.2 4.8-.2z"></path></svg>
  </main>
  <script src="https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

Playground

Change values, colours and text with the controls and watch the animation respond live. Or write in the code yourself. Then download the folder with your changes.

35 values to change

Or ask your AI assistant for it

With the Animorio connector, Claude Code, ChatGPT, Cursor or VS Code take it from here, guide included, and fit it into your project. Paste this request:

Take “Fluid headline resize” from Animorio and fit it into my site
Platform licenseFree on your sites and your clients’ sites. Want it inside a website builder, a template platform or a client dashboard? There’s a license for agencies and platforms.

What it does

On a warm white background sits the window of Lattice, a made-up site builder: at the top the logo, the page being edited and the preview width in pixels; in the middle, on a grey canvas, the white preview of a website hero, with the eyebrow Spring launch in blue, the big headline on two lines, Every word fits the frame., a thin blue line as wide as the longest line, a sentence and the black Start building button. On the right edge of the preview there is a handle. Dragging it to the left narrows the preview from both sides and the headline never wraps: first the letters narrow, going from width 125 to 75 of the variable font, and meanwhile they get slightly bolder so that the line keeps its weight; the blue line underneath lights up while that happens. Once the letters are as narrow as they go, the headline starts to get smaller. Past the phone format the handle resists, like a rubber band, and when released it springs back. At the bottom three pills, Desktop, Tablet and Phone, with a white background sliding under the right one: a click snaps the preview to its width on a short spring. Next to them, live, the wdth axis, the weight and the size in pixels. If nobody touches the page, a drawn pointer grabs the handle, narrows the preview down to the phone, then picks Tablet and finally Desktop.

How it works

The lines measured once

When the fonts are ready the script measures the two lines at 100 pixels for every step of the wdth axis, from 75 to 125, with the weight that goes with that step, and keeps the widest line in a table: afterwards, on every frame, it only reads the table and measures nothing.

First the width of the letters, then the size

The line’s room is the preview width minus the margins. If the wide headline fits at the largest size, MAX_SIZE, it stays like that; if not, a bisection finds the letter width that makes it fit to the pixel; if even the narrowest letters are not enough, the headline gets as much smaller as needed.

A little bolder as it narrows

Narrow letters look lighter: the weight grows by INK_GAIN for every point of width lost, so the line keeps the same blackness. Weight and width live in two CSS variables inside font-variation-settings.

The handle doubles the step

The preview is centred, so moving the right edge by one pixel changes the width by two: the script doubles the movement and keeps it in virtual pixels, from 390 to 1280, which do not change when the page is resized. Past the ends the movement counts half, and the drawing shows thirty per cent of it: the rubber band.

The formats on a spring

Desktop, Tablet and Phone carry their width in data-larga; GSAP takes the state there with back.out(SNAP_BOUNCE) and redraws headline, blue line and numbers at every step. The white background of the pills slides under the one for the format the preview is in.

Values to change

NameFileDefaultWhat it changes
WDTH_MAXscript.js125The letter width when the preview is wide (75 to 125).
WDTH_MINscript.js75The narrowest width before the headline starts getting smaller.
WEIGHTscript.js560The headline’s weight with the widest letters.
INK_GAINscript.js1.6How much weight the letters gain for every point of width lost.
MAX_SIZEscript.js72The headline’s largest size, in pixels.
MIN_FRAMEscript.js0.34The narrowest preview width, as a share of the canvas.
SNAP_BOUNCEscript.js1.3How much the preview bounces when a format is chosen (0 means no bounce).
AUTO_PILOTscript.js1.2After how many seconds the drawn pointer starts (0 means never).

Try them in the Playground

When to use it

For the hero of a landing page that wants to show its headline holds up on every screen, for the page of a site builder, an editor or a type foundry explaining fluid typography, or for a tutorial on variable fonts.

On phones

The same animation with adapted sizes: the window is measured on the screen’s width and height, the format pills show only their icon and the handle is dragged with a finger.

With reduced motion

No drawn pointer, no spring and no entrance: the window fades in, the handle and the formats change the preview at once, and the headline keeps fitting its lines.

Pitfalls

  • The table is built only once the fonts have arrived (document.fonts.ready): measured with the fallback font, the letter widths come out wrong and the headline wraps.
  • The weight changes the letter widths: the table has to be measured with the weight each axis step will really have, not with a fixed one.
  • The handle movement is kept in virtual pixels, not real ones: that way, resizing the page leaves the preview at the same format.
  • Each headline line has white-space: nowrap: without it, the moment a pixel is missing the browser wraps before the calculation catches up.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0.

Compressed with gzip, the three demo files weigh 7.2 KB; the libraries: GSAP 28 KB.

Testing

Tested on 29 October 2026

  • Desktop: Done
  • Phone: Done
  • Reduced motion: Done
  • Safari on iPhone: To do

License

Use it in your own projects and in your clients’ projects. Do not resell it or redistribute it as is. GSAP stays under its own Standard license. Full license.

All animations