Denis Potschien January 3rd, 2016

SVG: How to Work With Text and Font

The SVG format is not only useful for the display of vector based shapes. You can also place and edit font in it. In contrast to semantic font labelling in HTML, there are some particular things you need to keep in mind. You should also know some differences to the markup via CSS.

Marking Fonts Via SVG and Editing With CSS

While HTML knows several semantic elements that allow you to mark text as a headline, paragraph, list or grid, the SVG format only knows the „<text>“ element. You can place and edit font within it.
<text>Lorem ipsum.</text>
The design of the text is done just like it is in HTML using CSS. You can integrate web fonts and define the font type, font size, and orientation. While the font and its size are specified with the known properties „font-family“ and „font-size“, there is the property „text-anchor“ for the orientation. It knows the values „start“ for left-aligned, „end“ for right-aligned and „middle“ for centered text.
text {
  font-family: "Source Sans";
  font-size: 30px;
  text-anchor: middle;
}
As SVG doesn't know the classic multiline text, setting line spacing is unnecessary. Like every other SVG element, the „<text>“ element is placed using „x“ and „y“ coordinates. The „y“ value only defines a text's body line. When the value 0 is assigned to the „y“ trait, the text disappears from the upper image border.

SVG: Creating Multiline Text

The SVG syntax doesn't know an element like „<br>“, which would allow it to add a word wrap within a text. However, there is the „<tspan>“ element, which you can use to divide a text and arrange it individually.
<text>
  <tspan>Lorem ipsum.</tspan>
  <tspan>Lorem ipsum.</tspan>
</text>
By default, the „<tspan>“ elements are placed next to each other, just like the „<span>“ element in HTML. The placement of SVG elements is done with the attributes „x“ and „y“. However, these attributes cause an absolute placement of elements. That means, the „<tspan>“ elements are neither placed in relation to the superior „<text>“ element nor in relation to other „<tspan>“ elements. text-schrift-svg_tspan1 Standard Arrangement of „<tspan>“ Elements However, for „<tspan>“, the individual attributes „dx“ and „dy“ are available. These are responsible for a relative placement to the previously marked „<tspan>“ element. text-schrift-svg_tspan2 „<tspan>“ elements have been placed via „dx“ and „dy“ Using „dy“ allows you to define the line spacing between single „<tspan>“ elements. As „dx“ is relative to the previously defined „<tspan>“ element as well, you need to use „x“ for the vertical placement.
<text>
  <tspan x="0" dy="0">Lorem ipsum.</tspan>
  <tspan x="0" dy="36px">Lorem ipsum.</tspan>
  <tspan x="0" dy="36px">Lorem ipsum.</tspan>
</text>
This example causes all text lines to start on the x coordinate 0 and makes them move down by 36 pixels per line on the y-axis. text-schrift-svg_tspan3 „<tspan>“ elements, that are placed using „x“ and „dy“

SVG: Text Following to the path

One advantage of the SVG format over HTML is that SVG allows you to align a text on a path. Thus, you are able to place a text on a circular path, for example. To do so, you need to use the „<textPath>“ element. First, you need to define a path to orientate the text on. It is necessary to create this path via „<path>“ element. Even when you want the text to be aligned in a circle shape, you can not mark this circle using „<circle>“. „<textPath>“, as the name already tells, only allows you to mark path elements.
<defs>
  <path id="textpath" cd="M200,125c0,41.4-33.6,75-75,75s-75-33.6-75-75s33.6-75…" />
</defs>
In the example, we're drawing a circle using „<path>“. As the circle itself shouldn't be visible, you can mark it within the „<defs>“ area. You also need to assign an ID to the path. text-schrift-svg_textpath Text aligned on a circle path Afterwards, the „<textPath>“ element is placed between the „<text>“ and the „<tspan>“ element. The path is assigned according to the ID.
<text>
  <textPath xlink:href="#textpfad" startOffset="50%">
    <tspan >Lorem ipsum.</tspan>
  </textPath>
</text>
You decide at which position on the path the text should start using the „startOffset“ attribute. Both absolute indication and indication in percent is possible.

Embedding fonts in SVG

Besides the option to integrate web fonts via CSS, you can also embed fonts in SVG. To do that, there are the elements „<font>“ and „<glyph>“. „<font>“ is used to define a font. Every symbol of the font is marked with „<glyph>“. The font's contour is deposited as a path. When you create SVG files in Illustrator, you can also embed fonts this way. This comes in handy when you need an SVG file that works without reference to external data. As embedded fonts cause much larger files than web fonts, you should preferably embed them via Stylesheets. In general, when dealing with SVG, you should keep the files as small as possible, especially with the mobile web in mind. (dpe) Image by Pexels from Pixabay

Denis Potschien

Denis works as a freelance web designer since 2005.

Leave a Reply

Your email address will not be published. Required fields are marked *