Code

SVG

Text

Beginner SVG text and tspan elements: alignment, baseline control, letter-spacing, and textPath for text along a curve. MDN text

startmiddleend← text-anchor → Mix bold italic underline via tspanletter-spacing: 4automiddlehanging← dominant-baselineText flowing along a quadratic Bézier curve ✦
<!-- text-anchor: start | middle | end -->
<text x="240" y="50"
      text-anchor="middle"
      font-size="14"
      fill="#e11d48">middle</text>

<!-- tspan for inline style runs -->
<text x="40" y="115" font-size="14">
  Mix
  <tspan fill="#4f46e5"
         font-weight="bold"> bold</tspan>
  <tspan fill="#e11d48"
         font-style="italic"> italic</tspan>
</text>

<!-- letter-spacing -->
<text letter-spacing="4">
  spaced text
</text>

<!-- dominant-baseline -->
<text dominant-baseline="middle">
  centred on baseline
</text>

<!-- textPath: text on a curve -->
<defs>
  <path id="curve"
    d="M 40 220 Q 240 100 440 220"/>
</defs>
<text font-size="12">
  <textPath href="#curve"
            startOffset="10%">
    Along a Bézier curve
  </textPath>
</text>
Key Attributes
  • text-anchorstart | middle | end — horizontal alignment relative to x
  • dominant-baselineauto | middle | hanging — vertical alignment relative to y
  • tspan — inline child element for mixed styles within one text block
  • dx / dy on tspan — relative offset from previous character
  • letter-spacing / word-spacing — spacing between glyphs/words
  • textPath href="#id" — flows text along any path shape
  • startOffset — percentage or length along the path where text begins