O'Reilly logo

SVG Text Layout by Kurt Cagle, Amelia Bellamy-Royds

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. The Perfect Fit

If you cannot always control the font the browser uses, even with web fonts, what can you do to ensure that your overall text layout is preserved?

There are two ways you can hint to the browser how much space a given text element should take up. One comes from CSS, and adjusts the used font size. The other is SVG specific, and adjusts the spacing and width of the letters. Surprisingly, it is the CSS option that has the poorer browser support, although neither is as reliable as one would like.

Fixing Font Size

To reemphasize why text size control is important, consider Figure 11-1. It shows the SVG from Figure 10-2, and then the exact same SVG code displayed using a fallback cursive font (Comic Sans MS) instead of the chosen web fonts. The font itself doesn’t have the fun and artistic feel of the web fonts, but it is the sizing that really creates the problem. The floating letters running off the page significantly distract from the appearance of writing on paper, don’t you think?

svtl 1101
Figure 11-1. An SVG designed for two custom fonts: rendered with the intended fonts (left) and rendered with a fallback font (right)

Part of the problem is that the lowercase letters in Comic Sans take up a much larger proportion of the height; in other words, their ex-height is larger for the same em-height. The letters are correspondingly wider, despite the same font-size ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required