<rect>
Core element
Use one element to draw boxes, panels, buttons, tracks, and diagram blocks.

SVG’s <rect> draws rectangles and rounded rectangles from position and size values. This tutorial covers x, y, width, height, rounded corners with rx / ry, fill and stroke styling, responsive viewBox usage, five worked examples, and how <rect> compares to HTML boxes and other SVG shapes.
Core element
Use one element to draw boxes, panels, buttons, tracks, and diagram blocks.
Top-left corner
Position the rectangle on the SVG canvas using its top-left starting point.
Size
Control the box dimensions directly with simple numeric values.
Rounded corners
Turn sharp boxes into cards, chips, and pill buttons without extra paths.
Presentation
Paint the inside, outline the edges, or build outline-only rectangles.
Responsive
Scale UI boxes and graphics cleanly across layouts because SVG is vector-based.
SVG’s <rect> is one of the most practical basic shapes on the web. It draws everything from simple highlight boxes to fully rounded buttons, cards, panels, progress tracks, and diagram containers.
Unlike a CSS layout box, an SVG rectangle lives inside a graphic coordinate system. That makes it ideal when you want precise geometry, scalable vector output, and easy combination with gradients, filters, icons, text, or other shapes.
Many modern interface pieces are rectangles with slight radius, subtle stroke, and responsive scaling. Learning <rect> gives you the foundation for buttons, cards, panels, badges, and data-visualisation frames.
x, y, width, and height define the basic box.
One tweak transforms a plain box into a card, chip, or pill button.
Use fill, stroke, and stroke-width to style the rectangle.
Combine <rect> with a viewBox so one drawing scales anywhere.
In short: set the top-left point, give the box a width and height, then add fill, stroke, and optional rounded corners.
Basic form of the SVG rectangle element:
<svg width="220" height="140">
<rect x="30" y="20" width="160" height="90" />
</svg> | Attribute | Type | Description |
|---|---|---|
x | Length / number | x-coordinate of the rectangle’s top-left corner. Defaults to 0. |
y | Length / number | y-coordinate of the rectangle’s top-left corner. Defaults to 0. |
width | Length / number | Width of the rectangle. If 0, nothing is drawn. |
height | Length / number | Height of the rectangle. If 0, nothing is drawn. |
rx | Length / number | Horizontal radius of the rounded corners. |
ry | Length / number | Vertical radius of the rounded corners. |
fill | Paint | Interior color, gradient, pattern, or none. |
stroke | Paint | Outline color around the rectangle. |
stroke-width | Length / number | Outline thickness in user units. |
| Result | Details |
|---|---|
| vector rectangle | Renders a box shape from a top-left point plus width and height, optionally with rounded corners. |
<svg width="220" height="140" viewBox="0 0 220 140">
<rect x="30" y="20" width="160" height="90"
fill="#3b82f6" stroke="#0f172a" stroke-width="2" />
</svg> | Idea | Detail | Notes |
|---|---|---|
| Origin | (0, 0) is top-left | y grows downward |
| Visibility | width and height must be positive | Zero size means nothing renders |
| Rounded corners | rx / ry soften corners | Large values create capsule-like shapes |
| Goal | Code |
|---|---|
| Basic rectangle | <rect x="30" y="20" width="160" height="90" /> |
| Rounded card | rx="16" ry="16" |
| Outline only | fill="none" stroke="#0f172a" stroke-width="3" |
| Pill button | rx="999" ry="999" |
| Responsive SVG | <svg viewBox="0 0 220 140">... + CSS width |
| CSS themed box | fill="currentColor" or CSS rect { fill: ... } |
<rect> vs CSS box vs <circle> vs <polygon> vs <path>All can create visible shapes, but they solve different problems.
box shapeBest for vector rectangles, panels, cards, and rounded UI blocks inside SVG.
layout boxBest for normal HTML layout, document flow, and standard interface structure.
round shapeUse when you need one radius and a perfect circle.
many cornersUse for custom multi-sided shapes like triangles or hexagons.
any outlineUse when primitives are not enough and you need curves or mixed commands.
<rect>Reach for <rect> whenever your SVG needs a box-based foundation.
Perfect for container boxes, headers, tiles, and dashboard cards.
Rounded rectangles make natural foundations for pills, tags, and CTAs.
Use one rect for the track and another for the filled progress portion.
Flow charts and architecture diagrams often use rectangles for steps or services.
If the shape should be perfectly round, use <circle> instead.
Key benefit: one simple element covers sharp boxes, rounded cards, and pill buttons without switching to complex paths.
Five starter snippets using <rect>. Click View Output for a preview, or Try It Yourself to edit live.
Start with a basic filled box, then add rounded corners.
Draw a blue rectangle with a dark outline using position, size, fill, and stroke.
<svg width="220" height="140">
<rect x="30" y="25" width="160" height="90"
fill="#3b82f6" stroke="#0f172a" stroke-width="2" />
</svg> The browser places the rectangle’s top-left corner at (30, 25), then stretches it to the given width and height. fill paints the inside, while stroke outlines the edges.
rx / ryRound the corners to create a softer card or button-like shape.
<svg width="240" height="150" viewBox="0 0 240 150">
<rect x="30" y="35" width="180" height="80"
rx="18" ry="18"
fill="#10b981" stroke="#065f46" stroke-width="3" />
</svg> rx and ry curve the corners instead of leaving sharp 90-degree angles. Rounded corners are especially common in modern interface design for cards, pills, and call-to-action buttons.
Outline styles, responsive boxes, and pill-shaped buttons.
Remove the fill and keep only the border for frames, placeholders, and focus rings.
<svg width="240" height="140">
<rect x="25" y="25" width="190" height="90"
fill="none" stroke="#7c3aed" stroke-width="4" />
</svg> fill="none" stops the interior from being painted, so the rectangle shows only its outline. This is a common pattern for frames, empty states, and hover highlights.
viewBoxDefine the rectangle in SVG coordinates, then let CSS control how large it appears on the page.
<style>
.card-frame { width: 180px; height: auto; display: block; }
</style>
<svg class="card-frame" viewBox="0 0 220 120"
role="img" aria-label="Responsive blue card frame">
<rect x="20" y="20" width="180" height="80"
rx="14" fill="#dbeafe" stroke="#2563eb" stroke-width="3" />
</svg> The viewBox defines the internal coordinate system, while CSS controls the displayed size. That lets the same rectangle scale cleanly across layouts without changing x, y, width, or height.
Use a large corner radius so the rectangle becomes a capsule-style button background.
<svg width="260" height="90" viewBox="0 0 260 90">
<rect x="20" y="20" width="220" height="50"
rx="25" ry="25"
fill="#111827" stroke="#60a5fa" stroke-width="2" />
<text x="130" y="51" text-anchor="middle"
font-size="18" fill="white">Get Started</text>
</svg> When the corner radius approaches half the rectangle’s height, the ends become fully rounded. This is the standard SVG recipe for pill buttons, tags, and badge backgrounds.
Real-world places where SVG rectangles show up every day.
Card shells, alert boxes, stat tiles, and dashboard panels.
Example: a rounded profile summary card.
CTA backgrounds, pill buttons, tags, and capsules.
Example: a rounded "Subscribe" button.
Background tracks and filled bars in gauges or loaders.
Example: a horizontal upload progress indicator.
Process steps, labels, and grouped boxes in diagrams.
Example: a "Validate Input" node in a workflow.
Tooltip boxes, legend items, and chart annotation backgrounds.
Example: a chart tooltip with a light panel background.
Inline labels, message bubbles, and notification shells.
Example: a "New" badge next to a feature name.
Pro Tip: if your design is mostly box-based, <rect> usually gets you there faster than building the same outline with <path>.
Why build rectangles with SVG instead of image assets.
Position and size are obvious from just four core attributes.
Rectangles stay crisp on high-density displays without extra exports.
rx and ry create modern rounded UI without custom path math.
One element can replace a downloaded asset for many box-based graphics.
Combine rectangles with gradients, patterns, masks, filters, and animation.
Pro Tip: inline SVG rectangles are ideal when a box needs to scale, theme, or sit alongside other vector shapes as one graphic.
Follow these practices for clean, scalable SVG rectangles.
viewBox for responsive graphicsDefine geometry once, then let CSS control the displayed size.
Small radii feel like cards; large radii create chips and pill buttons.
Half of stroke-width extends outside the box edge and can clip near boundaries.
currentColor or CSS for themingThat makes SVG buttons and panels adapt naturally to dark mode or brand palettes.
<rect> often works best as the visual foundation for a larger SVG component.
Pro Tip: when a rectangle acts as a button shell, center any <text> with text-anchor="middle" and a clear vertical y-value.
Avoid these mistakes when your rectangle does not look right.
width or heightA rectangle with no area does not render.
→ Ensure both dimensions are positive numbers.
fill="none" Without a strokeNothing is painted if both the fill and outline are effectively invisible.
→ Pair outline-only rectangles with stroke and stroke-width.
If the box extends beyond the SVG coordinate system, it gets clipped.
→ Keep x + width and y + height within the SVG bounds, plus room for stroke.
Huge rx / ry values can make the shape look more pill-like than intended.
→ Choose a radius that matches the component style you actually want.
<rect> for HTML layoutAn SVG rectangle is a graphic primitive, not a document layout container.
→ Use normal HTML/CSS boxes for page structure and <rect> for vector graphics.
Pro Tip: if a rectangle vanishes, first check width, height, fill/stroke visibility, and whether the shape fits inside the viewBox.
<rect> Is DrawnThe rectangle begins at (x, y), measured from the SVG origin at the top-left corner.
These values tell the browser how far to extend the box horizontally and vertically from the starting point.
Add rx and ry if the box should feel softer or capsule-shaped.
Use fill for the interior and stroke / stroke-width for the outline.
The browser draws the rectangle mathematically, so it stays sharp at every zoom level.
<rect> uses top-left positioning plus width and height, not a center point.rx and ry create rounded corners and can turn a box into a pill shape.fill paints the inside; stroke paints the border.<circle> instead of forcing a rectangle.viewBox when the artwork must scale responsively.Quick Takeaway: top-left point + size + paint. Add rx / ry when the box should feel more modern or button-like.
The SVG <rect> element is supported in all modern browsers — and has been for many years — as part of SVG 1.1 basic shapes.
Use
Bottom line: Safe for production sites. Prefer inline SVG or cached .svg assets; no polyfill is required for current browsers.
SVG <rect> is one of the most useful shapes in vector graphics: set x, y, width, and height, then style with fill, stroke, and optional rounded corners.
Practice the five examples above, then continue to SVG Circle to learn how round shapes differ from box-based ones.
Keep dimensions visible, leave room for strokes, and use rx / ry thoughtfully so the box matches the intended UI style.
viewBox when the rectangle must scale responsivelyrx / ry values that match the UI stylefill="none" for outline-only boxes and add a visible strokewidth or height to 0 and expect a visible shape<path> markup when a simple <rect> already fits<rect>Build crisp box shapes the SVG way.
Core SVG box element
APIx & y set the start
Geometrywidth & height matter
Shaperx / ry soften the box
StyleScales cleanly
ResponsiveAn SVG <rect> is the foundation of UI cards, buttons, panels, badges, progress tracks, and diagram blocks because one shape can be square, rounded, filled, outlined, or responsive. Because <rect> is so flexible, many SVG UI kits use it as the base shape for skeleton loaders, chart tooltips, toggles, and card backgrounds.
Learn how center point and radius create perfect round shapes after mastering SVG rectangles.
8 people found this page helpful