HTML 中的内联 SVG 对齐

发布于 2024-12-29 03:21:21 字数 726 浏览 3 评论 0原文

目前,我在边界 DIV 内正确对齐内联 SVG 时遇到问题,如本例所示。

<!DOCTYPE html>
<html>
<body>
    <div style="border: 1px solid black; height: 50px; width: 100px; vertical-align:top;">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"">
            <rect x="0" y="0" width="100" height="100" style="fill: red;" />
        </svg>
    </div>
</body>
</html>

SVG 调整大小以匹配 div 的尺寸,但我无法为 SVG 设置任何对齐方式。 text-align 和 Vertical-align 似乎都不适用于 Firefox 9、Chrome 18、IE 9 或 Opera 11.61 。我只想将内联 SVG 放置在 DIV 的左上角。

请注意,在我的例子中,周围的 DIV 具有动态尺寸(%值),因此使用绝对定位不起作用。

有人知道如何在这种情况下对齐 SVG 吗?

I currently have problems aligning inline SVG correctly inside a bounding DIV, like in this example.

<!DOCTYPE html>
<html>
<body>
    <div style="border: 1px solid black; height: 50px; width: 100px; vertical-align:top;">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"">
            <rect x="0" y="0" width="100" height="100" style="fill: red;" />
        </svg>
    </div>
</body>
</html>

The SVG gets resized to match the the dimensions of the div, but I am not able to set any alignment for the SVG. Neither text-align nor vertical-align seem to work in Firefox 9, Chrome 18, IE 9 or Opera 11.61 . I just want to position the inline SVG at the upper left corner of the DIV.

Note that in my case the surrounding DIV has dynamic dimmension (%-values), so using absolute positioning does not work.

Anybody have any clue how to align the SVG in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2025-01-05 03:21:21

添加 preserveAspectRatio="xMinYMin" 作为 svg 元素上的属性,例如 http://jsfiddle.net/longsonr/ fLWhu/

Add preserveAspectRatio="xMinYMin" as an attribute on the svg element e.g. http://jsfiddle.net/longsonr/fLWhu/

清引 2025-01-05 03:21:21

preserveAspectRatio="xMidYMid meet" 属性添加到 元素
根据 http://tutorials.jenkov.com/svg/svg-viewport -view-box.html

Add preserveAspectRatio="xMidYMid meet" attribute to <svg> element
as per http://tutorials.jenkov.com/svg/svg-viewport-view-box.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文