Untitled conversation
- Usercode
- Assistant
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Nested Weird SVGs</title>
<style>
body {
background: #111;
color: #eee;
font-family: monospace, monospace;
text-align: center;
padding: 20px;
}
svg {
border: 1px solid #444;
margin: 10px;
background: #222;
display: inline-block;
}
</style>
</head>
<body>
<h1>Nested Weird SVG Code Example</h1>
<!-- Outer SVG with odd dimensions and no viewBox -->
<svg width="25pc" height="15pc" xmlns="http://www.w3.org/2000/svg">
<!-- Nested SVG #1 with em units and viewBox -->
<svg x="2em" y="1em" width="10em" height="7em" viewBox="0 0 100 70" xmlns="http://www.w3.org/2000/svg" style="background:#222; border:2px dashed #66ccff;">
<circle cx="50" cy="35" r="30" fill="lime" stroke="purple" stroke-width="5" />
<text x="50" y="40" font-size="12" text-anchor="middle" fill="#000" font-family="Courier New">Nested 1</text>
</svg>
<!-- Nested SVG #2 with % units (missing viewBox) -->
<svg x="50%" y="50%" width="40%" height="40%" xmlns="http://www.w3.org/2000/svg" style="background:#440; border:1px solid #ee5533;">
<rect width="100" height="100" fill="url(#grad1)" />
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<polygon points="10,90 50,10 90,90" fill="cyan" stroke="magenta" stroke-width="3" />
</svg>
<!-- Nested SVG #3 with vw and vh units and odd viewBox -->
<svg x="10vw" y="5vh" width="20vw" height="15vh" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg" style="background:#003344; border:3px dotted #ffeebb;">
<ellipse cx="100" cy="50" rx="90" ry="40" fill="orange" stroke="black" stroke-width="2" />
<line x1="10" y1="90" x2="190" y2="10" stroke="lime" stroke-width="4" />
<text x="100" y="55" font-size="18" text-anchor="middle" fill="black" font-family="Arial Black">Nested 3</text>
</svg>
<!-- Nested SVG #4 with cm and mm units and weird missing viewBox -->
<svg x="1cm" y="10mm" width="6cm" height="4cm" xmlns="http://www.w3.org/2000/svg" style="background:#550055; border:5px double #ff55ff;">
<path d="M 10 80 Q 95 10 180 80" stroke="white" fill="transparent" stroke-width="6" />
<circle cx="100" cy="50" r="30" fill="transparent" stroke="yellow" stroke-width="10" />
</svg>
<!-- Nested SVG #5 nested inside Nested SVG #1 with weird units and missing viewbox -->
<svg x="60%" y="60%" width="4em" height="4em" xmlns="http://www.w3.org/2000/svg" style="background:#222222; border:1px solid #ffffff;">
<rect width="100" height="100" fill="none" stroke="lime" stroke-width="4" />
<circle cx="50" cy="50" r="40" fill="none" stroke="red" stroke-width="5" />
<text x="50" y="56" font-size="14" text-anchor="middle" fill="lime" font-family="Comic Sans MS">Tiny</text>
</svg>
</svg>
</body>
</html>
hughly nested something looks weird.
You're right to feel that something's off. The layout and rendering issues you're noticing largely come down to the complex nesting of SVG elements with mixed and relative units, unusual or missing viewBox attributes, and conflicting coordinate contexts.
Let me break down the main issues and suggest fixes or explanations:
width="25pc" and height="15pc". pc means picas (1pc = 12pt = 1/6 inch), which is unusual but valid.% inside SVGs are relative to the SVG viewport for width and height, but for x and y on nested SVGs, percentages are relative to the parent SVG's dimensions.viewBox set, some SVGs have no internal coordinate system to map their content to the displayed size. This makes percentages and some units unpredictable.% units but has no viewBox.% for x and y, but Nested SVG #1 has a viewBox. It's uncertain how the percentages inside Nested #1 will be resolved for Nested #5.viewBox AttributesviewBox at all.viewBox (like #1 and #3), others don't (#2, #4, #5).This causes:
viewBox use their width and height as the user coordinate system (1:1).x and y may get misaligned.Your comment says:
<!-- Nested SVG #5 nested inside Nested SVG #1 -->
but your code shows:
<svg x="2em" y="1em" width="10em" height="7em" viewBox="0 0 100 70" ...>
<!-- Nested SVG content #1 ... -->
</svg>
<!-- Nested SVG #5 -->
<svg x="60%" y="60%" width="4em" height="4em" ...>
...
</svg>
That means Nested SVG #5 is not actually nested inside SVG #1 in the DOM tree; it's a sibling nested inside the outer SVG.
This is probably a mistake or copy-paste error, leading to unexpected rendering.
x/y positioning on nested SVG pathsWhen you nest SVG elements inside another SVG, positioning can become complicated, especially with nested SVGs shifted by some amounts and also scaling their own viewports.
<svg x="2em" y="1em" width="10em" height="7em" viewBox="0 0 100 70" ...>
...existing content...
<!-- Nested SVG #5 inside SVG #1 -->
<svg x="60%" y="60%" width="4em" height="4em" ...>
...
</svg>
</svg>
This will make #5 positioned relative inside #1's coordinate system, making the % and em units behave in a nested manner.
For SVG #2, #4, and #5, add appropriate viewBox values that match the coordinate size of their content:
<svg x="50%" y="50%" width="40%" height="40%" viewBox="0 0 100 100" ...>
...
</svg>
<svg x="1cm" y="10mm" width="6cm" height="4cm" viewBox="0 0 200 100" ...>
...
</svg>
<svg x="60%" y="60%" width="4em" height="4em" viewBox="0 0 100 100" ...>
...
</svg>
This aligns the user coordinate system so your shapes can scale nicely.
For example, use pixels or relative units consistently:
px for outer SVG (or use viewBox + % sizes).x, y) and size (width, height).Sometimes nested <svg> elements complicate runtime positioning.
You might replace some nested <svg> with plain <g> elements (groups) with transform/scaling to avoid confusion.
Your weird-looking nested SVG page is mostly caused by:
viewBox attributes on nested SVGs.<!-- Nested SVG #1 with nested SVG #5 inside -->
<svg x="2em" y="1em" width="10em" height="7em" viewBox="0 0 100 70" xmlns="http://www.w3.org/2000/svg" style="background:#222; border:2px dashed #66ccff;">
<circle cx="50" cy="35" r="30" fill="lime" stroke="purple" stroke-width="5" />
<text x="50" y="40" font-size="12" text-anchor="middle" fill="#000" font-family="Courier New">Nested 1</text>
<!-- Move Nested SVG #5 here -->
<svg x="60%" y="60%" width="4em" height="4em" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" style="background:#222222; border:1px solid #ffffff;">
<rect width="100" height="100" fill="none" stroke="lime" stroke-width="4" />
<circle cx="50" cy="50" r="40" fill="none" stroke="red" stroke-width="5" />
<text x="50" y="56" font-size="14" text-anchor="middle" fill="lime" font-family="Comic Sans MS">Tiny</text>
</svg>
</svg>
And add missing viewBox attributes to the other nested SVGs similarly.
If you'd like, I can provide a fully fixed version of your code. Would you want me to do that?
Not yet included in a published release.