I was stuck with some annoying white space in my SVG logo and tried adjusting the ViewBox attribute value, after a bit of playing around with it, I was able to fix the white space.
I understand that ViewBox is kind of a box where the inner SVG content is placed but what is the system behind it that decides how the inner SVG content is going to look?
viewBoxenables you to have a custom co-ordinate system within your defined SVG view port.Lets say, you have the following code:
<svg width="500" height="200" viewBox="0 0 50 20" > <rect x="20" y="10" width="10" height="5" style="stroke: #000000; fill:none;"/> </svg>You can see that the view port is defined to have a width of 500px (
pxis the default unit, when you don't specify any), and a height of 200px. But now, because I have specified aviewBoxattribute, this changes things.According to MDN:
In other words, going with the above example, the viewBox starts at 0, 0; and has a width of 50; and a height of 20. So an amount of 500px of view port width is occupied by an amount of 50 units of view box width; so that means each unit in the view box corresponds to an amount of (500px/50 OR 200px/20) 10px. So the above rectangle is going to have a width of (10 x 10px) 100px, and a height of (5 x 10px) 50px; and its starting co-ordinates (the top-most left corner of the rectangle) will be at (20 x 10px, 10 x 10px) -> (200px, 100px).
That's a short brief on
viewBox.In this example you can see the aspect ratios, for both the view box, and the view port, are the same: 500/200 == 50/20; in the cases when they are not, the attribute
preserveAspectRatiokicks in.I refer you to this article: tutorials.jenkov.com/svg/svg-viewport-view-box.ht… ... from where the above example is borrowed from; which goes on in great detail about
viewBox,preserveAspectRatio, and all the different use cases with the corresponding attributes.