What Is the SVG Defs Element Used For?

The <defs> element in Scalable Vector Graphics (SVG) serves as a container used to define reusable graphical components that do not render directly onto the canvas when parsed. This article explains the primary function of the <defs> container, how it works with referencing elements, its most common use cases, and the performance benefits it provides for modern web graphics.

The Core Purpose of <defs>

In SVG, <defs> stands for “definitions.” Its sole purpose is to store graphical elements, styles, and effects in memory so they can be referenced and rendered multiple times throughout the document. Any child element placed inside a <defs> block remains invisible until it is explicitly called by another element elsewhere in the SVG using an id attribute.

How <defs> Works

When an SVG renderer encounters elements inside <defs>, it registers them in the document object model without drawing pixels to the screen. To make an item visible, you reference its unique id through elements like <use> or presentation attributes like fill, stroke, or filter.

For example, a reusable icon is instantiated using the <use> tag:

<svg viewBox="0 0 100 100">
  <defs>
    <circle id="my-circle" cx="10" cy="10" r="10" />
  </defs>

  <!-- Rendering the defined element -->
  <use href="#my-circle" x="20" y="20" fill="blue" />
  <use href="#my-circle" x="50" y="50" fill="red" />
</svg>

Common Use Cases

The <defs> element typically holds assets and graphical parameters that need to be reused or applied as properties to other shapes:

Key Benefits of Using <defs>