【问题标题】:Text with image background in svg or css带有 svg 或 css 图像背景的文本
【发布时间】:2012-06-02 00:30:11
【问题描述】:

svg 或 css 中的文本是否可以有背景?这意味着,每个角色都有背景? 这是一个例子:

我在 Photoshop 中制作了这个并制作了一个蒙版,我只是想知道我可以在 svg 或 css 中使用它吗?

【问题讨论】:

标签: css svg


【解决方案1】:

这是一个使用模式的示例。如果需要,它使用带有图案填充的<tspan> 元素来向您展示如何在每个字符的基础上完成此操作:

演示:http://jsfiddle.net/xWNR3/2/

<svg xmlns="http://www.w3.org/2000/svg" xmlns:x="http://www.w3.org/1999/xlink">
  <style>
    svg  { background:#ddd }
    text {
      font-family:Verdana; font-size:160pt; font-weight:bold;
      stroke:#000;
    }
  </style>
  <defs>
    <pattern id="p1" patternUnits="userSpaceOnUse" width="32" height="32">
      <image x:href="alphaball.png" width="32" height="32" />
    </pattern>
    <pattern id="p2" patternUnits="userSpaceOnUse" width="10" height="10">
      <image x:href="grid.gif" width="10" height="10" />
    </pattern>
  </defs>
  <text x="20" y="170" fill="url(#p1)">
    Hello
    <tspan x="20" y="350"
           fill="url(#p2)">Wo<tspan fill="url(#p1)">r</tspan>ld</tspan>
  </text>
</svg>

【讨论】:

    【解决方案2】:

    除了 robertc 建议的答案中的模式方式之外,您也可以使用 maskclipPath 来执行此操作。

    这是来自使用 clipPath 的 svg 测试套件中的 an example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多