HTML SVG <rect>

Beginner
8 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

The SVG <rect> element draws a rectangle from a top-left corner (x, y) plus width and height. Place it inside an <svg>.

Round corners with rx and ry. Style with fill, stroke, and stroke-width. Next basic shape: <circle>.

Baseline · Widely available.

<rect> is a stable SVG basic shape across modern browsers since about July 2015.

SVG2 · Geometry as CSS.

In SVG2, x, y, width, height, rx, and ry are also geometry properties — you can set them as CSS where supported.

Zero size hides the rectangle.

If width or height is missing, 0, or negative, nothing is drawn. Always set positive dimensions.

Syntax

rect.html
<svg width="240" height="140" viewBox="0 0 240 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Example rectangle">
  <rect x="30" y="30" width="180" height="80" rx="12"
        fill="#2563eb" stroke="#0f172a" stroke-width="4" />
</svg>

Self-closing <rect /> is fine in HTML. Keep a meaningful aria-label (or title) when the graphic matters.

Attributes

Core <rect> attributes:

AttributeDescriptionTry it
x / yTop-left corner (default 0)Try it
width / heightSize; ≤ 0 disables renderingTry it
rx / ryCorner radii (round the corners)Try it
fill / strokeInterior and outline colorsTry it
stroke-widthOutline thicknessTry it
pathLengthOptional path length for dash mathTry it

Global Attributes

The <rect> tag also supports the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Change the width and corner radius. Position stays at (30, 35) with height 70.

140
12

viewBox keeps the graphic scalable while you edit size and corners.

SVG

width="140" rx="12"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Filled Rectangle

Set position, size, fill, and a light stroke.

HTML
<svg width="240" height="140" viewBox="0 0 240 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Blue filled rectangle">
  <rect x="30" y="30" width="180" height="80"
        fill="#2563eb" stroke="#0f172a" stroke-width="3" />
</svg>
Try It Yourself

How It Works

  1. Create an <svg> with a matching viewBox.
  2. Set x / y for the top-left corner.
  3. Set width and height for size.
  4. Add fill and stroke for color and outline.

Example 2 — Rounded Corners

Use rx (and optional ry) to soften the corners.

HTML
<svg width="240" height="140" viewBox="0 0 240 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Rounded rectangle">
  <rect x="30" y="30" width="180" height="80" rx="16"
        fill="#16a34a" stroke="#0f172a" stroke-width="3" />
</svg>
Try It Yourself

How It Works

  1. Keep the same x, y, size as a sharp rectangle.
  2. Add rx="16" for horizontal corner rounding.
  3. If ry is omitted, browsers usually match it to rx.
  4. Large rx (near half the smaller side) creates a pill shape.

Example 3 — Stroke Only

Use fill="none" for an outlined frame.

HTML
<svg width="240" height="140" viewBox="0 0 240 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Outlined rectangle frame">
  <rect x="30" y="30" width="180" height="80" rx="10"
        fill="none" stroke="#ea580c" stroke-width="6" />
</svg>
Try It Yourself

How It Works

  1. fill="none" leaves the interior empty.
  2. stroke draws the border.
  3. stroke-width sets thickness.
  4. Optional rx still rounds the outline.

Example 4 — Square

Equal width and height make a square.

HTML
<svg width="200" height="200" viewBox="0 0 200 200"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Purple square">
  <rect x="40" y="40" width="120" height="120" rx="8"
        fill="#7c3aed" stroke="#0f172a" stroke-width="3" />
</svg>
Try It Yourself

How It Works

  1. Use the same number for width and height.
  2. Center it by choosing x and y that leave equal margins.
  3. Still a <rect> — there is no separate square element.
  4. Optional rx softens the square into a rounded tile.

Example 5 — Dashed Stroke

Combine stroke-dasharray with an outlined rectangle.

HTML
<svg width="240" height="140" viewBox="0 0 240 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Dashed rectangle outline">
  <rect x="30" y="30" width="180" height="80" rx="12"
        fill="none" stroke="#7c3aed" stroke-width="5"
        stroke-dasharray="14 8" />
</svg>
Try It Yourself

How It Works

  1. Stroke-only rect as in example 3.
  2. stroke-dasharray="14 8" means 14 on, 8 off.
  3. Optional pathLength can normalize dash math for animation.
  4. See also the SVG stroke page for more line styles.

Edge Cases & Pitfalls

Most missing-rect bugs are size, viewport, or fill/stroke settings.

size

width and height must be positive

Zero or negative dimensions disable rendering. Always set both explicitly for teaching examples.

viewBox

Stay inside the viewBox

If the rect sits outside viewBox / SVG size, it is clipped or invisible. Fit x + width and y + height.

corners

rx is clamped by size

Very large rx/ry values are clamped so corners stay valid. Huge radii will not explode the shape.

style

fill none needs a stroke

With fill="none" and no stroke, you see nothing. Set at least one visible paint.

Browser Support

The SVG <rect> element is supported in every modern browser.

✓ Baseline · Widely available

SVG <rect>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support rect geometry, rounded corners, and presentation attributes. Internet Explorer 9+ supports basic SVG rectangles.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
<rect> Widely supported

Bottom line: Safe for production icons, cards, and UI marks. Prefer attributes for teaching; CSS geometry properties are fine in modern browsers.

Key Takeaways

  • Geometry: x, y, positive width/height.
  • Corners: rx / ry for rounding.
  • Paint: fill, stroke, stroke-width.
  • Scale: parent viewBox + CSS size.

One line: position + size + paint inside an <svg>.

Default CSS Settings

SVG shapes use presentation defaults more than a browser CSS rule sheet. Typical initial paint:

defaults.css
/* Conceptual SVG defaults */
rect {
  fill: black;
  stroke: none;
  stroke-width: 1;
}

Set fill / stroke explicitly in examples so results stay predictable across contexts.

Frequently Asked Questions

Put a <rect> inside an <svg>. Set x and y for the top-left corner, width and height for size, then style with fill and/or stroke.
x and y place the top-left corner. width and height set the size. If width or height is 0 or less, the rectangle is not rendered.
They round the corners. rx is the horizontal radius and ry is the vertical radius. If you set only one, browsers usually infer the other.
Common causes: width or height is 0, the shape is outside the viewBox, fill is none with no stroke, or the SVG has zero size.
Yes. Use presentation attributes or CSS. In SVG2, x, y, width, height, rx, and ry are also geometry properties where supported.
Use <rect> inside SVG graphics that need vector scaling or mix with other SVG shapes. Use a normal HTML/CSS box for page layout.

Next: SVG Circle

Learn cx, cy, and r for circular shapes.

SVG Circle →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful