« SVG Attribute reference home
In some cases, typically when using the viewBox attribute, it is desirable that the graphics stretch to fit non-uniformly to take up the entire viewport. In other cases, it is desirable that uniform scaling be used for the purposes of preserving the aspect ratio of the graphics.
Attribute preserveAspectRatio indicates whether or not to force uniform scaling.
For all elements that support that attribute (see above), except for the <image> element, preserveAspectRatio only applies when a value has been provided for viewBox on the same element. For these elements, if attribute viewBox is not provided, then preserveAspectRatio is ignored.
For <image> elements, preserveAspectRatio indicates how referenced images should be fitted with respect to the reference rectangle and whether the aspect ratio of the referenced image should be preserved with respect to the current user coordinate system.
Usage context
| Categories | None |
|---|---|
| Value | [defer] <align> [<meetOrSlice>] |
| Animatable | Yes |
| Normative document | SVG 1.1 (2nd Edition) |
- defer
- If the value of
preserveAspectRatioon an<image>element starts withdeferthen the value of thepreserveAspectRatioattribute on the referenced content if present should be used. If the referenced content lacks a value forpreserveAspectRatiothen thepreserveAspectRatioattribute should be processed as normal (ignoringdefer). ForpreserveAspectRatioon all other elements thedeferportion of the attribute is ignored. - <align>
- The
<align>parameter indicates whether to force uniform scaling and, if so, the alignment method to use in case the aspect ratio of theviewBoxdoesn't match the aspect ratio of the viewport. The<align>parameter must be one of the following strings:- none
Do not force uniform scaling. Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle.
(Note: if<align>isnone, then the optional<meetOrSlice>value is ignored.) - xMinYMin - Force uniform scaling.
Align the<min-x>of the element'sviewBoxwith the smallest X value of the viewport.
Align the<min-y>of the element'sviewBoxwith the smallest Y value of the viewport. - xMidYMin - Force uniform scaling.
Align the midpoint X value of the element'sviewBoxwith the midpoint X value of the viewport.
Align the<min-y>of the element'sviewBoxwith the smallest Y value of the viewport. - xMaxYMin - Force uniform scaling.
Align the<min-x>+<width>of the element'sviewBoxwith the maximum X value of the viewport.
Align the<min-y>of the element'sviewBoxwith the smallest Y value of the viewport. - xMinYMid - Force uniform scaling.
Align the<min-x>of the element'sviewBoxwith the smallest X value of the viewport.
Align the midpoint Y value of the element'sviewBoxwith the midpoint Y value of the viewport. - xMidYMid (the default) - Force uniform scaling.
Align the midpoint X value of the element'sviewBoxwith the midpoint X value of the viewport.
Align the midpoint Y value of the element'sviewBoxwith the midpoint Y value of the viewport. - xMaxYMid - Force uniform scaling.
Align the<min-x>+<width>of the element'sviewBoxwith the maximum X value of the viewport.
Align the midpoint Y value of the element'sviewBoxwith the midpoint Y value of the viewport. - xMinYMax - Force uniform scaling.
Align the<min-x>of the element'sviewBoxwith the smallest X value of the viewport.
Align the<min-y>+<height>of the element'sviewBoxwith the maximum Y value of the viewport. - xMidYMax - Force uniform scaling.
Align the midpoint X value of the element'sviewBoxwith the midpoint X value of the viewport.
Align the<min-y>+<height>of the element'sviewBoxwith the maximum Y value of the viewport. - xMaxYMax - Force uniform scaling.
Align the<min-x>+<width>of the element'sviewBoxwith the maximum X value of the viewport.
Align the<min-y>+<height>of the element'sviewBoxwith the maximum Y value of the viewport.
- none
- <meetOrSlice>
- The
<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:- meet (the default) - Scale the graphic such that:
- aspect ratio is preserved
- the entire
viewBoxis visible within the viewport - the
viewBoxis scaled up as much as possible, while still meeting the other criteria
viewBox(i.e., the area into which theviewBoxwill draw will be smaller than the viewport). - slice - Scale the graphic such that:
- aspect ratio is preserved
- the entire viewport is covered by the
viewBox - the
viewBoxis scaled down as much as possible, while still meeting the other criteria
viewBoxdoes not match the viewport, some of theviewBoxwill extend beyond the bounds of the viewport (i.e., the area into which theviewBoxwill draw is larger than the viewport).
- meet (the default) - Scale the graphic such that:
Example
Elements
The following elements can use the preserveAspectRatio attribute