我有一个SVG,我试图在一个div居中。div的宽度为900px。SVG的宽度为400px。SVG的左边距和右边距设置为auto。不起作用,它就像左距为0一样(默认值)。
有人知道误差是多少吗?
我有一个SVG,我试图在一个div居中。div的宽度为900px。SVG的宽度为400px。SVG的左边距和右边距设置为auto。不起作用,它就像左距为0一样(默认值)。
有人知道误差是多少吗?
当前回答
只需将容器视为flex,并将svg项以flex属性居中即可:
<div classname='icon'>
<svg>.....</svg>
</div>
.icon{
display:flex;
align-items:center;
justify-content:center;
}
其他回答
HTML:
<div class="wrap-center">
<svg width="20px" height="20px"></svg>
</div>
CSS:
.wrap-center {
display: flex;
justify-content: center;
align-items: center;
}
如果你使用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>
谢谢你的回答,但只是补充你可以使用px而不是%
position: relative;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
确保你的CSS如下:
margin: 0 auto;
即使您说您已经将左和右设置为auto,您可能会放置一个错误。当然我们不知道,因为你没有给我们看任何代码。
上面的答案对我都没用。如果SVG来自谷歌图标,请尝试以下方法:
display: inline-block;
width: 24px;
vertical-align: middle;