Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

SVG <text> and <tspan>


SVG Text - <text>

The <text> element is used to define a text.

The <text> element has seven basic attributes to position and rotate the text:

Attribute Description
x The x position of the start of the text. Default is 0
y The y position of the start of the text. Default is 0
dx The horizontal shift position for text (from previous text position)
dy The vertical shift position for text (from previous text position)
rotate The rotation (in degrees) applied to each letter of text
textLength The width that the text must fit in
lengthAdjust How the text should be compressed or stretched to fit the width defined by the textLength attribute

A Simple Text

Write a simple text with SVG:

I love SVG! Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg height="30" width="200" xmlns="http://www.w3.org/2000/svg">
  <text x="5" y="15" fill="red">I love SVG!</text>
</svg>
Try it Yourself »

A Text With no Fill

A text with no fill and stroke only:

I love SVG! Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg height="40" width="200" xmlns="http://www.w3.org/2000/svg">
  <text x="5" y="30" fill="none" stroke="red" font-size="35">I love SVG!</text>
</svg>
Try it Yourself »

A Text With Fill and Stroke

A text with fill and stroke:

I love SVG! Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg height="40" width="200" xmlns="http://www.w3.org/2000/svg">
  <text x="5" y="30" fill="pink" stroke="blue" font-size="35">I love SVG!</text>
</svg>
Try it Yourself »

Rotate Each Letter of Text

Rotate each letter of the text, by a degree, with the rotate attribute:

I love SVG! Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg height="40" width="200">
  <text x="5" y="30" fill="red" font-size="35" rotate="30">I love SVG!</text>
</svg>
Try it Yourself »


Rotate Whole Text

Rotate the whole text with the transform attribute:

I love SVG! Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg height="100" width="200">
  <text x="5" y="30" fill="red" font-size="25" transform="rotate(30 20,40)">I love SVG!</text>
</svg>
Try it Yourself »

SVG Text - <tspan>

The <tspan> element is used to mark up parts of a text (just like the HTML <span> element).

The <tspan> element must be a child of a <text> element or another <tspan> element.

The <tspan> element has six basic attributes to position and rotate the text:

Attribute Description
x Sets a new absolute x position for the start of the text in tspan
y Sets a new absolute y position for the start of the text in tspan
dx The horizontal shift position for text in tspan (from previous text position)
dy The vertical shift position for text in tspan (from previous text position)
rotate The rotation (in degrees) applied to each letter of text in tspan
textLength The width that the text in tspan must fit in

Use of <tspan> Element

Use of the <tspan> element inside the <text> element:

I Love SVG! Sorry, your browser does not support inline SVG.

Here is the SVG code:

Example

<svg height="40" width="250" xmlns="http://www.w3.org/2000/svg">
  <text x="5" y="30" fill="red" font-size="35">I Love
    <tspan fill="none" stroke="green">SVG</tspan>!
  </text>
</svg>
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.