html - 不能在 svg 的中心制作 g

标签 html css svg

你好 Stackoverflow 社区。我需要帮助,我正在尝试在 svg 标签的中心制作 g 标签,但我做不到。我试过让 margin 0 auto 但还是一样。我该如何解决这个问题,这是我的带有完整代码的 jsfiddle:https://jsfiddle.net/mky4qqvd/

<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600px" height="100px" viewBox="0 0 600 100">
                <style type="text/css">

                        text {
                            filter: url(#filter);
                            fill: black;
                            font-family: "Cinzel",serif;
                            font-size: 100px;
                            -webkit-font-smoothing: antialiased;
                            -moz-osx-font-smoothing: grayscale;
                        }

                </style>
                <g style="
    /* width: 100%; */
    width: auto;
">
                    <text x="0" y="100">Swinger</text>
                </g>
                </svg>

请帮我解决这个问题,以便我以后知道。

最佳答案

SVG 不是这样工作的。 SVG 没有像 HTML 那样的自动布局。将它想象成一个绘图程序 - 您必须告诉它在哪里绘制所有内容。

width: automargin 等 HTML 样式属性也不适用于 SVG。

SVG 确实能够在 SVG 中的特定位置水平居中文本。您也可以(某种程度上)垂直居中。

<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600px" height="100px" viewBox="0 0 600 100">
    <style type="text/css">

        text {
            fill: black;
            font-family: "Cinzel",serif;
            font-size: 100px;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
            text-anchor: middle;
            alignment-baseline: middle;
        }

    </style>
    <g>
        <text x="300" y="50">Swinger</text>
    </g>
</svg>

关于html - 不能在 svg 的中心制作 g,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284096/

相关文章:

javascript - 单击刷新/重新加载/重新启动 SVG

javascript - 如何将 png 背景图像和填充颜色添加到 SVG?

javascript - Javascript undefined variable ,基本html形式

javascript - 单击 td 内的 img 时更改 tr 背景颜色

javascript - PHP hint.css 添加新的文本行

html - 当我向其中添加元素时,div 的高度如何增长

css - 修复 IE7 div overflow-y : scroll from hiding contents behind the scrollbar

jquery - 通过使用 jQuery 动态设置 CSS 值使图像在 div 中居中

html - 尝试通过 css 设置背景图片,但未显示

javascript - SVG 过滤器性能问题