SVG 中心文本在圆圈中

新手上路,请多包涵

我正在尝试使用 svg 将文本居中。

文本的大小将是动态的。

谢谢阿维

笨蛋

我的代码:

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 500 500">

  <g id="UrTavla">
      <circle style="fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;" cx="250" cy="250" r="245">

      </circle>
      <text x="50%" y="50%" stroke="#51c5cf" stroke-width="2px" dy=".3em"> Look, I’m centered!Look, I’m centered! </text>
  </g>
</svg>

原文由 אVי 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 970
2 个回答

text-anchor="middle" 添加到 text 元素。

普朗克

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
viewBox="0 0 500 500">
  <g id="UrTavla">
    <circle style="fill:url(#toning);stroke:#010101;stroke-width:1.6871;stroke-miterlimit:10;" cx="250" cy="250" r="245">
    </circle>
    <text x="50%" y="50%" text-anchor="middle" stroke="#51c5cf" stroke-width="2px" dy=".3em">Look, I’m centered!Look, I’m centered!</text>
  </g>
</svg>

原文由 Weafs.py 发布,翻译遵循 CC BY-SA 4.0 许可协议

当您想绘制不以容器为中心的 circle 时,建议和接受的解决方案无效!

使用 x="50%" y="50%" on text 标签仅在 SVG 元素包含以视口为中心的圆时才有效。

如果要绘制 3 个圆,还必须更改 (x,y) 文本坐标,使它们等于 (cx,cy) 圆坐标,如下例所示

正如 random 所建议的,我添加了 alignment-baseline="middle" 代码片段中的第一个圆圈,所以你可以看到 Label 完美对齐(现在是文本)。

 <svg height="350" width="350">
    <circle cx="110" cy="110" r="100"
            stroke="red"
            stroke-width="3"
            fill="none"
            />
    <text x="110" y="110"
          text-anchor="middle"
          stroke="red"
          stroke-width="1px"
          alignment-baseline="middle"
          > Label
    </text>
    <circle cx="240" cy="110" r="100"
            stroke="blue"
            stroke-width="3"
            fill="none"
            />
    <text x="240" y="110"
          text-anchor="middle"
          stroke="blue"
          stroke-width="1px"
          > Ticket
    </text>
    <circle cx="170" cy="240" r="100"
            stroke="green"
            stroke-width="3"
            fill="none"
            />
    <text x="170" y="240"
          text-anchor="middle"
          stroke="green"
          stroke-width="1px"
          > Vecto
    </text>
</svg>

只是为了好玩,我用 3 个圆圈来选择每个部分。只需点击它!

     function setReadableCode()
        {
        var circLabel = document.getElementById('circLabel');
        var circTicket = document.getElementById('circTicket');
        var circVecto = document.getElementById('circVecto');
        var interLabelTicket = document.getElementById('interLabelTicket');
        var interTicketVecto = document.getElementById('interTicketVecto');
        var interVectoLabel = document.getElementById('interVectoLabel');
        var interLabelTicketVecto = document.getElementById('interLabelTicketVecto');
        }

    function clickCircle(sCircle, sInter2a, sInter2b, sInter3)
        {
        var circ = document.getElementById(sCircle);
        var inter2a = document.getElementById(sInter2a);
        var inter2b = document.getElementById(sInter2b);
        var inter3 = document.getElementById(sInter3);

        var sColor = '';

        if (circ.style.fill == '' || circ.style.fill == 'white')
            {
            sColor = 'yellow';
            }
        else
            {
            sColor = 'white';
            }

        circ.style.fill = sColor;
        inter2a.style.fill = sColor;
        inter2b.style.fill = sColor;
        inter3.style.fill = sColor;

        setReadableCode();
        }

                    function clickCircLabel() {
                        clickCircle('circLabel', 'interLabelTicket', 'interVectoLabel', 'interLabelTicketVecto');
                    }

                    function clickCircTicket() {
                        clickCircle('circTicket', 'interLabelTicket', 'interTicketVecto', 'interLabelTicketVecto');
                    }

                    function clickCircVecto() {
                        clickCircle('circVecto', 'interVectoLabel', 'interTicketVecto', 'interLabelTicketVecto');
                    }

                    function clickIntersection2(sInter2, sInter3) {
                        var inter2 = document.getElementById(sInter2);
                        var inter3 = document.getElementById(sInter3);
                        var sColor = '';

                        if (inter2.style.fill == '' || inter2.style.fill == 'white') {
                            sColor = 'yellow';
                        }
                        else {
                            sColor = 'white';
                        }

                        inter2.style.fill = sColor;
                        inter3.style.fill = sColor;

                        setReadableCode();
                    }

                    function clickInterLabelTicket() {
                        clickIntersection2('interLabelTicket', 'interLabelTicketVecto');
                    }

                    function clickInterTicketVecto() {
                        clickIntersection2('interTicketVecto', 'interLabelTicketVecto');
                    }

                    function clickInterVectoLabel() {
                        clickIntersection2('interVectoLabel', 'interLabelTicketVecto');
                    }

                    function clickInterLabelTicketVecto() {
                        var inter = document.getElementById('interLabelTicketVecto');
                        var sColor = '';

                        if (inter.style.fill == '' || inter.style.fill == 'white') {
                            sColor = 'yellow';
                        }
                        else {
                            sColor = 'white';
                        }

                        inter.style.fill = sColor;
                        setReadableCode();
                    }
 text
    {
    font-family:Arial;
    }
 <svg height="350" width="350">
<circle id="circLabel" cx="110" cy="110" r="100" stroke="red" stroke-width="0" fill="white" onclick="clickCircLabel();"/>
<text x="60" y="110" text-anchor="middle" stroke="red" stroke-width="1px" onclick="clickCircLabel();">Label</text>
<circle id="circTicket" cx="210" cy="110" r="100" stroke="blue" stroke-width="0" fill="yellow" onclick="clickCircTicket();"/>
<text x="260" y="110" text-anchor="middle" stroke="blue" stroke-width="1px" onclick="clickCircTicket();">Ticket</text>
<circle id="circVecto" cx="160" cy="196.602541" r="100" stroke="green" stroke-width="0" fill="white" onclick="clickCircVecto();" />
<text x="160" y="240" text-anchor="middle" stroke="green" stroke-width="1px" onclick="clickCircVecto();">Vecto</text>
<path id="interLabelTicket"
      d="M 160 23.397460 a100,100 0 0,0 0,173.205081 a100,100 0 0,0 0,-173.205081 z"
      fill="white"
      stroke-width="3"
      onclick="clickInterLabelTicket();"
      />

<path id="interVectoLabel"
      d="M 60 196.602541 a100,100 0 0,0 150,-86.602540 a100,100 0 0,0 -150,86.602540 z"
      fill="white"
      stroke-width="3"
      onclick="clickInterVectoLabel();"
      />

<path id="interTicketVecto"
      d="M 260 196.602541 a100,100 0 0,0 -150,-86.602540 a100,100 0 0,0 150,86.602540 z"
      fill="white"
      stroke-width="3"
      onclick="clickInterTicketVecto();"
      />

<path id="interLabelTicketVecto"
      d="M 110 110 a100,100 0 0,1 100,0 a100,100 0 0,1 -50,86.602540 a100,100 0 0,1 -50,-86.602540 z"
      fill="none"
      stroke-width="3"
      onclick="clickInterLabelTicketVecto();"
      />

<circle cx="110" cy="110" r="100" stroke="red" stroke-width="3" fill="none" />
<circle cx="210" cy="110" r="100" stroke="blue" stroke-width="3" fill="none" />
<circle cx="160" cy="196.602541" r="100" stroke="green" stroke-width="3" fill="none"/>

</svg>

原文由 schlebe 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏