如何将 SVG 置于 div 中心?
我有一个 SVG,我试图将其放在 div 的中心。 div 的宽度为 900px。 SVG 的宽度为 400 像素。 SVG 将其 margin-left 和 margin-right 设置为 auto。不起作用,它只是表现得好像左边距为 0(默认)。
有谁知道有什么错误吗?
I have a SVG that I am trying to center in a div. The div has a width of 900px. The SVG has a width of 400px. The SVG has its margin-left and margin-right set to auto. Doesn't work, it just acts as if the left margin is 0 (default).
Does Anyone know what's the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
SVG 默认情况下是内联的。
添加
display: block
到它,然后margin: auto
将按预期工作。或者,根据您的布局,您可能希望保持 SVG 内联并在父元素上设置
text-align: center
。作为另一种选择,您可以在父元素上使用 Flex 或网格布局将 SVG 居中。
SVG is inline by default.
Add
display: block
to it and thenmargin: auto
will work as expected.Or depending on your layout you may want to keep SVG inline and set
text-align: center
on a parent element instead.As another alternative, you can center SVG using flex or grid layouts on the parent element.
以上答案对我不起作用。
不过,将属性
preserveAspectRatio="xMidYMin"
添加到标记就可以解决问题。需要指定
viewBox
属性才能使其正常工作。来源:Mozilla 开发者网络
Above answers did not work for me.
Adding the attribute
preserveAspectRatio="xMidYMin"
to the<svg>
tag did the trick though. TheviewBox
attribute needs to be specified for this to work as well.Source: Mozilla developer network
读完上面的内容后,svg 默认情况下是内联的,我只是将以下内容添加到 div: 中
,它对我来说很有效。
纯粹主义者可能不喜欢它(它是图像,而不是文本),但在我看来,HTML 和 CSS 在居中方面搞砸了,所以我认为这是合理的。
Having read above that svg is inline by default, I just added the following to the div:
and it did the trick for me.
Purists may not like it (it’s an image, not text) but in my opinion HTML and CSS screwed up over centring, so I think it’s justified.
Flexbox 是另一种方法:添加
并将
.container
类添加到包含 svg 的 div 中。Flexbox is another approach: add
And add the
.container
class to the div which contains your svg.这些答案都不适合我。我就是这样做的。
None of these answers worked for me. This is how I did it.
上面的答案看起来不完整,因为它们仅从 CSS 的角度进行讨论。
svg 在视口中的定位受两个 svg 属性的影响
点击 codepen 中的此链接了解详细说明
Answers above look incomplete as they are talking from css point of view only.
positioning of svg within viewport is affected by two svg attributes
Follow this link from codepen for detailed description
我不得不使用
I had to use
只需将容器视为 Flex 并通过 Flex 属性将 svg 项目居中:
Just treat the container as flex and center the svg item by flex properties:
确保你的CSS显示为:
即使你说你将左侧和右侧设置为自动,你也可能会出错。当然我们不知道,因为您没有向我们展示任何代码。
make sure your css reads:
Even though you're saying you have the left and right set to auto, you may be placing an error. Of course we wouldn't know though because you did not show us any code.
将这两行放入
style.css
在您指定的
div
类中。然后尝试运行它,您将能够看到
svg
现在已居中对齐。Put these two lines in
style.css
In your specified
div
class.and then try to run it, you will be able to see that
svg
is now aligned in the center.您还可以这样做:
You can also do this:
HTML:
CSS:
HTML:
CSS:
对我来说,解决方法是将
margin: 0 auto;
添加到包含的元素上。
像这样:
For me, the fix was to add
margin: 0 auto;
onto the element containing the<svg>
.Like this:
上述答案都不适合我。如果 SVG 来自 Google Icons,请尝试以下操作:
None of the above answers worked for me. If the SVG is from Google Icons try this:
如果您使用 bootstrap,请将代码放在此
div
之间:Put your code in between this
div
if you are using bootstrap:感谢您的回答,但只是补充一下,您可以使用 px 而不是 %
Thank you for this answer but just to add you can use px instead of %
此代码将 SVG 中心放在 div 元素中。
This code center SVG in div element.