I have an 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?
SVG is inline by default. Add display: block
to it and then margin: auto
will work as expected.
Above answers did not work for me. Adding the attribute preserveAspectRatio="xMidYMin"
to the <svg>
tag did the trick though. The viewBox
attribute needs to be specified for this to work as well. Source: Mozilla developer network
.container { display: flex; justify-content: center; }
And add the .container class to the div which contains your svg.
Having read above that svg is inline by default, I just added the following to the div:
<div style="text-align:center;">
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.
None of these answers worked for me. This is how I did it.
position: relative;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
left: 100%
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
viewBox: defines the rectangle area for svg to cover. preserveAspectRatio: defined where to place the viewBox wrt viewport and how to strech it if viewport changes.
Follow this link from codepen for detailed description
<svg viewBox="70 160 800 190" preserveAspectRatio="xMaxYMax meet">
Flexbox is another approach: add
.container { display: flex; justify-content: center; }
And add the .container
class to the div which contains your svg.
I had to use
display: inline-block;
Just treat the container as flex and center the svg item by flex properties:
<div classname='icon'>
<svg>.....</svg>
</div>
.icon{
display:flex;
align-items:center;
justify-content:center;
}
make sure your css reads:
margin: 0 auto;
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.
You can also do this:
Put these two lines in style.css
In your specified div
class.
display: block;
margin: auto;
and then try to run it, you will be able to see that svg
is now aligned in the center.
Put your code in between this div
if you are using bootstrap:
<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>
None of the above answers worked for me. If the SVG is from Google Icons try this:
display: inline-block;
width: 24px;
vertical-align: middle;
For me, the fix was to add margin: 0 auto;
onto the element containing the <svg>
.
Like this:
<div style="margin: 0 auto">
<svg ...</svg>
</div>
HTML:
<div class="wrap-center">
<svg width="20px" height="20px"></svg>
</div>
CSS:
.wrap-center {
display: flex;
justify-content: center;
align-items: center;
}
Thank you for this answer but just to add you can use px instead of %
position: relative;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
Success story sharing
td
, not adiv