Keep SVG pattern spacing fixed across shapes

Compare the same dotted tile in panels 80 and 160 units wide.

Asset preparation2 min read

You widen a patterned banner panel and the dots spread apart. The tile width is a fraction of the panel's bounds, so the spacing grows with the panel. Use patternUnits="userSpaceOnUse" when the wider panel should fit more dots at the same spacing.

Measure the distance between repeats

Open the editable SVG. Both rows use the same dot artwork. The panels are 80 and 160 units wide and both are 48 units tall.

In the top row, both panels show four dots across. In the bottom row, the narrow panel shows four dots and the wide panel shows eight at the same spacing.

Tile width80-unit panel160-unit panel
.25 of each panel's bounds4 dots, 20-unit spacing4 dots, 40-unit spacing
20 user units4 dots, 20-unit spacing8 dots, 20-unit spacing

Set the tile size and the artwork coordinates

svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 48">
  <defs>
    <pattern id="dots" patternUnits="userSpaceOnUse"
             width="20" height="12" viewBox="0 0 20 12">
      <circle cx="10" cy="6" r="3" fill="#7cebc8"/>
    </pattern>
  </defs>
  <rect width="160" height="48" fill="url(#dots)"/>
</svg>

patternUnits sets the repeat rectangle's coordinates. The pattern's viewBox fits the dot artwork into that rectangle. With a viewBox present, patternContentUnits has no effect. The relative example keeps dots circular with xMidYMid meet even though the spacing changes. SVG 2 pattern coordinates.

Resize the shape and count the repeats again. If you also scale the whole SVG or apply a transform to an individual shape, check the final export at its display size. Twenty SVG units correspond to twenty screen pixels only at one-to-one scale. Keep the bounding-box mode when each shape needs the same number of repeats.

Image tools
All guides