首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG文本,tspan字体大小和居中

SVG文本,tspan字体大小和居中
EN

Stack Overflow用户
提问于 2019-10-15 15:00:05
回答 2查看 2.3K关注 0票数 0

我在SVG上有一个字体大小的问题,我们在svg中可以看到字体大小:16 on与段落字体大小不同,但是它们的值16px相同。我希望在svg这样的段落中有字体大小。

第二个问题是,当我删除<tspan text-anchor="middle" class="tc-label-value">85</tspan>时,如何将<tspan class="label-text" text-anchor="middle" x="0" y="50" dy="15">Text in SVG</tspan>集中--我的意思是,当我有Text in svg而没有https://codepen.io/palaniichukdmytro/pen/BaaKbze的时候

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-16 07:19:46

SVG文本,& span字体大小和中心解决方案

代码语言:javascript
复制
body {
  margin: 0;
}

.wr {
  width: 180px;
  margin: 0 auto;
}

.label-text {
  font-size: 12px;
  text-align: center;
}

.tc-label-value {
  font-size: 12px;
  text-align: center;
}

.par {
  font-size: 16px;
  text-align: center;
}
代码语言:javascript
复制
<div class='wr'>
  <svg viewBox="0 0 100 100" width="100%" height="100%" style="display: block;">
            <path d="M 95.5 50 A 45.5 45.5 0 1 1 11.315120324302569 26.047336589080288" stroke-width="9" stroke-dasharray="168.16760675098308" stroke-dashoffset="2.842170943040401e-14" stroke="#a3bfff" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
            <path d="M 11.315120324302569 26.047336589080288 A 45.5 45.5 0 0 1 95.5 49.999999999999986" stroke-width="9" stroke-dasharray="117.71732472568812" stroke-dashoffset="0" stroke="#66bb6a" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
            <text transform="translate(50)" x="0" y="50">
                <tspan text-anchor="middle" class="tc-label-value">85</tspan>
                <tspan class="label-text" text-anchor="middle" x="0" y="50" dy="15">Text in SVG</tspan>
            </text>
        </svg>
  <p class='par'>Text Paragraph</p>
</div>

票数 2
EN

Stack Overflow用户

发布于 2019-10-15 19:19:42

svg元素的字体大小应该是8.88px。为什么?因为svg的宽度是100个单位或px ( viewBox="0 100 100“),并且被缩放到180 px( .wr{width: 180px;} )。因为您需要svg内的字体大小看起来像16 be,所以真正的字体大小应该是16* 100 / 180 = 8.88

代码语言:javascript
复制
.wr {
  width: 180px; 
}
.par{
  font-size: 16px;
}

svg{font-size: 8.88px;}
代码语言:javascript
复制
<div class='wr'>
  <svg viewBox="0 0 100 100" style="display: block;">
    <path d="M 95.5 50 A 45.5 45.5 0 1 1 11.315120324302569 26.047336589080288" stroke-width="9" stroke-dasharray="168.16760675098308" stroke-dashoffset="2.842170943040401e-14" stroke="#a3bfff" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
      <path d="M 11.315120324302569 26.047336589080288 A 45.5 45.5 0 0 1 95.5 49.999999999999986" stroke-width="9" stroke-dasharray="117.71732472568812" stroke-dashoffset="0" stroke="#66bb6a" stroke-linecap="round" fill="none" style="transition: stroke-dashoffset 500ms ease-out 0s;"></path>
    <text transform="translate(50)" x="0" y="50">
      <tspan text-anchor="middle" class="tc-label-value">85</tspan>
      <tspan class="label-text" text-anchor="middle" x="0" y="50" dy="15">Text in SVG</tspan>       
    </text>
 </svg>
  <p class='par'>Text Paragraph</p>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58397618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档