SVG <text>

SVG 文本 - <text>

<text> 元素用于定義文本。

例子 1

寫一段文本:

I love SVG!

這是 SVG 代碼:

<svg height="30" width="200">
  <text x="0" y="15" fill="red">I love SVG!</text>
</svg>

親自試一試

例子 2

旋轉文本:

I love SVG

這是 SVG 代碼:

<svg height="60" width="200">
  <text x="0" y="15" fill="red" transform="rotate(30 20,40)">I love SVG</text>
</svg>

親自試一試

例子 3

<text> 元素可以與 <tspan> 元素一起,被安排在任意數量的子組中。每個 <tspan> 元素可以包含不同的格式和位置。

多行文本(使用 <tspan> 元素):

Several lines: First line. Second line.

這是 SVG 代碼:

<svg height="90" width="200">
  <text x="10" y="20" style="fill:red;">Several lines:
    <tspan x="10" y="45">First line.</tspan>
    <tspan x="10" y="70">Second line.</tspan>
  </text>
</svg>

親自試一試

例子 4

作為鏈接的文本(帶有 <a> 元素):

I love SVG!

這是 SVG 代碼:

<svg height="30" width="200" xmlns:xlink="http://www.w3.org/1999/xlink">
  <a xlink:href="www.codew3c.com/graphics/index.asp" target="_blank">
    <text x="0" y="15" fill="red">I love SVG!</text>
  </a>
</svg>

親自試一試