How do I center an SVG in a div?

Don P picture Don P · Dec 26, 2011 · Viewed 275k times · Source

I have an SVG that I am trying to center in a div. The div has a width or 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).

Anyone know my error?

Answer

Spadar Shut picture Spadar Shut · Dec 27, 2011

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