Skip to content

text 相关 ​

在 SVG 中,<text> 元素用于渲染文本,它拥有非常丰富的属性体系。为了让你更清晰地掌握,我将属性分为 定位布局、样式外观、文本排版 和 SVG 特有高级属性 四大类。


一、定位与布局属性 ​

属性说明示例值
x文本基线的起始 X 坐标x="50"
y文本基线的起始 Y 坐标y="30"
dx相对于当前位置的 X 偏移量(可逐个字符设置)dx="10,20,5"
dy相对于当前位置的 Y 偏移量(可逐个字符设置)dy="2,5,0"
textLength强制文本渲染后的总长度(会拉伸或压缩)textLength="200"
lengthAdjust配合 textLength,指定调整方式spacing(调整间距) / spacingAndGlyphs(调整字宽)
html
<text x="20" y="40" dx="5,10,15" dy="2,0,0">SVG</text>

二、样式与外观属性(CSS + 呈现属性) ​

这些属性既可以用 CSS 写,也可以作为 XML 属性直接写在标签上。

属性说明示例值
fill文本颜色fill="red" / fill="#ff0000"
fill-opacity填充透明度(0~1)fill-opacity="0.8"
stroke文本描边颜色stroke="black"
stroke-width描边宽度stroke-width="2"
font-family字体font-family="Arial, sans-serif"
font-size字号font-size="24" / font-size="1.5em"
font-weight字重bold / normal / 100~900
font-style字体样式italic / normal / oblique
text-decoration文本装饰underline / line-through / overline
letter-spacing字符间距letter-spacing="2"
word-spacing单词间距word-spacing="5"
opacity整体透明度opacity="0.6"
transform变换(旋转/缩放/平移)transform="rotate(15, 50, 50)"

三、文本排版与对齐属性 ​

属性说明示例值
text-anchor文本水平对齐(相对于 x 定位点)start(左) / middle(中) / end(右)
dominant-baseline垂直对齐基线(相对于 y 定位点)auto / middle / hanging / alphabetic / central
text-rendering渲染质量优化auto / optimizeSpeed / optimizeLegibility / geometricPrecision
direction文本方向ltr / rtl
unicode-bidi双向文本控制normal / embed / bidi-override
html
<text x="100" y="50" text-anchor="middle" dominant-baseline="central">居中文本</text>

四、SVG 特有高级排版属性 ​

1. <tspan> 相关(行内片段控制) ​

通过 <tspan> 子元素可精细控制片段样式:

html
<text>
  普通文字 <tspan fill="red" font-weight="bold">红色加粗</tspan> 继续普通
</text>

<tspan> 支持以下特有属性:

  • x / y / dx / dy(可单独偏移)
  • rotate(逐字符旋转角度)
  • baseline-shift(基线偏移,用于上/下标)

2. 文本路径 <textPath> ​

让文本沿着 <path> 路径排列:

html
<defs>
  <path id="myPath" d="M20,50 Q100,10 180,50" />
</defs>
<text>
  <textPath href="#myPath" startOffset="20%">
    沿着曲线行走的文字
  </textPath>
</text>

startOffset 控制文字从路径的什么位置开始。

3. 特殊呈现属性 ​

属性说明示例值
rotate对每个字符单独设置旋转角度(列表)rotate="0,10,20,0"
glyph-orientation-vertical竖排文字时字符方向(已不推荐)auto / 0 / 90
writing-mode书写模式(竖排支持)horizontal-tb / vertical-rl

五、完整示例(综合运用) ​

html
<svg viewBox="0 0 400 200" xmlns="http://www.w3.org/2000/svg">
  
  <!-- 基础样式 -->
  <text x="50" y="50" 
        font-family="Arial" font-size="28" font-weight="bold"
        fill="#2c3e50" stroke="#3498db" stroke-width="1"
        text-anchor="start" dominant-baseline="alphabetic">
    基础 SVG 文本
  </text>

  <!-- 居中 + 描边 + 下划线 -->
  <text x="200" y="110" 
        font-size="30" fill="white" stroke="#e74c3c" stroke-width="2"
        text-anchor="middle" dominant-baseline="central"
        text-decoration="underline">
    居中对齐
  </text>

  <!-- tspan 片段控制 + 旋转 -->
  <text x="50" y="170" font-size="24" fill="#333">
    这是 
    <tspan fill="#e67e22" font-weight="bold" font-style="italic">橙色斜体</tspan>
    的文字
  </text>

  <!-- 文本路径 -->
  <defs>
    <path id="wave" d="M20,210 Q100,180 180,210 Q260,240 340,210" fill="none" stroke="#ddd"/>
  </defs>
  <text font-size="18" fill="#8e44ad">
    <textPath href="#wave" startOffset="10%">
      沿着波浪路径行走的文字
    </textPath>
  </text>
</svg>

六、快速对照表(常用场景) ​

需求场景推荐属性组合
普通标题文字x y font-size fill font-weight
居中标题x="50%" text-anchor="middle" dominant-baseline="central"
带描边的空心字fill="none" stroke="color" stroke-width="n"
多色/多样式片段使用 <tspan> 子元素
沿曲线排列<textPath> + <path>
自适应容器宽度textLength + lengthAdjust="spacing"
单个字符分别偏移dx / dy / rotate 使用列表值

最后更新:

由 vercel 提供快速的部署服务