Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why nest an <svg> element inside another <svg> element?

Tags:

svg

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/

like image 227
Web_Designer Avatar asked Jun 03 '13 05:06

Web_Designer


People also ask

Can SVG elements be nested?

The SVG format allows for the nesting of SVG graphics. It is possible for an “<svg>” elements, to be placed within another “<svg>” element.

Can you put an SVG inside an SVG?

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.

What is viewBox in SVG?

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 .


1 Answers

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 the position (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, and width. The enclosing svg elements have different x-values. Since the x-position of the rectangles are interpreted relative to their enclosing svg elements x-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

like image 183
Mr. Alien Avatar answered Sep 27 '22 21:09

Mr. Alien