Text Lock-Up

Avatar of Geoff Graham
Geoff Graham on (Updated on )
<svg viewBox="0 0 100 100">
    <tspan class="line-1" textLength="100" x="0" y="1em" font-size="20">
      The Cat
    <tspan class="line-2" textLength="100" x="0" dy="0.9em" font-size="20" lengthAdjust="spacingAndGlyphs">
      in the
    <tspan class="line-3" textLength="100" x="0" dy="0.9em" font-size="35">

SVG offers the <tspan> tag. While it functions a lot like a normal <span> in HTML, it accepts attributes that unlock powerful text-shaping capabilities.

One of those attributes is textLength. If we set this to 100, then the text wrapped in <tspan> will be forced to the full length of the SVG container.

See the Pen SVG Text Lockup – Step 1 by Geoff Graham (@geoffgraham) on CodePen.

Another one of those attributes is lengthAdjust. This only applies when <tspan> (or <text>) has a set textLength and handles the way the text is expanded or compressed to fit that space. The default value is spacing which preserves the letter shapes but adjusts the gaps between characters. We can use spacingAndGlyphs instead and that will adjust expand of compress the shape of the characters as well for when the natural spacing is awkward.

See the Pen SVG Text Lockup – Step 2 by Geoff Graham (@geoffgraham) on CodePen.

Like <text>, the <tspan> tag also accepts a font-size attribute which does exactly as you might expect: change the size of the text. We can use that to make adjustments to the text where increasing textLength leaves too much or too little space and lengthAdjust warps the characters out of whack.

Combined together, these three attributes give us the ability to create text lock-ups. Here’s what we get as a result of the snippet above with some additional CSS for extra styling:

See the Pen SVG Text Lockup by Geoff Graham (@geoffgraham) on CodePen.

Other Lockups

We’ve written about type lockups before:

A type lockup is a typographic design where the words and characters are styled and arranged very specifically. Like the design is literally locked in place.

SVG is perfect for this kind of thing, because of the way it resizes. Text inside SVG doesn’t reflow like type in HTML does, it scales in the unique way SVG does, which is often at the perfect aspect ratio it was designed at (although you can control that).

So if you design something like this in vector editing software like Adobe Illustrator:

You can display it on the web (even using custom fonts) in such a way that the lockup is preserved and scaleable:

See the Pen Example of a Text Lockup by Chris Coyier (@chriscoyier) on CodePen.

Read more.