The SVGRect represents a rectangle. Rectangles consist of an x and y coordinate pair identifying a minimum x value, a minimum y value, and a width and height, which are constrained to be non-negative.

An SVGRect object can be designated as read only, which means that attempts to modify the object will result in an exception being thrown.

Properties

This interface also inherits properties from its parent, SVGGeometryElement.

SVGRect.x Read only
The exact effect of this coordinate depends on each element. If the attribute is not specified, the effect is as if a value of 0 were specified.
SVGRect.y Read only
The exact effect of this coordinate depends on each element.If the attribute is not specified, the effect is as if a value of 0 were specified.
SVGRect.width Read only
This represents the width of the rectangle.A value that is negative results to an error. A value of zero disables rendering of the element
SVGRect.height Read only
This represents the height of the rectangle.A value that is negative results to an error.A value of zero disables rendering of the element.

Exceptions on setting: A DOMException with the code NO_MODIFICATION_ALLOWED_ERR is raised on an attempt to change the value of a read-only attribute.

Methods

This interface also inherits properties from its parent, SVGGeometryElement.

Example

Here is a simple usage of rect interface. (Changing the color of the rect interface on every click)

SVG content

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <rect width="300" height="100" id="myrect" onclick="doRectClick()"
      style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" />
  <text x="60" y="40" fill="white" font-size="40"
      onclick="doRectClick();">Click Me</text>
</svg>

JavaScript content

function doRectClick(){
  var myrect = document.getElementById('myrect');
  var r = Math.floor(Math.random() * 255);
  var g = Math.floor(Math.random() * 255);
  var b = Math.floor(Math.random() * 255);
  myrect.style.fill = 'rgb(' + r + ', ' + g + ' , ' + b + ')';
}

Click the rect.

Specifications

Specification Status Comment
Scalable Vector Graphics (SVG) 2
The definition of 'SVGRectElement' in that specification.
Candidate Recommendation Changed the inheritance from SVGElement to SVGGeometryElement and removed the implemented interfaces SVGTests, SVGLangSpace, SVGExternalResourcesRequired, SVGStylable, and SVGTransformable.
Scalable Vector Graphics (SVG) 1.1 (Second Edition)
The definition of 'SVGRectElement' in that specification.
Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 1Edge Full support YesFirefox Full support 1.5IE Full support 9Opera Full support 8Safari Full support 3.1WebView Android Full support 1Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 8Safari iOS Full support 3.1Samsung Internet Android ?
heightChrome Full support 1Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android Full support 1Chrome Android Full support 18Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
widthChrome Full support 1Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android Full support 1Chrome Android Full support 18Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
xChrome Full support 1Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android Full support 1Chrome Android Full support 18Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
yChrome Full support 1Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android Full support 1Chrome Android Full support 18Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

Document Tags and Contributors

Contributors to this page: fscholz, Sebastianz, mattwojo, essymo, kscarfone, Sheppy, Jeremie
Last updated by: fscholz,