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?
回答1:
SVG is inline by default. Add display: block to it and then margin: auto will work as expected.
回答2:
Above answers did not work for me. Adding the attribute preserveAspectRatio="xMidYMin" to the tag did the trick though. The viewBox attribute needs to be specified for this to work as well. Source: Mozilla developer network
回答3:
None of these answers worked for me. This is how I did it.
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.