简体   繁体   English

如何在SVG中保持恒定的字体大小

[英]How to keep a constant font-size in svg

I'm new with SVG and I try to add some text in a shape that I created. 我是SVG的新手,我尝试以我创建的形状添加一些文本。 I've done it like this : https://fiddle.jshell.net/30kL3mzt/ But the text in the SVG change with the polygone. 我这样做是这样的: https : //fiddle.jshell.net/30kL3mzt/但是SVG中的文本随多边形而变化。 Is it possible to keep a constant size of text? 是否可以保持恒定的文本大小? Or at least to control the size of letters? 还是至少要控制字母的大小?

<div id="tab">
    <div class='chevron'>
        <svg width="25%" height='100%' viewBox="0 0 20 20" preserveAspectRatio="none">
        <polygon fill=steelblue stroke-width=0 
                     points="0,0 2,10 0,20 18,20 20,10 18,0" />
        <text x="0" y="10" font-family="Verdana" font-size="2" >I love SVG!</text>
        </svg>
    </div>
</div>
<div id="middle">
            <svg width="25%" height='100%' viewBox="0 0 20 20" preserveAspectRatio="none">
        <polygon fill=steelblue stroke-width=0 
                     points="0,0 2,10 0,20 18,20 20,10 18,0" />
        <text x="0" y="10" font-family="Verdana" font-size="2" >I love SVG!</text>
        </svg>
</div>

Why do you need the textual content to be in the SVG if you don't want it to scale? 如果您不希望缩放文本内容,为什么需要将文本内容包含在SVG中? Probably it's better to have the text outside and style it with CSS to be where you want it. 最好在外面放置文本并使用CSS设置样式以使其位于所需位置。

Instead of adding svg directly inside your HTML, why not use a font library like fontello/icomoon? 与其直接在HTML内部添加svg,为什么不使用fontello / icomoon之类的字体库?

I know for a fact, when you choose the same grid size for your icons you can scale up your icons separately from your text. 我知道一个事实,当您为图标选择相同的网格大小时 ,可以与文本分开放大图标。 This is because an icon from a font file uses CSS on the pseudo-elements :before and/or :after . 这是因为来自字体文件的图标在伪元素:before和/或:after上使用CSS。 Combined with line-height you have complete control on the size of an icon. 结合line-height您可以完全控制图标的大小。 => Line-height is best calculated by the height of the layer divided by the font-size . =>线高最好用图层的height除以font-size

For example: 24px div / 20px text = 1.2 例如:24px div / 20px text = 1.2

In my opinion you're too far off from an x-browser solution to your problem. 在我看来,您与问题的X浏览器解决方案相距太远。

  • convert your shapes into font library (import svg files) 将您的形状转换为字体库(导入svg文件)
  • add the proper CSS from the download (or read the readme.txt) 从下载中添加正确的CSS(或阅读readme.txt)
  • make sure to support .woff on the server 确保在服务器上支持.woff
  • use font icon classes where needed 在需要的地方使用字体图标类
  • override the proper font-sizes to your need in CSS (there's your solution) 根据CSS的需要覆盖适当的字体大小(这是您的解决方案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM