Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. SVGRectElement

SVGRectElement

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheSVGRectElement interface provides access to the properties of<rect> elements, as well as methods to manipulate them.

EventTarget Node Element SVGElement SVGGraphicsElement SVGGeometryElement SVGRectElement

Instance properties

This interface also inherits properties from its parent,SVGGeometryElement.

SVGRectElement.xRead only

Returns anSVGAnimatedLength corresponding to thex attribute of the given<rect> element.

SVGRectElement.yRead only

Returns anSVGAnimatedLength corresponding to they attribute of the given<rect> element.

SVGRectElement.widthRead only

Returns anSVGAnimatedLength corresponding to thewidth attribute of the given<rect> element.

SVGRectElement.heightRead only

Returns anSVGAnimatedLength corresponding to theheight attribute of the given<rect> element.

SVGRectElement.rxRead only

Returns anSVGAnimatedLength corresponding to therx attribute of the given<rect> element.

SVGRectElement.ryRead only

Returns anSVGAnimatedLength corresponding to thery attribute of the given<rect> element.

Instance methods

This interface doesn't implement any specific methods, but inherits methods from its parent,SVGGeometryElement.

Examples

Changing the color of an SVG rectangle

This example sets the fill color of anSVGRectElement to a random value whenever the user clicks it.

HTML

html
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">  <rect width="300" height="100" />  <text x="60" y="40" fill="white" font-size="40">Click Me</text></svg>

CSS

css
#myrect {  fill: blue;  stroke-width: 1;  stroke: black;}

JavaScript

js
const myRect = document.querySelector("#myrect");myRect.addEventListener("click", () => {  const r = Math.floor(Math.random() * 255);  const g = Math.floor(Math.random() * 255);  const b = Math.floor(Math.random() * 255);  myRect.style.fill = `rgb(${r} ${g} ${b})`;});

Result

Specifications

Specification
Scalable Vector Graphics (SVG) 2
# InterfaceSVGRectElement

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp