
使用webfont: 使用在线字体服务(如google fonts)提供的webfont。
首先,在HTML文件中引入Webfont:
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@700" rel="stylesheet">
然后,在SVG的style标签中使用Webfont:
<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>
</svg>使用开源字体: 考虑使用开源字体,如SIL OFL v1.1许可的"D-Din",可以从font squirrel下载。
示例:嵌入字体 vs. 未嵌入字体
立即学习“前端免费学习笔记(深入)”;
以下示例展示了嵌入字体和未嵌入字体的SVG图片在显示上的差异。
SVG Font Embedding Example <link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@700" rel="stylesheet">Font embedded
@@##@@Font not embedded (fallback font is used)
@@##@@Inline SVG - using Webfont (Roboto)
Illustrator在导出SVG时,可能会将文本元素分割成多个
<text class="cls-1" transform="translate(133.05 117.67)">2018</text> <text class="cls-2" transform="translate(143.07 61.95)">1<tspan class="cls-3" x="19.99" y="0">S</tspan> <tspan x="39.83" y="0">T</tspan> </text><text class="cls-4" transform="translate(57.23 179.3)">SUMO CHAL<tspan class="cls-5" x="147.92" y="0">L</tspan> <tspan x="162.25" y="0">ANGE</tspan> </text>
解决方法:
简化SVG结构,将文本合并到一个
<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>
注意: 这种方法可能只适用于特定字体。
通过将文字转换为路径、嵌入字体或使用Webfont,可以有效地解决SVG图片中的字体显示问题。同时,简化SVG结构可以避免Illustrator导出SVG时可能出现的文字间距问题。在实际开发中,应根据具体情况选择合适的解决方案。
以上就是解决CSS中SVG图片字体显示问题的实用指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号