XML technologies / SVG / Filters, transformations and gradients / Defining shadows and lighting effects with filters / The defs tag in an SVG file

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

The <defs> tag has already appeared several times without explanation. Let us explain it now. In larger or more complex SVG documents using filters, gradients or fill patterns, these must be defined somewhere. That is the purpose of the <defs> section, which should be at the beginning of the SVG file. Define the <defs> tag immediately after the <svg> tag, and place the SVG file’s actual visible contents after the </defs> tag.

This brings us to the properties of the <defs> section. Unlike elements defined directly inside <svg></svg>, elements defined in the <defs> section become visible only when used. A valid <svg> document with a <defs> section could look as follows:

<?xml version="1.0"?>
...
<svg width="7.5cm" height="5cm">
  <defs>
    <filter>
      ...
    </filter>
    <linearGradient>
      ...
    </linearGradient>
    <pattern>
      ...
    </pattern>
  </defs>
  <rect x="1" y="1" width="198" height="118" style="fill:#888888; stroke:blue"/>
  <g>
    ...
  </g>
</svg>

   

 

 

 

back to top