SVG Text – text, tspan, and textPath Elements

SVG can render three main kinds of graphic content: vector shapes, images, and text. SVG text is not just plain lettering on a canvas. It can be positioned with coordinates, styled with CSS or presentation attributes, transformed, clipped, and placed along a path. This article explains how text is written and arranged in SVG markup.

In this article, you will learn to:

  • Use <text> to place SVG text at a specific baseline position.
  • Use <tspan> for multiline SVG text and inline styling.
  • Use <textPath> to make text follow a path curve.
  • Control text alignment, direction, writing mode, textLength, and glyph spacing.
  • Understand the difference between characters, glyphs, and fonts.

What are Glyphs, Fonts and Characters?

Understanding the relationship between characters, glyphs, and fonts is essential when you need precise control over SVG text rendering.

SVG Text Content Elements

SVG defines three content elements that generate visible text:

ElementPurpose
<text>Defines a block of text.
<tspan>Provides fine‑grained positioning and styling inside <text>.
<textPath>Aligns text along a <path> curve.

How to Add Text to SVG

  1. Create a <text> element inside the <svg> document.
  2. Set the x and y attributes to position the text baseline.
  3. Add font and painting attributes such as font-family, font-size, fill, or stroke.
  4. Use <tspan> elements when one text block needs multiple lines or mixed styling.
  5. Use <textPath> when the text must follow a path instead of a straight baseline.
  6. Check the viewport, baseline, and font size if the text is clipped.

SVG <text> Element

The <text> element creates a text run. The x and y attributes position the start of the text baseline, not the top-left corner of a text box. Because the baseline sits near the bottom of the letters, a small y value can place part of the text above the viewport. In practice, set y large enough for the chosen font-size.

The following example shows why baseline position matters. The first text uses a very small y value, so the letters are clipped. The second text moves the baseline down and becomes fully visible ( svg-text-position.svg).

1<svg height="100" width="200" xmlns="http://www.w3.org/2000/svg">
2    <text x="10" y="6" fill="red">The text is not fully visible </text>
3    <text x="10" y="30" fill="green">The text is fully visible </text>
4</svg>

SVG text baseline positioning example showing clipped and visible text

The attributes of <text> and <tspan> control writing direction, alignment, font, spacing, and character positioning. The most common attributes are:

These attributes can be combined to create complex layouts, such as stretched text, right‑to‑left scripts, or vertical writing.

1<svg height="300" width="400" xmlns="http://www.w3.org/2000/svg">
2    <text x="180" y="30" fill="red">Aspose.SVG</text>
3    <text x="180" y="60" fill="blue" textLength="140">Aspose.SVG</text>
4    <text x="180" y="90" fill="grey" textLength="160" lengthAdjust="spacingAndGlyphs"
5          style="direction: rtl; unicode-bidi: bidi-override">Aspose.SVG</text>
6    <text x="180" y="120" fill="green" style="text-anchor: middle">Aspose.SVG</text>
7    <text x="260" y="90" style="writing-mode: tb">Aspose.SVG</text>
8</svg>

SVG text arrangement example showing textLength, text-anchor, right-to-left direction, and vertical writing

<tspan> – Inline Styling and Line Breaks

<tspan> can be nested inside <text> or another <tspan>. Being a child element, <tspan> serves several important functions in text displaying and formatting:

  1. SVG text does not automatically wrap to a new line. Use <tspan> elements to create separate lines and give each line its own position.
  2. Use <tspan> when only part of a text block needs a different style, position, color, font size, or font weight.
1<svg height="300" width="600" xmlns="http://www.w3.org/2000/svg">
2    <text x="20" y="60" style="font-family:arial">
3        <tspan style="font-weight:bold; font-size:55px">ASPOSE</tspan>
4        <tspan x="50" y="90" style="font-size:20px; fill:grey">Your File Format APIs </tspan>
5    </text>
6</svg>

SVG tspan example showing multiline text with different font sizes and fill colors

SVG <textPath> – Text Along a Curve

<textPath> attaches text to a <path> element, allowing characters to follow any vector curve. The path can be referenced in two ways:

 1<svg height="300" width="800" xmlns="http://www.w3.org/2000/svg">
 2    <path id="my_path1" d="M 50 100 Q 25 10 180 100 T 350 100 T 520 100 T 690 100"
 3          fill="transparent" />
 4    <path id="my_path2" d="M 50 100 Q 25 10 180 100 T 350 100"
 5          transform="translate(0,75)" fill="transparent" />
 6    <text>
 7        <textPath href="#my_path1">
 8            Aspose.SVG for .NET is a flexible library for SVG file processing and is fully compatible with its specifications.
 9        </textPath>
10        <textPath href="#my_path2">
11            Aspose.SVG for .NET is a flexible library for SVG file processing and is fully compatible with its specifications.
12        </textPath>
13    </text>
14</svg>

SVG textPath example showing text following two curved paths

If the path is shorter than the text, the overflow is clipped at the path’s end. Styling works the same way as with regular text: CSS properties such as font-weight, font-style, text-decoration, and text-transform can be applied.

The illustration shows two text strings attached to two different paths. The first line follows a long wave-like path, while the second line stops sooner because its referenced path is shorter.

Common Mistakes and Fixes

ProblemCauseSolution
Text is cut off at the top of the viewporty value is smaller than the font sizeSet y larger than the font size (e.g., y="30" for 24 px text)
Text does not follow the curve as expectedhref points to a non‑existent <path> IDVerify the <path> id and ensure the href value matches exactly
Glyphs appear distortedlengthAdjust set to spacing while using textLengthUse lengthAdjust="spacingAndGlyphs" to scale both spacing and glyphs
Right‑to‑left text still displays left‑to‑rightMissing unicode-bidi:bidi-overrideAdd style="direction: rtl; unicode-bidi: bidi-override" to the <text> or <tspan>
Text does not wrap to a new lineRelying on automatic wrappingInsert a new <tspan> with its own x/y coordinates for each line

Quick Recipes

GoalSVG Snippet (copy‑paste)
Center text horizontally<text x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">Centered</text>
Vertical text (top‑to‑bottom)<text x="20" y="20" writing-mode="tb">Vertical</text>
Stretch text to exact width<text x="10" y="40" textLength="200" lengthAdjust="spacingAndGlyphs">Stretch me</text>
Place text on a curve<path id="curve" d="M10,80 C40,10 65,10 95,80" fill="none"/> <text><textPath href="#curve">On a curve</textPath></text>
Apply underline and overline<text x="10" y="40" style="text-decoration: underline overline;">Styled</text>

FAQ – SVG Text

How do I position SVG text?

Set the x and y attributes on a <text> element. The y value positions the baseline, so it usually needs to be larger than the font size to avoid clipping at the top of the viewport.

How do I create multiline SVG text?

Use multiple <tspan> elements inside one <text> element. Give each line its own x and y values, or use dy to shift each line relative to the previous one.

Can SVG text follow a curve?

Yes. Define a <path> with an id, then reference it from <textPath href="#path-id">. The text follows the path geometry and can still be styled with text-related CSS properties.

Can I convert SVG text to vector outlines?

Yes. Text can be converted to vector paths when you need font-independent output. See Text Vectorization & Text Security – .NET and Convert SVG Text to Vector in Python.

Specification and Related Resources

Banner for Text to Vector application