web api interface
SVGRectElement
The SVGRectElement interface provides access to the properties of <rect> elements, as well as methods to manipulate them.
Instance properties
This interface also inherits properties from its parent, SVGGeometryElement.
xRead only- : Returns an
SVGAnimatedLengthcorresponding to thexattribute of the given<rect>element.
- : Returns an
yRead only- : Returns an
SVGAnimatedLengthcorresponding to theyattribute of the given<rect>element.
- : Returns an
widthRead only- : Returns an
SVGAnimatedLengthcorresponding to thewidthattribute of the given<rect>element.
- : Returns an
heightRead only- : Returns an
SVGAnimatedLengthcorresponding to theheightattribute of the given<rect>element.
- : Returns an
rxRead only- : Returns an
SVGAnimatedLengthcorresponding to therxattribute of the given<rect>element.
- : Returns an
ryRead only- : Returns an
SVGAnimatedLengthcorresponding to theryattribute of the given<rect>element.
- : Returns an
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 an SVGRectElement to a random value whenever the user clicks it.
HTML
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect width="300" height="100" id="myrect" />
<text x="60" y="40" fill="white" font-size="40">Click Me</text>
</svg>
CSS
#myrect {
fill: blue;
stroke-width: 1;
stroke: black;
}
JavaScript
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
Interactive exampleOpen the canonical MDN source to run this embedded demo.
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.