Back to Content

Index

files/en-us/web/svg/reference/element/rect/index.md

latest3.2 KB
Original Source

The <rect> SVG element is a basic SVG shape that draws rectangles, defined by their position, width, and height. The rectangles may have their corners rounded.

Usage context

{{svginfo}}

Attributes

  • {{SVGAttr("x")}}
    • : The x coordinate of the rect. Value type: <length> | <percentage>; Default value: 0; Animatable: yes
  • {{SVGAttr("y")}}
    • : The y coordinate of the rect. Value type: <length> | <percentage>; Default value: 0; Animatable: yes
  • {{SVGAttr("width")}}
    • : The width of the rect. Value type: auto | <length> | <percentage>; Default value: auto; Animatable: yes
  • {{SVGAttr("height")}}
    • : The height of the rect. Value type: auto | <length> | <percentage>; Default value: auto; Animatable: yes
  • {{SVGAttr("rx")}}
    • : The horizontal corner radius of the rect. Defaults to ry if it is specified. Value type: auto | <length> | <percentage>; Default value: auto; Animatable: yes
  • {{SVGAttr("ry")}}
    • : The vertical corner radius of the rect. Defaults to rx if it is specified. Value type: auto | <length> | <percentage>; Default value: auto; Animatable: yes
  • {{SVGAttr("pathLength")}}
    • : The total length of the rectangle's perimeter, in user units. Value type: <number>; Default value: none; Animatable: yes

[!NOTE] Starting with SVG2, x, y, width, height, rx and ry are Geometry Properties, meaning those attributes can also be used as CSS properties for that element.

DOM Interface

This element implements the {{domxref("SVGRectElement")}} interface.

Example

css
html,
body,
svg {
  height: 100%;
}
html
<svg viewBox="0 0 220 100" xmlns="http://www.w3.org/2000/svg">
  <!-- Regular rectangle -->
  <rect width="100" height="100" />

  <!-- Rounded corner rectangle -->
  <rect x="120" width="100" height="100" rx="15" />
</svg>

{{EmbedLiveSample('Example', 100, '100%')}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • SVG presentation attributes including {{SVGAttr("fill")}} and {{SVGAttr("stroke")}}
  • Other basic SVG shapes: {{SVGElement('circle')}}, {{ SVGElement('ellipse') }}, {{ SVGElement('line') }}, {{ SVGElement('polygon') }}, {{ SVGElement('polyline') }}