Home SVG <rect> HTML SVG <rect> Definition
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
Syntax <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.
Reference
Attributes Core <rect> attributes:
Attribute Description Try it x / yTop-left corner (default 0) Try itwidth / heightSize; ≤ 0 disables rendering Try itrx / ryCorner radii (round the corners) Try itfill / strokeInterior and outline colors Try itstroke-widthOutline thickness Try itpathLengthOptional path length for dash math Try it
Also supported
Global Attributes The <rect> tag also supports the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.
Try it
Live Preview Change the width and corner radius. Position stays at (30, 35) with height 70.
Code
HTML Examples Short, copy-ready snippets. Use Try It Yourself for a live editor.
1. Basic 2. Rounded 3. Stroke 4. Square 5. Dashed Example 1 — Basic Filled Rectangle Set position, size, fill, and a light stroke.
<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> How It Works Create an <svg> with a matching viewBox.Set x / y for the top-left corner.Set width and height for size.Add fill and stroke for color and outline.Example 2 — Rounded Corners Use rx (and optional ry) to soften the corners.
<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> How It Works Keep the same x, y, size as a sharp rectangle.Add rx="16" for horizontal corner rounding.If ry is omitted , browsers usually match it to rx.Large rx (near half the smaller side) creates a pill shape.Example 3 — Stroke Only Use fill="none" for an outlined frame.
<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> How It Works fill="none" leaves the interior empty.stroke draws the border.stroke-width sets thickness.Optional rx still rounds the outline.Example 4 — Square Equal width and height make a square.
<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> How It Works Use the same number for width and height.Center it by choosing x and y that leave equal margins.Still a <rect> — there is no separate square element.Optional rx softens the square into a rounded tile.Example 5 — Dashed Stroke Combine stroke-dasharray with an outlined rectangle.
<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> How It Works Stroke-only rect as in example 3.stroke-dasharray="14 8" means 14 on, 8 off.Optional pathLength can normalize dash math for animation.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.
Compatibility
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.
Remember
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>.
UA style
Default CSS Settings SVG shapes use presentation defaults more than a browser CSS rule sheet. Typical initial paint:
/* 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 How do I draw a rectangle in SVG? 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.
What do x, y, width, and height mean? 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.
What do rx and ry do? 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.
Why is my rectangle invisible? 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.
Can I style rect with CSS? Yes. Use presentation attributes or CSS. In SVG2, x, y, width, height, rx, and ry are also geometry properties where supported.
When should I use rect vs a CSS box? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion