Revision 426835 of preserveAspectRatio

  • Revision slug: Web/SVG/Attribute/preserveAspectRatio
  • Revision title: preserveAspectRatio
  • Revision id: 426835
  • Created:
  • Creator: Jeremie
  • Is current revision? Yes
  • Comment 6 words removedSVG/Attribute/preserveAspectRatio Web/SVG/Attribute/preserveAspectRatio

Revision Content

« SVG Attribute reference home

In some cases, typically when using the {{ SVGAttr("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 {{ SVGElement("image") }} element, preserveAspectRatio only applies when a value has been provided for {{ SVGAttr("viewBox") }} on the same element. For these elements, if attribute {{ SVGAttr("viewBox") }} is not provided, then preserveAspectRatio is ignored.

For {{ SVGElement("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 preserveAspectRatio on an {{ SVGElement("image") }} element starts with defer then the value of the preserveAspectRatio attribute on the referenced content if present should be used. If the referenced content lacks a value for preserveAspectRatio then the preserveAspectRatio attribute should be processed as normal (ignoring defer). For preserveAspectRatio on all other elements the defer portion 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 the {{ SVGAttr("viewBox") }} doesn'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> is none, then the optional <meetOrSlice> value is ignored.)
  • xMinYMin - Force uniform scaling.
    Align the <min-x> of the element's {{ SVGAttr("viewBox") }} with the smallest X value of the viewport.
    Align the <min-y> of the element's {{ SVGAttr("viewBox") }} with the smallest Y value of the viewport.
  • xMidYMin - Force uniform scaling.
    Align the midpoint X value of the element's {{ SVGAttr("viewBox") }} with the midpoint X value of the viewport.
    Align the <min-y> of the element's {{ SVGAttr("viewBox") }} with the smallest Y value of the viewport.
  • xMaxYMin - Force uniform scaling.
    Align the <min-x>+<width> of the element's {{ SVGAttr("viewBox") }} with the maximum X value of the viewport.
    Align the <min-y> of the element's {{ SVGAttr("viewBox") }} with the smallest Y value of the viewport.
  • xMinYMid - Force uniform scaling.
    Align the <min-x> of the element's {{ SVGAttr("viewBox") }} with the smallest X value of the viewport.
    Align the midpoint Y value of the element's {{ SVGAttr("viewBox") }} with the midpoint Y value of the viewport.
  • xMidYMid (the default) - Force uniform scaling.
    Align the midpoint X value of the element's {{ SVGAttr("viewBox") }} with the midpoint X value of the viewport.
    Align the midpoint Y value of the element's {{ SVGAttr("viewBox") }} with the midpoint Y value of the viewport.
  • xMaxYMid - Force uniform scaling.
    Align the <min-x>+<width> of the element's {{ SVGAttr("viewBox") }} with the maximum X value of the viewport.
    Align the midpoint Y value of the element's {{ SVGAttr("viewBox") }} with the midpoint Y value of the viewport.
  • xMinYMax - Force uniform scaling.
    Align the <min-x> of the element's {{ SVGAttr("viewBox") }} with the smallest X value of the viewport.
    Align the <min-y>+<height> of the element's {{ SVGAttr("viewBox") }} with the maximum Y value of the viewport.
  • xMidYMax - Force uniform scaling.
    Align the midpoint X value of the element's {{ SVGAttr("viewBox") }} with the midpoint X value of the viewport.
    Align the <min-y>+<height> of the element's {{ SVGAttr("viewBox") }} with the maximum Y value of the viewport.
  • xMaxYMax - Force uniform scaling.
    Align the <min-x>+<width> of the element's {{ SVGAttr("viewBox") }} with the maximum X value of the viewport.
    Align the <min-y>+<height> of the element's {{ SVGAttr("viewBox") }} with the maximum Y value of the viewport.
<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 {{ SVGAttr("viewBox") }} is visible within the viewport
    • the {{ SVGAttr("viewBox") }} is scaled up as much as possible, while still meeting the other criteria
    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 {{ SVGAttr("viewBox") }} (i.e., the area into which the {{ SVGAttr("viewBox") }} will draw will be smaller than the viewport).
  • slice - Scale the graphic such that:
    • aspect ratio is preserved
    • the entire viewport is covered by the {{ SVGAttr("viewBox") }}
    • the {{ SVGAttr("viewBox") }} is scaled down as much as possible, while still meeting the other criteria
    In this case, if the aspect ratio of the {{ SVGAttr("viewBox") }} does not match the viewport, some of the {{ SVGAttr("viewBox") }} will extend beyond the bounds of the viewport (i.e., the area into which the {{ SVGAttr("viewBox") }} will draw is larger than the viewport).

Example

Elements

The following elements can use the preserveAspectRatio attribute

  • {{ SVGElement("svg") }}
  • {{ SVGElement("symbol") }}
  • {{ SVGElement("image") }}
  • {{ SVGElement("feImage") }}
  • {{ SVGElement("marker") }}
  • {{ SVGElement("pattern") }}
  • {{ SVGElement("view") }}

Revision Source

<p>« <a href="/en/SVG/Attribute" title="en/SVG/Attribute">SVG Attribute reference home</a></p>
<p>In some cases, typically when using the {{ SVGAttr("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.<br> <br> Attribute <code>preserveAspectRatio</code> indicates whether or not to force uniform scaling.<br> <br> For all elements that support that attribute (see above), except for the {{ SVGElement("image") }} element, <code>preserveAspectRatio</code> only applies when a value has been provided for {{ SVGAttr("viewBox") }} on the same element. For these elements, if attribute {{ SVGAttr("viewBox") }} is not provided, then <code>preserveAspectRatio</code> is ignored.<br> <br> For {{ SVGElement("image") }} elements, <code>preserveAspectRatio</code> 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.</p>
<h2 id="Usage_context">Usage context</h2>
<table class="standard-table"> <tbody> <tr> <th scope="row">Categories</th> <td>None</td> </tr> <tr> <th scope="row">Value</th> <td>[defer] &lt;align&gt; [&lt;meetOrSlice&gt;]</td> </tr> <tr> <th scope="row">Animatable</th> <td>Yes</td> </tr> <tr> <th scope="row">Normative document</th> <td><a class="external" href="http://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute" title="http://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute">SVG 1.1 (2nd Edition)</a></td> </tr> </tbody>
</table>
<dl> <dt>defer</dt> <dd>If the value of <code>preserveAspectRatio</code> on an {{ SVGElement("image") }} element starts with <code>defer</code> then the value of the <code>preserveAspectRatio</code> attribute on the referenced content if present should be used. If the referenced content lacks a value for <code>preserveAspectRatio</code> then the <code>preserveAspectRatio</code> attribute should be processed as normal (ignoring <code>defer</code>). For <code>preserveAspectRatio</code> on all other elements the <code>defer</code> portion of the attribute is ignored.</dd> <dt>&lt;align&gt;</dt> <dd>The <code>&lt;align&gt;</code> parameter indicates whether to force uniform scaling and, if so, the alignment method to use in case the aspect ratio of the {{ SVGAttr("viewBox") }} doesn't match the aspect ratio of the viewport. The <code>&lt;align&gt;</code> parameter must be one of the following strings: <ul> <li><strong>none</strong><br> 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.<br> (Note: if <code>&lt;align&gt;</code> is <code>none</code>, then the optional <code>&lt;meetOrSlice&gt;</code> value is ignored.)</li> <li><strong>xMinYMin</strong> - Force uniform scaling.<br> Align the <code>&lt;min-x&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the smallest X value of the viewport.<br> Align the <code>&lt;min-y&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the smallest Y value of the viewport.</li> <li><strong>xMidYMin</strong> - Force uniform scaling.<br> Align the midpoint X value of the element's {{ SVGAttr("viewBox") }} with the midpoint X value of the viewport.<br> Align the <code>&lt;min-y&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the smallest Y value of the viewport.</li> <li><strong>xMaxYMin</strong> - Force uniform scaling.<br> Align the <code>&lt;min-x&gt;+&lt;width&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the maximum X value of the viewport.<br> Align the <code>&lt;min-y&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the smallest Y value of the viewport.</li> <li><strong>xMinYMid</strong> - Force uniform scaling.<br> Align the <code>&lt;min-x&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the smallest X value of the viewport.<br> Align the midpoint Y value of the element's {{ SVGAttr("viewBox") }} with the midpoint Y value of the viewport.</li> <li><strong>xMidYMid</strong> (the default) - Force uniform scaling.<br> Align the midpoint X value of the element's {{ SVGAttr("viewBox") }} with the midpoint X value of the viewport.<br> Align the midpoint Y value of the element's {{ SVGAttr("viewBox") }} with the midpoint Y value of the viewport.</li> <li><strong>xMaxYMid</strong> - Force uniform scaling.<br> Align the <code>&lt;min-x&gt;+&lt;width&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the maximum X value of the viewport.<br> Align the midpoint Y value of the element's {{ SVGAttr("viewBox") }} with the midpoint Y value of the viewport.</li> <li><strong>xMinYMax</strong> - Force uniform scaling.<br> Align the <code>&lt;min-x&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the smallest X value of the viewport.<br> Align the <code>&lt;min-y&gt;+&lt;height&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the maximum Y value of the viewport.</li> <li><strong>xMidYMax</strong> - Force uniform scaling.<br> Align the midpoint X value of the element's {{ SVGAttr("viewBox") }} with the midpoint X value of the viewport.<br> Align the <code>&lt;min-y&gt;+&lt;height&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the maximum Y value of the viewport.</li> <li><strong>xMaxYMax</strong> - Force uniform scaling.<br> Align the <code>&lt;min-x&gt;+&lt;width&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the maximum X value of the viewport.<br> Align the <code>&lt;min-y&gt;+&lt;height&gt;</code> of the element's {{ SVGAttr("viewBox") }} with the maximum Y value of the viewport.</li> </ul> </dd> <dt>&lt;meetOrSlice&gt;</dt> <dd>The <code>&lt;meetOrSlice&gt;</code> parameter is optional and, if provided, is separated from the <code>&lt;align&gt;</code> value by one or more spaces and then must be one of the following strings: <ul> <li><strong>meet</strong> (the default) - Scale the graphic such that: <ul> <li>aspect ratio is preserved</li> <li>the entire {{ SVGAttr("viewBox") }} is visible within the viewport</li> <li>the {{ SVGAttr("viewBox") }} is scaled up as much as possible, while still meeting the other criteria</li> </ul> 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 {{ SVGAttr("viewBox") }} (i.e., the area into which the {{ SVGAttr("viewBox") }} will draw will be smaller than the viewport).</li> <li><strong>slice</strong> - Scale the graphic such that: <ul> <li>aspect ratio is preserved</li> <li>the entire viewport is covered by the {{ SVGAttr("viewBox") }}</li> <li>the {{ SVGAttr("viewBox") }} is scaled down as much as possible, while still meeting the other criteria</li> </ul> In this case, if the aspect ratio of the {{ SVGAttr("viewBox") }} does not match the viewport, some of the {{ SVGAttr("viewBox") }} will extend beyond the bounds of the viewport (i.e., the area into which the {{ SVGAttr("viewBox") }} will draw is larger than the viewport).</li> </ul> </dd>
</dl>
<h2 id="Example">Example</h2>
<h2 id="Elements">Elements</h2>
<p>The following elements can use the <code>preserveAspectRatio</code> attribute</p>
<ul> <li>{{ SVGElement("svg") }}</li> <li>{{ SVGElement("symbol") }}</li> <li>{{ SVGElement("image") }}</li> <li>{{ SVGElement("feImage") }}</li> <li>{{ SVGElement("marker") }}</li> <li>{{ SVGElement("pattern") }}</li> <li>{{ SVGElement("view") }}</li>
</ul>
Revert to this revision