HTML SVG <text>

Beginner
8 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

The SVG <text> element draws a text label inside an <svg>. Position it with x and y, align with text-anchor, and style with font-size, fill, and optional stroke.

Use nested <tspan> for a second line or mixed styles. SVG text stays sharp at any scale — ideal for chart labels and icons.

Baseline · Widely available.

<text> is a stable SVG text-content element across modern browsers since about July 2015.

Paint with fill, not color.

In SVG, text colour comes from fill (and stroke). The HTML/CSS color property does not paint SVG text the same way.

No wrap by default.

Long strings stay on one line unless you use <tspan> lines or CSS white-space wrapping. Text not inside <text> is not rendered.

Syntax

text.html
<svg width="240" height="100" viewBox="0 0 240 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Example SVG text">
  <text x="120" y="55" text-anchor="middle"
        font-family="system-ui, sans-serif" font-size="28"
        fill="#2563eb">Hello SVG</text>
</svg>

Put the characters between the tags. y places the baseline.

Attributes

Core <text> pieces:

AttributeDescriptionTry it
x / yStart point; y is the baseline (default 0)Try it
text-anchorstart, middle, or endTry it
font-size / font-familyType size and faceTry it
fill / strokeText paint and outlineTry it
dx / dyRelative shifts (handy on <tspan>)Try it
<tspan>Styled or multiline chunk inside <text>Try it

Global Attributes

The <text> tag also supports the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Change font size and horizontal anchor. The anchor point stays at (120, 55).

28

y is the baseline. The red dot marks the (x, y) anchor.

SVG
Hello SVG

font-size="28" text-anchor="middle"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Label

Place text with x, y, font-size, and fill.

HTML
<svg width="240" height="100" viewBox="0 0 240 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Basic SVG text label">
  <text x="24" y="58"
        font-family="system-ui, sans-serif" font-size="28"
        fill="#2563eb">Hello SVG</text>
</svg>
Try It Yourself

How It Works

  1. Create an <svg> with a matching viewBox.
  2. Add <text> with the characters between the tags.
  3. Set x / y — remember y is the baseline.
  4. Style with font-size and fill.

Example 2 — Centered Text

Use text-anchor="middle" with x at the center.

HTML
<svg width="240" height="100" viewBox="0 0 240 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Centered SVG text">
  <text x="120" y="55" text-anchor="middle"
        font-family="system-ui, sans-serif" font-size="28"
        fill="#16a34a">Centered</text>
</svg>
Try It Yourself

How It Works

  1. Put x at the midpoint of the SVG (here 120).
  2. Set text-anchor="middle" so the string centers on that point.
  3. start (default) aligns the left edge; end aligns the right.
  4. Useful for chart titles and badge labels.

Example 3 — Fill and Stroke

Outline letters with stroke while keeping a fill.

HTML
<svg width="240" height="100" viewBox="0 0 240 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Outlined SVG text">
  <text x="120" y="58" text-anchor="middle"
        font-family="system-ui, sans-serif" font-size="36" font-weight="700"
        fill="#facc15" stroke="#0f172a" stroke-width="1.5">Outline</text>
</svg>
Try It Yourself

How It Works

  1. fill paints the letter interiors.
  2. stroke draws the outline (see also SVG stroke).
  3. Keep stroke-width modest so glyphs stay readable.
  4. fill="none" creates outline-only text.

Example 4 — Multiline with tspan

Shift the second line down with dy.

HTML
<svg width="240" height="120" viewBox="0 0 240 120"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Two-line SVG text with tspan">
  <text x="120" y="45" text-anchor="middle"
        font-family="system-ui, sans-serif" font-size="24"
        fill="#0f172a">
    Line one
    <tspan x="120" dy="32">Line two</tspan>
  </text>
</svg>
Try It Yourself

How It Works

  1. Keep one parent <text>.
  2. Add a <tspan> for the next line.
  3. Reset x and set dy for vertical spacing.
  4. SVG does not wrap automatically — you choose each line.

Example 5 — Styled tspan

Restyle part of a sentence without a second <text>.

HTML
<svg width="280" height="100" viewBox="0 0 280 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="SVG text with a styled tspan">
  <text x="24" y="58"
        font-family="system-ui, sans-serif" font-size="26"
        fill="#0f172a">
    Learn
    <tspan fill="#7c3aed" font-weight="700">SVG text</tspan>
    today
  </text>
</svg>
Try It Yourself

How It Works

  1. Wrap the highlight in <tspan>.
  2. Override fill / font-weight on that span only.
  3. Other characters keep the parent <text> styles.
  4. Great for emphasis inside a single graphic label.

Edge Cases & Pitfalls

Most missing-text bugs are baseline position, paint, or missing <text>.

baseline

y is the baseline

Letters hang mostly above y. A small y can clip the top of the glyphs.

wrap

No automatic wrapping

Long strings stay on one line. Use <tspan> lines or CSS wrapping when you need breaks.

paint

Use fill, not color

SVG text is painted with fill/stroke. Setting only CSS color often leaves text looking wrong or invisible.

a11y

Label meaningful graphics

Visible <text> can help, but decorative SVGs still need role="img" and an accessible name when appropriate.

Browser Support

The SVG <text> element is supported in every modern browser.

✓ Baseline · Widely available

SVG <text>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support text, tspan, text-anchor, and fill/stroke styling. Internet Explorer 9+ supports basic SVG text.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
<text> Widely supported

Bottom line: Safe for production chart labels, badges, and icon captions. Prefer SVG text for graphic labels; use HTML for long readable page copy.

Key Takeaways

  • Position: x, baseline y.
  • Align: text-anchor (start / middle / end).
  • Paint: fill (and optional stroke).
  • Lines / style: nest <tspan>.

One line: <text x y> + fill / font-size inside an <svg>.

Default CSS Settings

Conceptual defaults for SVG text:

defaults.css
/* Conceptual SVG text defaults */
text {
  fill: black;          /* not CSS color */
  stroke: none;
  font-size: medium;
  text-anchor: start;
  white-space: nowrap;  /* no wrap by default */
}

Set font-family, font-size, and fill explicitly in examples so results stay predictable.

Frequently Asked Questions

Use a <text> element inside <svg>. Set x and y for position, then style with font-size, font-family, and fill.
x is the horizontal position. y is the text baseline (not the top of the letters), so text can look a bit lower than beginners expect.
Set text-anchor="middle" and place x at the center point. For vertical centering, also use a suitable y or dominant-baseline.
Yes. Use stroke and stroke-width like shapes. You can combine fill and stroke, or use fill="none" for outline-only text.
Common causes: text outside a <text> element, coordinates outside the viewBox, fill none with no stroke, or a tiny font-size.
Use <tspan> for a second line (with dy) or differently styled words inside one <text>. Use HTML text for long page copy.

Next: Styling & Effects

Browse stroke, filters, and fills, then open each guide.

Styling & Effects →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful