ChatGPT解决这个技术问题 Extra ChatGPT

How do I center an SVG in a div?

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?


l
l0b0

SVG is inline by default. Add display: block to it and then margin: auto will work as expected.


pretty sure this also means that text-align: center on the parent element will work too (it works for me in Chrome anyway)
this DID NOT work for me, but using 'inline-block' instead of 'block', that did work. Maybe it was because I inserted the SVG into a td, not a div
a
aknuds1

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


My current approach is flexbox. Just add: .container { display: flex; justify-content: center; } And add the .container class to the div which contains your svg.
Consider adding another answer with this new approach so I can upvote it. Thanks for your help!
D
David

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.


thanks for this. simple and effective. been going mad for hours messing with viewPort and viewBox. this did the trick in seconds. just wrap the stupid thing in a div and center it.
@anon58192932 — You are very welcome. It's stupidly simple, but satisfying that it's of use to others as I have benefited from so many more sophisticated answers to questions here.
R
Ryan

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%);

i don't know why, but i had to use left: 100%
S
Shishir Arora

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">

A
Always Helping

Flexbox is another approach: add

.container { display: flex; justify-content: center; }

And add the .container class to the div which contains your svg.


Unfortunately, Chrome does not like this. Safari loves this.
@SEoF Can you specify what Chrome does not like about this?
Chrome doesn't display the SVG at all if you flex it within a container. Conversely, Safari doesn't display the SVG if you try to set the height to 100% of a flexed area, unless you do it after the SVG has loaded. Basically, they are a right pain and both kick up a fuss at the solution for the other. I ended up having to add the style attributes after the SVG had loaded using JS (not a good fix).
Works like a charm on Chrome.
J
Joanna Schweiger

I had to use

display: inline-block;

A
Arihant Jain

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;
 }

M
Michael Rader

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.


M
Martin Lloyd Jose

You can also do this:


N
Nachiket Gohil

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.


E
Employee

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>

M
Marten

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;

C
Collin Krawll

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>

M
Mahdyar

HTML:

<div class="wrap-center">
    <svg width="20px" height="20px"></svg>
</div>

CSS:

.wrap-center {
    display: flex;
    justify-content: center;
    align-items: center;
}

C
Ching Ching

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%);

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.