<svg viewBox="0 0 100 100"> <text> <tspan class="line-1" textLength="100" x="0" y="1em" font-size="20"> The Cat </tspan> <tspan class="line-2" textLength="100" x="0" dy="0.9em" font-size="20" lengthAdjust="spacingAndGlyphs"> in the </tspan> <tspan class="line-3" textLength="100" x="0" dy="0.9em" font-size="35"> Hat </tspan> </text> </svg>
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.
Another one of those attributes is
lengthAdjust. This only applies when
<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.
<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:
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: