In addition to shapes, SVG can render text. They are rendered like any other graphical element so they are subject to the same features — painting, coordinate system transformation, clipping, etc. — as any other shape.

<text>

A single string of text is rendered by the <text> element but be aware that there is no automatic line breaking or word wrapping. The <text> element has countless attributes, some of which are listed below.

AttributePurposeValueDefault
baseline-shiftControl subscripts and superscriptsbaseline | sub | superbaseline
dx,dyShift the current text position in the x- and y-directions.A decimal number.0
fillControl the fill colour of the glyph.none or hash sign # followed by six-digit hexadecimal number representing the RGB colour.black
fill-opacityControl the opacity of the fill of the glyphA decimal number between 0 and 1, with values toward 0 more transparent and values toward 1 more opaque.1
font-familyControl the font family.Name of a font family, either generic (e.g. sans-serif) or specific (e.g. Arial).
font-sizeControl the font sizeAn absolute size (e.g. 16 or 16px) or a relative size (e.g. medium).medium
font-styleControl the style of the fontnormal | italic | obliquenormal
font-variantControl the use of small capitalsnormal | small-capsnormal
font-weightControl the weight of the fontlighter | normal | bold | boldernormal
idProvide a unique identifier for a line of textUnique identification or name
kerningControl the spacing between glyphsauto or a number, a number plus unit of measurement, or percentageauto
lengthAdjustControl stretching or compression of the text to fit the space.spacing | spacingAndGlyphsspacing
rotateControl the orientation of each individual glyph.List of numbers
strokeControl the outline colour of the glyphnone or hash sign # followed by six-digit hexadecimal number representing the RGB colour.black
stroke-opacityControl the outline opacity of the glyphA decimal number between 0 and 1, with values toward 0 more transparent and values toward 1 more opaque.1
stroke-widthControl the outline width of the glyphA number or a number plus unit of measurement (such as px or em).1
text-anchorControl horizontal alignment.start | middle | endstart
text-decorationControl various lines above, through or below the textnone | overline | line-through | underlinenone
transformControl various transformationsSome combination of rotate, scale, skew and translate.
word-spacingControl the spacing between wordsnormal or a number, a number plus unit of measurement, or percentagenormal
x,yControl the starting position of the text

Examples

Here is some cyan-coloured text without an outline.

Once upon a time… <text x=”25″ y=”50″ fill=”#00ffff” font-size=”48″ stroke=”none”> Once upon a time…</text>

This text consists of an outline only.

Once upon a time… <text x=”25″ y=”50″ fill=”none” font-size=”48″ stroke=”#000099″ stroke-width=”3″>Once upon a time…</text>

Now combine colour and an outline.

Once upon a time… <text x=”25″ y=”50″ fill=”#ffff00″ font-size=”48″ stroke=”#000000″ stroke-width=”1″>Once upon a time…</text>

There are a number of ways to decorate the text besides colour and outline. Here we see a serif typeface that is italicised and underlined.

Once upon a time… <text x=”25″ y=”50″ fill=”#ff0000″ font-family=”serif” font-size=”48″ font-style=”italic” stroke=”none” text-decoration=”underline”> Once upon a time…</text>

It is easy to achieve a shadow effect. Using two <text> elements, the first (purple) is shifted slightly downward and to the right whilst the second (magenta) is in the correct position. The shadow text must be declared first since it will be overwritten by the actual text.

Once upon a time… Once upon a time… <text x=”25″ y=”50″ dx=”2″ dy=”2″ fill=”#800080″ font-size=”48″ stroke=”none”>Once upon a time…</text> <text x=”25″ y=”50″ fill=”#ff00ff” font-size=”48″ stroke=”none”> Once upon a time…</text>

Use the text-anchor attribute to left-justify, centre, or right-justify the text.

Once upon a time… Once upon a time… Once upon a time… <text x=”25″ y=”50″ fill=”#00ff00″ font-size=”24″ stroke=”none” text-anchor=”start”>Once upon a time…</text> <text x=”250″ y=”50″ fill=”#008000″ font-size=”24″ stroke=”none” text-anchor=”middle”>Once upon a time…</text> <text x=”475″ y=”50″ fill=”#003300″ font-size=”24″ stroke=”none” text-anchor=”end”>Once upon a time…</text>

<tspan>

The <tspan> tags encloses text within the <text> element. It is used to modify the <text> attributes for a subset of the text.

Examples

Use the baseline-shift attribute to produce subscripts and superscripts.

eπi + 1 = 0 <text x=”25″ y=”50″ fill=”#000000″ font-size=”48″ stroke=”none”> e<tspan baseline-shift=”super” fill=”#ff0000″ font-size=”36″ font-style=”italic”>πi</tspan> <tspan fill=”#0000ff”>+</tspan> 1 <tspan fill=”#0000ff”>=</tspan> 0</text>

The dx and dy attributes can take a series of numbers to produce some interesting effects. Each number is applied to individual glyphs.

Falling apart Falling away <text x=”25″ y=”50″ fill=”#000000″ font-size=”48″ stroke=”none”>Falling <tspan fill=”#ff0000″ dx=”3 5 8 13 21″>apart</tspan></text> <text x=”25″ y=”100″ fill=”#000000″ font-size=”48″ stroke=”none”>Falling <tspan fill=”#00ff00″ dy=”3 5 8 13″>away</tspan></text>

The font-weight attribute can be used to make things bold.

To boldly go! <text x=”25″ y=”50″ fill=”#000000″ font-size=”48″ stroke=”none”>To <tspan fill=”#ff0000″ font-weight=”bold”>boldly</tspan> go!</text>

Since SVG does not provide automatic line breaks or word-wrap, you must break up lines of text manually.

It was the best of times, it was the worst of times, <text fill=”#00ffff” font-family=”serif” font-size=”36″ stroke=”#000099″ stroke-width=”1″>   <tspan x=”25″ y=”50″>It was the best of times,</tspan>   <tspan x=”25″ y=”100″>it was the worst of times,</tspan> </text>

<textPath>

It is also possible to write text along a pre-defined path using the <textPath> element. <textPath> takes the following attributes:

AttributePurposeValueDefault
hrefThe URL of the path or shape on which to render the text. If the path attribute is set, this has no effect.name
lengthAdjustControl stretching or compression of the text to fit the space.spacing | spacingAndGlyphsspacing
methodControl the rendering of each glyph along the path.align | stretchalign
pathDescription of the path along which the text is to be rendered.path descriptors
sideControl which side of the path the text is to be rendered.left | rightleft
spacingControl the spacing between glyphsauto | exactexact
startOffsetControl how far from the beginning of the path the text should start.number or percentage0
textLengthControl the width of the space into which the text will render.number of percentageauto

Examples

The actual path is not visible but has been included (light grey) so you can see how the text follows it.

This text is along a curved path <defs>   <path id=”theCurve” d=”M 25,50 L 125,50 A 150,100 0 0,0 375,50 L 475,50″/> </defs> <text x=”0″ y=”0″ fill=”#ff00ff” font-size=”28″ stroke=”none”> <textPath href=”#theCurve”>This text is along a curved path</textPath></text>

Let’s look at the same example but with the startOffset attribute set to 20%.

This text is along a curved path <defs>   <path id=”theCurve” d=”M 25,50 L 125,50 A 150,100 0 0,0 375,50 L 475,50″/> </defs> <text x=”0″ y=”0″ fill=”#ff00ff” font-size=”28″ stroke=”none”> <textPath href=”#theCurve” startOffset=”20%”> This text is along a curved path</textPath></text>