SVG provides opportunities for structuring a document using special elements that allow you to define and group objects, as well as refer to them in the future. They simplify code reuse while keeping it clean and readable. This article will examine these elements, as well as their differences and benefits.
Grouping using an item
Element
used to logically group a set of related graphic elements. This can be compared with the grouping of objects in graphic editors.
Elementcombines all its contents into a group. As a rule, he is given an identifier by which the appeal will be made in the future. Any styles applied to an element.will also be applied to all its descendants. This allows you to set styles and transformations, as well as add interactivity and animation immediately to a whole group of objects.
As an example, we will use a bird drawn in SVG. It consists of several separate figures described by circles and paths. [For obscure reasons, the author described circles in ways instead of using circles, although it was promised to use circles. Do not ask why. I fixed it. Do not ask why. - Per .]
Codepen
example Let's say you draw such a bird in Illustrator. If you need to move it from one place to another, the grouping of objects simplifies this task, since it allows you to not move each element individually. Grouping in SVG does roughly the same thing. In the example above, in addition to the general group for the entire bird (id = "bird"), we also identified subgroups that define the head and body separately (id = "body", id = "head").
If you change the fill color of the #body group, the fill of all elements within the group will change. It can be very convenient. [We are talking about elements for which the fill color is not set explicitly. For example, in the same #body, the #wing element has its own color. - Per .]
Grouping elements can be very useful not only for organizing and structuring a document. It can be of particular benefit if you want to add interactivity to the SVG graphics or set some transformations. Having grouped the elements, you can move them, scale or rotate all together, maintaining their position relative to each other.
So, in the case of a grouped bird, you can scale it with just one line of CSS:
#bird {transform: scale(2);}
Grouping adds convenience when implementing interactivity. You can hang the mouse events on the whole bird and make it react as a whole. This is much more convenient than doing the same for each element individually.
Element has another important and interesting feature: it can contain tags и <desc>, которые позволят изображению быть обработанным Screen Reader'ами [Программы воспроизведения текста для людей с ограниченными возможностями; не нашел емкого и адекватного перевода термина. — <em>Пер</em>.]; также они позволяют сделать код более читаемым для человека. Пример использования:<br>
<pre><code class="html"><g id="bird">
<title>BirdAn image of a cute little green bird with an orange beak.
Reusing items with
When working with graphics, you can often find a situation where repeating elements are used. In graphic editors in this case, the copy-paste method is usually used, which is more convenient than creating an element from scratch.
In SVG, an element implements similar functionality.