Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. SVG
  3. Tutorials
  4. SVG from scratch
  5. Texts

Texts

When talking about text in SVG, we have to differentiate between two almost completely separate topics. One is the inclusion and display of text in an image, and the other is SVG fonts. The latter is described in a later section of the tutorial, while this page will focus on the first part: bringing text into an SVG image.

Basics

We have seen in theintroducing example that thetext element can be used to put arbitrary text in SVG documents:

xml
<text x="10" y="10">Hello World!</text>

Thex andy attributes determine where in the viewport the text will appear. The attributetext-anchor, which can have the values"start","middle","end" or"inherit", decides in which direction the text flows from this point. The attributedominant-baseline decides the vertical alignment.

Like with the shape elements, text can be colorized with thefill attribute and given a stroke with thestroke attribute. Both may also refer to gradients or patterns, which makes coloring text in SVG very powerful.

Setting font properties

An essential part of a text is the font in which it is displayed. SVG offers a set of attributes, many similar to their CSS counterparts, to enable font selection. Each of the following properties can be set as an attribute or via a CSS declaration:font-family,font-style,font-weight,font-variant,font-stretch,font-size,font-size-adjust,letter-spacing,word-spacing andtext-decoration.

Other text-related elements

tspan

This element is used to mark up sub-portions of a larger text. It must be a child of atext element or anothertspan element. A typical use case is to paint one word of a sentence bold red.

html
<svg width="350" height="60" xmlns="http://www.w3.org/2000/svg">  <text>    This is    <tspan font-weight="bold" fill="red">bold and red</tspan>  </text>  <style>    <![CDATA[      text {        dominant-baseline: hanging;        font: 28px Verdana, Helvetica, Arial, sans-serif;      }    ]]>  </style></svg>

Thetspan element has the following custom attributes:

x

Sets a new absolutex coordinate for the containing text. This overwrites the default current text position. The attribute may also contain a list of numbers that are one by one applied to the single characters of thetspan element.

dx

Starts drawing the text with a horizontal offsetdx from the default current position. Here, too, you may provide a list of values that are applied to consecutive characters, hence piling up the offset over time.

Likewise, there arey anddy for vertical displacement.

rotate

Rotates all characters by this degree. A list of numbers makes each character rotate to its respective value, with remaining characters rotating according to the last value.

textLength

Gives the calculated length of the string. This is a more obscure attribute, and it is meant to allow the rendering engine to fine-tune the positions of the glyphs when its own measured text length doesn't meet the one provided here.

textPath

This element fetches via itshref attribute an arbitrary path and aligns the characters, which it encircles, along this path:

html
<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">  <path d="M 20,20 C 80,60 100,40 120,20" fill="transparent" />  <text>    <textPath xmlns:xlink="http://www.w3.org/1999/xlink" href="#my_path">      A curve.    </textPath>  </text>  <style>    <![CDATA[      text {        dominant-baseline: hanging;        font: 28px Verdana, Helvetica, Arial, sans-serif;      }    ]]>  </style></svg>

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp