XML technologies / SVG / The coordinate system of SVG graphics / Building groups: The g tag

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

As previous sections showed, the <g> tag groups elements. SVG also provides document structure through the tags <desc>, <title> and the attribute id. The tag <desc> supplies a group description when inserted before it. The tag <title> supplies a title, and the attribute id names the group.

Note:
The tags <desc> and <title> are general-purpose and can describe elements other than the <g> tag.

The following code defines a group forming a symbol that could serve as a bullet. It consists of four squares and is described with the tags <desc> and <title>.

<?xml version="1.0" standalone="no"?>
...
<svg width="500" height="500">
  <desc>Drei Quadrate, die als Aufzaehlungszeichen dienen!</desc>
  <title>aufz01</title>
  <g id="aufz01">
    <rect x="17" y="7" width="35" height="35" rx="0" ry="0" style="fill:rgb(255,64,64)"/>
    <rect x="36" y="22" width="42" height="42" rx="0" ry="0" style="fill:rgb(255,255,0) "/>
    <rect x="24" y="32" width="22" height="22" rx="0" ry="0" style="fill:rgb(64,64,255)"/>
  </g>
</svg>

Inside a <g> tag, define any SVG elements or child groups using further <g> tags.

Advantages and disadvantages of groups

Groups have advantages and some drawbacks, but the latter arise only with unsuitable use. Their advantages are obvious:

  • Related elements can only be moved together.

  • Grouped elements can be formatted uniformly.

  • Grouped elements can be rotated together or receive filter effects.

Since groups are treated as units, individual elements cannot receive hyperlinks unless those hyperlinks are defined as group elements. Otherwise the hyperlink applies to the entire group, which is not always wanted.

   

 

 

 

back to top