viewBox, & preserveAspectRatio
<svg> element using the width and height attributes.
viewBox attribute on the <svg> element.
<min-x> value of the viewBox.
viewBox, respectively.
<min-y> value.
viewBox, respectively.
When the min-x and min-y values change, the mid-x, max-x, mid-y, and max-y values change accordingly.
The “show x & y guidelines” option allows you to show and hide the above mentioned lines to better visualize the position of the viewBox with respect to the viewport.
Note that the grey dotted horizontal and vertical lines represent the horizontal and vertical midpoints of the viewport, used to align the viewBox when a xMid* or *YMid value is used.
preserveAspectRatio = defer? <align> <meetOrSlice>?
The align and meetOrSlice parameters work together in a way similar to how background-position and background-size work together. Read further for more information.
The <align> parameter indicates whether to force uniform scaling and, if so, the alignment method to use in case the aspect ratio of the viewBox doesn’t match the aspect ratio of the viewport. It can take one of the following values:
viewBox inside the viewport is—in a way—similar to the positioning of a background image inside a background positioning area when using percentage values to position the image.
In other words, the viewBox is stretched or shrunk as necssary so that it fills the entire viewport exactly, disregarding the aspect ratio. The graphic may be distorted.
(Note: if <align> is none, then the optional <meetOrSlice> value is ignored.)
<min-x> of the element’s viewBox with the smallest X value of the viewport.
<min-y> of the element’s viewBox with the smallest Y value of the viewport.
backrgound-position: 0% 0%;.
<min-x> of the element’s viewBox with the smallest X value of the viewport.
viewBox with the midpoint Y value of the viewport.
background-position: 0% 50%;.
<min-x> of the element’s viewBox with the smallest X value of the viewport.
<min-y>+<height> of the element’s viewBox with the maximum Y value of the viewport.
backrgound-position: 0% 100%;.
viewBox with the midpoint X value of the viewport.
<min-y> of the element’s viewBox with the smallest Y value of the viewport.
backrgound-position: 50% 0%;.
viewBox with the midpoint X value of the viewport.
viewBox with the midpoint Y value of the viewport.
backrgound-position: 50% 50%;.
viewBox with the midpoint X value of the viewport.
<min-y>+<height> of the element’s viewBox with the maximum Y value of the viewport.
backrgound-position: 50% 100%;.
<min-x>+<width> of the element’s viewBox with the maximum X value of the viewport.
<min-y> of the element’s viewBox with the smallest Y value of the viewport.
backrgound-position: 100% 0%;.
<min-x>+<width> of the element’s viewBox with the maximum X value of the viewport.
viewBox with the midpoint Y value of the viewport.
backrgound-position: 100% 50%;.
<min-x>+<width> of the element’s viewBox with the maximum X value of the viewport.
<min-y>+<height> of the element’s viewBox with the maximum Y value of the viewport.
backrgound-position: 100% 100%;.
<meetOrSlice> parameter is optional and, if provided, is separated from the <align> value by one or more spaces and then must be one of the following strings:
viewBox is visible within the viewport
In this case, if the aspect ratio of the graphic does not match the viewport, some of the viewport will extend beyond the bounds of the viewBox (i.e., the area into which the viewBox will draw will be smaller than the viewport).
You can think of this as being similar to backrgound-size: contain. The background image is scaled as much as possible while preserving its aspect ratio and making sure it fits entirely into the background painting area. If the aspect ratio of the background is not the same as that of the element it is being applied to, parts of the background painting area will not be covered by the background image.
viewBox covers the entire viewport area, while maintaining its aspect ratio. The viewBox is scaled just enough to cover the viewport area (in both dimensions), but it is not scaled any more than needed to achieve that. In other words, it is scaled to the smallest size such that the width and height of the viewBox can completely cover the viewport.
In this case, if the aspect ratio of the viewBox does not match the viewport, some of the viewBox will extend beyond the bounds of the viewport (i.e., the area into which the viewBox will draw is larger than the viewport).
You can think of this as being similar to background-size: cover. In the case of a background image, the image is scaled while preserving its intrinsic aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area.
preserveAspectRatio attribute has no effect if no viewBox is specified.
preserveAspectRatio values may have no visually different effect on the graphic.
viewBox has the same size as the viewport, the preserveAspectRatio value has no effect.