このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

SVGSymbolElement: viewBox プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

viewBoxSVGSymbolElement インターフェイスの読み取り専用プロパティで、指定された <symbol> 要素の viewBox 属性を反映します。

SVGAnimatedRect オブジェクトです。

viewBox プロパティへアクセス

html
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <defs>
    <symbol id="exampleSymbol" viewBox="0 0 100 100">
      <circle cx="50" cy="50" r="50" fill="blue" />
    </symbol>
  </defs>
  <use href="#exampleSymbol" x="50" y="50" width="100" height="100" />
</svg>
js
const symbolElement = document.getElementById("exampleSymbol");

// viewBox プロパティへアクセス
const viewBox = symbolElement.viewBox.baseVal;

console.log(viewBox.x); // 出力結果: 0
console.log(viewBox.y); // 出力結果: 0
console.log(viewBox.width); // 出力結果: 100
console.log(viewBox.height); // 出力結果: 100

仕様書

Specification
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__viewBox

ブラウザーの互換性

関連情報