SVGSVGElement: preserveAspectRatio-Eigenschaft

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Die preserveAspectRatio schreibgeschützte Eigenschaft der SVGSVGElement-Schnittstelle spiegelt das Attribut preserveAspectRatio des gegebenen Elements wider. Sie definiert, wie der Inhalt des SVG-Elements skaliert werden soll, um in den gegebenen Raum zu passen, wobei das Seitenverhältnis beibehalten wird.

Wert

Beispiel

Bei folgendem SVG können wir die preserveAspectRatio-Eigenschaft verwenden, um das Skalierungsverhalten des SVG-Elements abzurufen:

html
<svg
  xmlns="http://www.w3.org/2000/svg"
  viewBox="0 0 200 200"
  preserveAspectRatio="xMidYMid meet">
  <circle cx="100" cy="100" r="50" fill="blue" />
</svg>

Wir können auf das preserveAspectRatio-Attribut zugreifen:

js
const svgElement = document.querySelector("svg");

console.log(svgElement.preserveAspectRatio.baseVal); // output: SVGPreserveAspectRatio {align: 1, meetOrSlice: 1}

Spezifikationen

Specification
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__preserveAspectRatio

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
preserveAspectRatio

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch