preserveAspectRatio=<alignment> [meet | slice]
For ‘image’ elements, preserveAspectRatio indicates how referenced images should be fitted concerning the reference rectangle and whether the aspect ratio of the referenced image should be preserved concerning the current user coordinate system.
The following are the methods to specify alignment for Preserve Aspect Ratio in SVG.
Table of Contents
1. Using <align> parameter
Align the minimum x of the viewBox with the left corner of the viewport and align the minimum y of the viewBox with the top edge of the viewport.
xMidYMin
Align the midpoint x value of the viewBox with the midpoint x value of the viewport and align the minimum y of the viewBox with the top edge of the viewport.
xMaxYMin
Align the maximum x value of the viewBox with the right corner of the viewport and align the minimum y value of the viewBox with the top edge of the viewport.
xMinYMid
Align the minimum x of the viewBox with the left corner of the viewport and align the midpoint y value of the viewBox with the midpoint y value of the viewport.
xMidYMid
Align the midpoint x value of the viewBox with the midpoint x value of the viewport, and align the midpoint y value of the viewBox with the midpoint y value of the viewport.
xMaxYMid
Align the maximum x value of the viewBox with the right corner of the viewport and align the midpoint y value of the viewBox with the midpoint y value of the viewport.
xMinYMax
Align the minimum x of the viewBox with the left corner of the viewport. Align the maximum y value of the viewBox with the bottom edge of the viewport.
xMidYMax
Align the midpoint x value of the viewBox with the midpoint x value of the viewport and align the maximum y value of the viewBox with the bottom edge of the viewport.
xMaxYMax
Align the maximum x value of the viewBox with the right corner of the viewport and align the maximum y value of the viewBox with the bottom edge of the viewport.
2. Using the meet Specifier
The following is an example to show the effects of using the meet specifier along with the align parameter for tall and wide viewports.
<!-- tall viewports --> <svg viewBox="0 0 200 200" width="300" height="200"> <g transform="translate(0,10)"> <text transform="translate(-10,0)">xMinYMin</text> <rect width="50" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 90 90" width="50" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg> </g> <g transform="translate(80,10)"> <text transform="translate(-11,0)">xMidYMid</text> <rect width="50" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 90 90" width="50" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> <g transform="translate(160,10)"> <text transform="translate(-12,0)">xMaxYMax</text> <rect width="50" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="xMaxYMax meet" viewBox="0 0 90 90" width="50" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> </svg> <!-- wide viewports --> <svg viewBox="0 0 200 200" width="200" height="200"> <g transform="translate(0,10)"> <rect width="200" height="50" stroke="blue" fill="none"></rect> <text transform="translate(60,0)">xMinYMin</text> <svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 90 90" width="200" height="50"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg> </g> <g transform="translate(0,75)"> <rect width="200" height="50" stroke="blue" fill="none"></rect> <text transform="translate(60,0)">xMidYMid</text> <svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 90 90" width="200" height="50"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> <g transform="translate(0,140)"> <rect width="200" height="50" stroke="blue" fill="none"></rect> <text transform="translate(60,0)">xMaxYMax</text> <svg preserveAspectRatio="xMaxYMax meet" viewBox="0 0 90 90" width="200" height="50"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> </svg>
3. Using the slice Specifier
<!-- Tall viewports --> <svg viewBox="0 0 200 200" width="300" height="200"> <g transform="translate(0,10)"> <text transform="translate(-10,0)">xMinYMin</text> <rect width="50" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="xMinYMin slice" viewBox="0 0 90 90" width="50" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg> </g> <g transform="translate(70,10)"> <text transform="translate(-11,0)">xMidYMid</text> <rect width="50" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="xMidYMid slice" viewBox="0 0 90 90" width="50" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> <g transform="translate(140,10)"> <text transform="translate(-12,0)">xMaxYMax</text> <rect width="50" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="xMaxYMax slice" viewBox="0 0 90 90" width="50" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> </svg> <!--Wide Viewports--> <svg viewBox="0 0 200 200" width="200" height="200"> <g transform="translate(0,10)"> <rect width="200" height="50" stroke="blue" fill="none"></rect> <text transform="translate(60,0)">xMinYMin</text> <svg preserveAspectRatio="xMinYMin slice" viewBox="0 0 90 90" width="200" height="50"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg> </g> <g transform="translate(0,75)"> <rect width="200" height="50" stroke="blue" fill="none"></rect> <text transform="translate(60,0)">xMidYMid</text> <svg preserveAspectRatio="xMidYMid slice" viewBox="0 0 90 90" width="200" height="50"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45"/> </svg></g> <g transform="translate(0,140)"> <rect width="200" height="50" stroke="blue" fill="none"></rect> <text transform="translate(60,0)">xMaxYMax</text> <svg preserveAspectRatio="xMaxYMax slice" viewBox="0 0 90 90" width="200" height="50"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45" /> </svg></g> </svg>
4. Using the none Specifier
<!--Tall Viewport--> <svg viewBox="0 0 100 200" width="120" height="200"> <g transform="translate(20,15)"> <text transform="translate(-5,0)">Tall Viewport</text> <rect width="80" height="180" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="none" viewBox="0 0 90 90" width="80" height="180"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45" /> </svg> </g> </svg> <!--Wide Viewport--> <svg viewBox="0 0 200 100" width="200" height="100"> <g transform="translate(0,15)"> <text transform="translate(30,0)">Wide Viewport</text> <rect width="180" height="80" stroke="blue" fill="none"></rect> <svg preserveAspectRatio="none" viewBox="0 0 90 90" width="180" height="80"> <polygon fill="red" stroke="brown" points="45,2 2,45 45,88 88,45" /> </svg> </g> </svg>