
如果你的项目已经使用了 web font,可以直接在 svg 中引用它们。
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@700" rel="stylesheet">
<svg id="Achievement" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 185">
<defs>
<style>
text {
font-family: 'Roboto Condensed', sans-serif;
font-size: 32px;
font-weight: bold;
}
</style>
</defs>
<text x="50%" y="30%" text-anchor="middle" dominant-baseline="central">2022</text>
<text x="50%" y="90%" text-anchor="middle" dominant-baseline="central">AWARD</text>
</svg>Illustrator 等工具在导出 SVG 时,可能会将文本分割成多个 <text> 或 <tspan> 元素,特别是当文本包含自定义间距或字距调整时。这会导致字体显示问题。
尽量简化 SVG 结构,将文本组合成更少的元素,并使用 CSS 属性(如 text-anchor 和 dominant-baseline)来控制文本的对齐方式。
<text x="50%" y="30%" text-anchor="middle" dominant-baseline="central"> 2022<tspan x="50%" data-dx="50%" dy="15%">1ST</tspan> </text> <text x="50%" y="90%" text-anchor="middle" dominant-baseline="central"> AWARD </text>
以下示例展示了嵌入字体和未嵌入字体在 SVG 显示上的差异。
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@700" rel="stylesheet"> <p>Font embedded</p> <img src="https://svgshare.com/i/iW8.svg" class="sample-item"> <p>Font not embedded (fallback font is used)</p> <img src="https://svgshare.com/i/iY4.svg" class="sample-item">
解决 SVG 字体显示问题需要根据具体情况选择合适的方法。将文本转换为路径可以确保在任何环境下都能正确显示,但会牺牲文本的可编辑性。嵌入字体或使用 Web Font 可以保持文本的可编辑性,但会增加文件大小或依赖于外部资源。简化 SVG 结构可以提高 SVG 的可维护性和兼容性。通过综合考虑这些因素,可以有效地解决 SVG 字体显示问题,确保 SVG 图片在各种浏览器中都能呈现出最佳效果。
立即学习“前端免费学习笔记(深入)”;
以上就是解决 CSS 中 SVG 图片字体显示问题的实用指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号