text

  • Revision slug: Web/SVG/Element/text
  • Revision title: text
  • Revision id: 491479
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Updated tags

Revision Content

{{ SVGRefElem() }}

The text element defines a graphics element consisting of text. Note that it is possible to apply a gradient, pattern, clipping path, mask or filter to text

Usage context

Categories Graphics element, text content element
Permitted content Character data and any number of the following elements, in any order:
Animation elements »
Descriptive elements »
Text content child elements »
{{ SVGElement("a") }}
Normative document SVG 1.1 (2nd Edition)

Example

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg"
     width="100px" height="30px" viewBox="0 0 1000 300">

  <text x="250" y="150" 
        font-family="Verdana" 
        font-size="55">
    Hello, out there
  </text>

  <!-- Show outline of canvas using 'rect' element -->
  <rect x="1" y="1" width="998" height="298"
        fill="none" stroke-width="2" />
</svg>

The <text> element is used to draw text . The following code example is used for drawing a text with coordinates.

<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50px">
  <text x="10" y="20">SVG Text Example</text>
</svg>

SVG text can be rotated. The following code example illustrates it.

<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50px">
  <text x="10"  y="20" 
        transform="rotate(30 20,40)">
    SVG Text Rotation example
  </text>
</svg>

SVG text can also be styled

<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50px">
  <text x="10"  y="20"
        style="font-family: Times New Roman;
               font-size  : 24;
               stroke     : #00ff00;
               fill       : #0000ff;">
    SVG text styling
  </text>
</svg>

Attributes

Global attributes

Specific attributes

  • {{ SVGAttr("x") }}
  • {{ SVGAttr("y") }}
  • {{ SVGAttr("dx") }}
  • {{ SVGAttr("dy") }}
  • {{ SVGAttr("rotate") }}
  • {{ SVGAttr("textLength") }}
  • {{ SVGAttr("lengthAdjust") }}

DOM Interface

This element implements the SVGTextElement interface.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) IE Opera Safari
Basic support {{ CompatChrome('1.0') }} {{ CompatGeckoDesktop('1.8') }} {{ CompatIE('9.0') }} {{ CompatOpera('8.0') }} {{ CompatSafari('3.0.4') }}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatAndroid('3.0') }} {{ CompatGeckoMobile('1.8') }} {{ CompatNo() }} {{ CompatVersionUnknown() }} {{ CompatSafari('3.0.4') }}

The chart is based on these sources.

  • {{ SVGElement("tspan") }}
  • {{ SVGElement("tref") }}
  • {{ SVGElement("altGlyph") }}

Revision Source

<p>{{ SVGRefElem() }}</p>
<p>The <code>text</code> element defines a graphics element consisting of text. Note that it is possible to apply a gradient, pattern, clipping path, mask or filter to text</p>
<h2 id="Usage_context">Usage context</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="row">Categories</th>
   <td>Graphics element, text content element</td>
  </tr>
  <tr>
   <th scope="row">Permitted content</th>
   <td>Character data and any number of the following elements, in any order:<br />
    <a href="/en/SVG/Element#Animation" title="en/SVG/Attribute#Animation">Animation elements</a>&nbsp;»<br />
    <a href="/en/SVG/Element#Descriptive" title="en/SVG/Attribute#Descriptive">Descriptive elements</a>&nbsp;»<br />
    <a href="/en/SVG/Element#TextContentChild" title="en/SVG/Attribute#TextContentChild">Text content child elements</a>&nbsp;»<br />
    {{ SVGElement("a") }}</td>
  </tr>
  <tr>
   <th scope="row">Normative document</th>
   <td><a class="external" href="http://www.w3.org/TR/SVG/text.html#TextElement" title="http://www.w3.org/TR/SVG/text.html#TextElement">SVG 1.1 (2nd Edition)</a></td>
  </tr>
 </tbody>
</table>
<h2 id="Example">Example</h2>
<pre class="brush: xml">
&lt;?xml version="1.0"?&gt;
&lt;svg xmlns="http://www.w3.org/2000/svg"
     width="100px" height="30px" viewBox="0 0 1000 300"&gt;

  &lt;text x="250" y="150" 
        font-family="Verdana" 
        font-size="55"&gt;
    Hello, out there
  &lt;/text&gt;

  &lt;!-- Show outline of canvas using 'rect' element --&gt;
  &lt;rect x="1" y="1" width="998" height="298"
        fill="none" stroke-width="2" /&gt;
&lt;/svg&gt;
</pre>
<p>The &lt;text&gt; element is used to draw text . The following code example is used for drawing a text with coordinates.</p>
<pre class="brush: xml">
&lt;svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50px"&gt;
  &lt;text x="10" y="20"&gt;SVG Text Example&lt;/text&gt;
&lt;/svg&gt;
</pre>
<p>SVG text can be rotated. The following code example illustrates it.</p>
<pre class="brush: xml">
&lt;svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50px"&gt;
  &lt;text x="10"&nbsp; y="20" 
        transform="rotate(30 20,40)"&gt;
    SVG Text Rotation example
  &lt;/text&gt;
&lt;/svg&gt;
</pre>
<p>SVG text can also be styled</p>
<pre class="brush: xml">
&lt;svg xmlns="http://www.w3.org/2000/svg" width="100px" height="50px"&gt;
  &lt;text x="10"&nbsp; y="20"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; style="font-family: Times New Roman;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp; font-size&nbsp; : 24;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stroke&nbsp;&nbsp;&nbsp;&nbsp; : #00ff00;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp; fill&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : #0000ff;"&gt;
    SVG text styling
  &lt;/text&gt;
&lt;/svg&gt;
</pre>
<h2 id="Attributes">Attributes</h2>
<h3 id="Global_attributes">Global attributes</h3>
<ul>
 <li><a href="/en/SVG/Attribute#ConditionalProccessing" title="en/SVG/Attribute#ConditionalProccessing">Conditional processing attributes</a>&nbsp;»</li>
 <li><a href="/en/SVG/Attribute#Core" title="en/SVG/Attribute#Core">Core attributes</a>&nbsp;»</li>
 <li><a href="/en/SVG/Attribute#GraphicalEvent" title="en/SVG/Attribute#GraphicalEvent">Graphical event attributes</a>&nbsp;»</li>
 <li><a href="/en/SVG/Attribute#Presentation" title="en/SVG/Attribute#Presentation">Presentation attributes</a>&nbsp;»</li>
 <li>{{ SVGAttr("class") }}</li>
 <li>{{ SVGAttr("style") }}</li>
 <li>{{ SVGAttr("externalResourcesRequired") }}</li>
 <li>{{ SVGAttr("transform") }}</li>
</ul>
<h3 id="Specific_attributes">Specific attributes</h3>
<ul>
 <li>{{ SVGAttr("x") }}</li>
 <li>{{ SVGAttr("y") }}</li>
 <li>{{ SVGAttr("dx") }}</li>
 <li>{{ SVGAttr("dy") }}</li>
 <li>{{ SVGAttr("rotate") }}</li>
 <li>{{ SVGAttr("textLength") }}</li>
 <li>{{ SVGAttr("lengthAdjust") }}</li>
</ul>
<h2 id="DOM.C2.A0Interface">DOM&nbsp;Interface</h2>
<p>This element implements the <code><a href="/en/DOM/SVGTextElement" title="en/DOM/SVGTextElement">SVGTextElement</a></code> interface.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>IE</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatChrome('1.0') }}</td>
    <td>{{ CompatGeckoDesktop('1.8') }}</td>
    <td>{{ CompatIE('9.0') }}</td>
    <td>{{ CompatOpera('8.0') }}</td>
    <td>{{ CompatSafari('3.0.4') }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE&nbsp;Phone</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatAndroid('3.0') }}</td>
    <td>{{ CompatGeckoMobile('1.8') }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatSafari('3.0.4') }}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>The chart is based on <a href="/en/SVG/Compatibility_sources" title="en/SVG/Compatibility sources">these sources</a>.</p>
<h2 id="Related">Related</h2>
<ul>
 <li>{{ SVGElement("tspan") }}</li>
 <li>{{ SVGElement("tref") }}</li>
 <li>{{ SVGElement("altGlyph") }}</li>
</ul>
Revert to this revision