我有一个SVG,我试图在一个div居中。div的宽度为900px。SVG的宽度为400px。SVG的左边距和右边距设置为auto。不起作用,它就像左距为0一样(默认值)。
有人知道误差是多少吗?
我有一个SVG,我试图在一个div居中。div的宽度为900px。SVG的宽度为400px。SVG的左边距和右边距设置为auto。不起作用,它就像左距为0一样(默认值)。
有人知道误差是多少吗?
当前回答
谢谢你的回答,但只是补充你可以使用px而不是%
position: relative;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
其他回答
我必须使用
display: inline-block;
HTML:
<div class="wrap-center">
<svg width="20px" height="20px"></svg>
</div>
CSS:
.wrap-center {
display: flex;
justify-content: center;
align-items: center;
}
对我来说,修正是添加边际:0 auto;到包含<svg>. xml的元素上。
是这样的:
<div style="margin: 0 auto">
<svg ...</svg>
</div>
这些答案对我都没用。我是这么做的。
position: relative;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
如果你使用bootstrap,把你的代码放在这个div之间:
<div class="text-center ">
<i class="fa fa-twitter" style="font-size:36px "></i>
<i class="fa fa-pinterest" style="font-size:36px"></i>
<i class="fa fa-dribbble" style="font-size:36px"></i>
<i class="fa fa-instagram" style="font-size:36px"></i>
</div>