Why would a demo such as this: http://jsbin.com/ejorus/2/edit, have an <svg>
element nested inside another <svg>
element?
<svg class="graph"> <svg viewBox="0 0 1000 1000" preserveAspectRatio="none"> <g transform="translate(30,0)"> <!-- ... --> </g> </svg> </svg>
The JS Bin is a modified version of the demo in this blog post: http://meloncholy.com/blog/making-responsive-svg-graphs/
The SVG format allows for the nesting of SVG graphics. It is possible for an “<svg>” elements, to be placed within another “<svg>” element.
The svg element is a container that defines a new coordinate system and viewport. It is used as the outermost element of SVG documents, but it can also be used to embed an SVG fragment inside an SVG or HTML document. Note: The xmlns attribute is only required on the outermost svg element of SVG documents.
The viewBox attribute defines the position and dimension, in user space, of an SVG viewport. The value of the viewBox attribute is a list of four numbers: min-x , min-y , width and height .
Nesting SVG elements can be useful to group SVG shapes together, and position them as a collection. All shapes nested inside an svg element will be
positioned (x, y) relative to
theposition (x, y)
of its enclosing svg element. By moving the x and y coordinates of the enclosing svg element, you move all the nested shapes too.Here is an example where two rectangles are nested inside two svg elements. Except for the colors the two rectangles have the same definitions for
x, y, height
, andwidth
. The enclosing svg elements have different x-values. Since thex-position
of the rectangles are interpreted relative to their enclosing svg elementsx-position
, the two rectangles are displayed at different x-positions.- By Jakob Jenkov
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <svg x="10"> <rect x="10" y="10" height="100" width="100" style="stroke:#ff0000; fill: #0000ff"/> </svg> <svg x="200"> <rect x="10" y="10" height="100" width="100" style="stroke:#009900; fill: #00cc00"/> </svg> </svg>
Credits
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With