css - SVG 元素的字体可以在 CSS 文件中声明吗?

标签 css svg fonts

有什么方法可以从链接的 CSS 文件中定义/更改/设置 SVG 元素的字体属性?还是所有这些数据都必须固定到 SVG 文件本身?强调“固定”。恶心。

换句话说,做下面这样的事情是否有效?

SVG:

<svg>
  <g id="buttonText">
    <text id="buttonTextShadow">CLICK ME</text>
    <text id="buttonTextColor">CLICK ME</text>
  </g>
</svg>

CSS:

#buttonTextColor, #buttonTextShadow  { font-family: "OpenSans"; }
#buttonTextColor { color: #fff; }
#buttonTextShadow { color: #333; }

如果能够从样式表中定位和设计这些东西会很好……如果没有,我会适应……:P

TIA!

WR!

最佳答案

这似乎有效,您可以根据需要直接定位 id

<svg>
  <defs>
    <style type="text/css">
      @font-face {
        font-family: "OpenSans";
        src: url('/path/to/font.woff');
      }

      #buttonText {
        font-family: "OpenSans";
      }
    </style>
  </defs>
  <g id="buttonText">
    <text id="buttonTextShadow">CLICK ME</text>
    <text id="buttonTextColor">CLICK ME</text>
  </g>
</svg>

关于css - SVG 元素的字体可以在 CSS 文件中声明吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37220354/

相关文章:

javascript - Chrome 有时在重绘之前不会呈现 Google 字体

java - Swing 文字看起来很糟糕

css - 如何防止内容重新调整其包装器 div 的大小?

css - css 选择器中的重复,SCSS

javascript - 如何使 svg 下的背景图像的任何部分都具有双色调?

javascript - 如何在不直接粘贴代码的情况下访问 html 中的 SVG 元素(最好使用 CSS)?

css - 使用 css 删除产品描述中图像的底部边距?

html - 字体在本地有效,但在服务器上无效

css - SVG - 过滤器和 CSS 跨浏览器问题

html - 字体系列在 Chrome 中自动更改为 "undefined,sans-serif!important"