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?
make sure your css reads:
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.
I had to use
Above answers did not work for me. Adding the attribute
preserveAspectRatio="xMidYMin"
to the<svg>
tag did the trick though. TheviewBox
attribute needs to be specified for this to work as well. Source: Mozilla developer networkHaving read above that svg is inline by default, I just added the following to the div:
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.
SVG is inline by default. Add
display: block
to it and thenmargin: auto
will work as expected.Put your code in between this
div
if you are using bootstrap: