Diagnosing SVG <rect> Rendering Failures: Missing Paint, Wrong Size, Wrong Position
A diagnostic guide for SVG <rect> elements that fail to paint, render at wrong dimensions, or appear at unexpected positions. Covers zero dimensions, missing paint, viewBox mismatches, namespace errors, stroke-width clipping, and transform interference with ordered checks and fixes.
24 Dec 2025, 01:45 UTC

Problem and Takeaway
An SVG <rect> that refuses to appear, renders at the wrong size, or shows up in the wrong place usually stems from one of five root causes: zero or missing dimensions, absent paint properties, coordinate-system mismatches, namespace errors, or transform interference. The fastest path to a fix is to verify computed attribute values in the browser inspector, then apply a high-contrast test stroke and fill to isolate visibility from geometry.
Recognizable Conditions
- Invisible rect: Element exists in DOM but no pixels paint.
- Wrong dimensions: Rect appears as a line, a dot, or fills the entire viewport.
- Wrong position: Rect renders far from the
x/ycoordinates you specified. - Clipped or distorted: Stroke dominates the shape or the rect is cut off at SVG boundaries.
Cause and Diagnostic Table
| Symptom | Most Likely Cause | Quick Verification |
|---|---|---|
| No pixels at all | Width/height zero, omitted, or non-numeric; or both fill and stroke absent | Inspect computed width/height; temporarily add fill="red" stroke="black" stroke-width="1" |
| Rect appears as a line | stroke-width ≥ min(width, height) | Compare stroke-width to rect dimensions in computed styles |
| Position shifted | Parent viewBox/preserveAspectRatio mismatch or CSS transform on ancestor | Check getCTM() on rect; inspect ancestor transforms |
| Rect missing from DOM | Element not in SVG namespace or inside non-SVG container | Inspect element namespace in devtools; look for parse errors in console |
| Unexpected scaling | Unitless attributes mixed with CSS length units | Verify all coordinates are unitless user coordinates or consistently use CSS units |
Ordered Diagnostic Checks
- Confirm namespace and parse health. Open browser devtools (F12), select the
<rect>, and verify itsnamespaceURIishttp://www.w3.org/2000/svg. Check the console for "SVG parse error" or "Invalid attribute" messages when the rect is inserted dynamically. - Validate dimensions. In the Elements panel, inspect the computed
widthandheightproperties. Both must be non-zero numbers. An omittedwidthorheightattribute defaults to0per the SVG spec. - Force visibility. Add a temporary inline style:
style="fill: #ff0000; stroke: #000000; stroke-width: 1px;". If the rect appears, the original issue was missing paint (SVG shapes have no default fill in most user-agent stylesheets). - Check coordinate system. Run
rect.getCTM()in the console. The returnedDOMMatrixreveals cumulative transforms fromviewBox,preserveAspectRatio, and CSS transforms. A non-identity matrix means yourx/yare in a transformed space. - Inspect stroke vs. size. If the rect renders as a thick line or solid block, compare
stroke-widthto the smaller ofwidth/height. Astroke-widthlarger than half the dimension fills or clips the shape. - Verify ancestor visibility. Walk up the DOM tree checking for
display: none,visibility: hidden, oropacity: 0on any ancestor including the root<svg>.
Fixes Tied to Findings
Zero or Missing Dimensions
Set explicit width and height attributes with unitless numbers (user coordinates). Example:
<rect x="10" y="20" width="100" height="50" fill="#3366cc" />
Do not rely on CSS width/height properties for SVG geometry; they affect the layout box, not the shape.
Missing Paint
Provide at least one of fill, stroke, or both. A transparent fill with visible stroke:
<rect x="10" y="20" width="100" height="50" fill="none" stroke="#000" stroke-width="2" />
Coordinate System Mismatch
Align the parent <svg> viewBox with your intended coordinate space. If you need a 500×300 drawing area:
<svg viewBox="0 0 500 300" preserveAspectRatio="xMidYMid meet">
<rect x="10" y="20" width="100" height="50" fill="#3366cc" />
</svg>
Changing preserveAspectRatio to none allows non-uniform scaling but distorts shapes.
Namespace Errors
Create rects with document.createElementNS('http://www.w3.org/2000/svg', 'rect'), not createElement('rect'). When serializing HTML, ensure the <svg> wrapper declares the namespace: <svg xmlns="http://www.w3.org/2000/svg">.
Stroke Overpowering Small Rects
Reduce stroke-width or increase rect dimensions. For a 10×10 rect with a 2-unit stroke, the visible fill area shrinks to 6×6. Use stroke-width="0.5" or vector-effect="non-scaling-stroke" if the stroke must stay thin under transforms.
Transform Interference
If a CSS transform on an ancestor shifts the rect, either move the rect into a non-transformed container or compensate in its x/y. To debug, compute the inverse matrix:
const ctm = rect.getCTM();
const inv = ctm.inverse();
const localPoint = inv.transformPoint(new DOMPoint(screenX, screenY));
Escalation Criteria
- After all six checks, the rect still does not render → file a browser bug with a minimal test case (single HTML file, no external dependencies).
- Rendering differs across browsers (Chrome vs. Firefox vs. Safari) → verify against the SVG 2 spec; the discrepancy may be a spec ambiguity.
- Rect renders correctly in standalone
.svgfile but fails when inlined in HTML → investigate HTML parser namespace handling or CSS interference from global stylesheets.
Limitations and Verification
This guide covers SVG 1.1/2 <rect> behavior in modern browsers (Chrome 100+, Firefox 100+, Safari 15+, Edge 100+). It does not address <rect> inside <foreignObject>, SVG 1.2 Tiny profiles, or server-side renderers like Batik or CairoSVG, which may have different default stylesheet behavior.
Practical verification: After applying a fix, reload the page and run this console snippet to confirm the rect paints pixels:
const r = document.querySelector('rect');
const bbox = r.getBBox();
console.log('bbox:', bbox); // width > 0 and height > 0 means paintable area exists
const canvas = document.createElement('canvas');
canvas.width = bbox.width + 10; canvas.height = bbox.height + 10;
const ctx = canvas.getContext('2d');
const serializer = new XMLSerializer();
const svgStr = serializer.serializeToString(r.closest('svg'));
const img = new Image();
img.onload = () => { ctx.drawImage(img, 0, 0); console.log('pixels:', ctx.getImageData(0,0,canvas.width,canvas.height).data); };
img.src = 'data:image/svg+xml;base64,' + btoa(svgStr);
If the pixel array contains non-zero alpha values, the rect is painting.