SVG Pattern Element Guide for Repeating Fills

The <pattern> element in Scalable Vector Graphics (SVG) is a specialized container used to define reusable graphical tiles for filling or stroking shapes. This article explains the primary purpose of the SVG pattern element, how its coordinate systems work, and how it enables designers and developers to create seamless, scalable repeating backgrounds and textures without relying on external raster images.

Understanding the Purpose of the <pattern> Element

In SVG, shapes like rectangles, circles, and complex paths are typically filled with solid colors or gradients. The <pattern> element expands this capability by allowing any combination of vector elements—such as paths, circles, text, or shapes—to be grouped into a single tile that repeats horizontally and vertically across the surface of a target shape.

Instead of manually duplicating vector shapes across a canvas, you define the tile once within an SVG’s <defs> (definitions) section and reference it wherever a repeating fill is needed.

How the <pattern> Element Works

To use a pattern, you assign it a unique id attribute and then reference that ID in the fill or stroke attribute of another SVG element using the url(#pattern-id) syntax.

A basic pattern structure looks like this:

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="dot-pattern" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
      <circle cx="10" cy="10" r="4" fill="#007ACC" />
    </pattern>
  </defs>

  <!-- Shape utilizing the pattern fill -->
  <rect width="200" height="200" fill="url(#dot-pattern)" />
</svg>

In this example, the circle is drawn inside a 20x20 tile. When applied to the rectangle, the pattern engine automatically repeats that 20x20 tile across the entire 200x200 area.

Key Attributes for Controlling Pattern Layout

The behavior of a repeating fill depends on two critical coordinate attributes:

Key Advantages of SVG Patterns