html - SVG 文本元素超出父宽度

标签 html css svg text overflow

因此,我将 SVG 元素的宽度设置为 100%,其中最后一个文本元素的宽度超过了作为其父元素的 SVG 元素的宽度。

我试过使用自动换行并为文本元素设置最大宽度,但无济于事。

基本上,我希望文本元素不会像当前那样脱离父 SVG 元素,并希望单词在到达该父元素宽度的边缘时换行。

此外,我不想只是将文本元素移到左侧,我有意将其放在原处。

<svg height="1000" width="100%">
        <circle cx="50%" cy="50" r=40 stroke="black" stroke-width="2" fill="white"/>
        <text x="49.8%" y="50" font-family="sans-serif" font-size="20px" text-anchor="middle" fill="black" dy=".38em">1939</text>
        <line x1="50%" y1="90" x2="50%" y2="950" style="stroke:black;stroke-width:2"/>
        <text id="first-life-block" x="50%" y="150" font-family="sans-serif" font-size="20px" fill="black" style="word-wrap: normal;">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</text>
</svg> 

这是调试器模式下的问题截图

enter image description here

这是我的 Codepen 调试器页面的链接:http://s.codepen.io/edsonmendieta/debug/jqjRry

非常感谢帮助:)

最佳答案

SVG 1.1 doesn't support wrapping of text.

You'd have to manually split it with JavaScript into separate elements or put the text in a <foreignObject> and let it wrap as HTML text.

来自Robert Longson


同时检查 this answer到“SVG 文本中的自动换行”和this JSFiddle使用(长)JavaScript 解决方法。

关于html - SVG 文本元素超出父宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429321/

相关文章:

可点击的 html 和 css3 菜单链接无法正常工作

javascript - 使用 D3 访问链接的 SVG 的元素 ID

jquery - 占位符在 IE9 中不起作用

html - 带有 div 的 Css 标题位于另一个 div 之上

html - 使用 CSS 为 ASP.NET MVC 中的每个 View 设置不同的背景颜色

css - 在响应式布局中设置元素高度?

javascript - 具有布局算法的 Canvas/SVG 基于 Web 的树形图库?

javascript - 在 D3 中创建嵌套后,如何将比例重置为新值?

javascript - 使用Javascript计算文本宽度而不进行四舍五入

javascript - Jquery index() 找不到我的元素