html - SVG 中的换行 g 元素

标签 html css svg

这可能是一个相当简单的问题,但无法找到答案。我想在 svg 元素内换行 g 元素标签。

CSS

.container { position: reletive; width: 200px; background: #ccc; }
.container g { display: inline-block; display: inline; }

HTML

<div class="container">
  <svg>
        <g transform="translate(0, 0)">
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Canada</text>
        </g>
        <g transform="translate(100, 0)">
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Germany</text>
        </g>
        <g transform="translate(200, 0)">
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Norway</text>
        </g>
        <g transform="translate(300, 0)">
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Russia</text>
        </g>
        <g transform="translate(400, 0)">
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">USA</text>
        </g>
    </svg>
</div>

这是 JSFiddle Link

最佳答案

SVG 没有像 HTML 那样的布局模型,每个元素都绘制在前面的元素之上,并且不影响其他元素的定位。

您可以使用单独的 <svg />元素,以便 HTML 布局模型可以控制元素的定位/包装:

.container { width: 200px; background: #ccc; }
.container svg { width:100px; margin:0; display: inline-block; }
<div class="container">
  <svg>
        <g>
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Canada</text>
        </g>
    </svg><svg>
        <g>
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Germany</text>
        </g>
    </svg><svg>
        <g>
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Norway</text>
        </g>
    </svg><svg>
        <g>
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">Russia</text>
        </g>
    </svg><svg>
        <g>
            <rect width="18" height="18"></rect>
            <text x="24" y="9" dy=".35em">USA</text>
        </g>
    </svg>
</div>

关于html - SVG 中的换行 g 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32601725/

相关文章:

javascript - 使用 HTML 和 CSS 将图像放置到 Circle 中

html - CSS - 跨浏览器导航 Div 大小

javascript - 单击另一个按钮时如何在按钮上创建幻灯片效果

javascript - Facebook 的视频通话如何运作

javascript - 如何仅在用户开始向下滚动后才显示标题? [ react ]

css - HTML 表格动态高度

Javascript – 使用 Modernizr 媒体查询更改 SVG viewBox

css - 更改 img src SVG 颜色

html - Firefox 和 Chrome 不会解码 base64 图像或 pdf 文件

html - 如何使 HTML/CSS 中未排序列表之间的空间可见?