SVG Vector Lab logo SVG Vector Lab

Repeating SVG paint

SVG Pattern Fill: Control Tile Size and Coordinates

To create an SVG pattern fill, define a repeating tile inside <defs> and reference its ID with fill="url(#tile)". Set patternUnits="userSpaceOnUse" for fixed tile dimensions in SVG user units, or use objectBoundingBox for dimensions relative to each filled shape. Set the tile's width and height explicitly.

How do you create a repeating SVG fill?

Start with vertical stripes. Save the complete snippet as an .svg file and open it in your browser. Both rectangles use one pattern definition, so you can compare the fill without maintaining two copies.

One stripe pattern on two rectangles The left rectangle is 80 units wide and the right is 160. Both show blue stripes repeating every 20 units.
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="120"
     viewBox="0 0 300 120" role="img" aria-labelledby="pf-stripe-title pf-stripe-desc">
  <title id="pf-stripe-title">One stripe pattern on two rectangles</title>
  <desc id="pf-stripe-desc">The left rectangle is 80 units wide and the right is 160. Both show blue stripes repeating every 20 units.</desc>
  <defs>
    <pattern id="pf-stripes" patternUnits="userSpaceOnUse"
             x="0" y="0" width="20" height="20">
      <rect width="20" height="20" fill="#e9f3fc"/>
      <rect width="8" height="20" fill="#1769aa"/>
    </pattern>
  </defs>
  <rect x="20" y="20" width="80" height="80" fill="url(#pf-stripes)"/>
  <rect x="120" y="20" width="160" height="80" fill="url(#pf-stripes)"/>
</svg>

Result: the left rectangle spans four tile widths; the right spans eight. Each repeat contains an 8-unit blue stripe and a 12-unit pale gap. Changing the target rectangle's width reveals more stripes without widening them.

To shift the stripe alignment, change the pattern's x="0" to x="5". In the left rectangle, the first blue stripe then starts at x=25 instead of x=20, while the repeat stays 20 units wide.

  1. Put reusable artwork in <defs>.
  2. Give <pattern> a unique ID and positive tile dimensions.
  3. Draw the motif within that tile.
  4. Reference the ID from the target's fill.

The MDN patterns tutorial explains this definition-and-reference structure. Use a pattern for repeating artwork; use an SVG gradient for a continuous color transition.

Why does the tile size change between shapes?

patternUnits controls the tile's x, y, width and height. Its default is objectBoundingBox, not userSpaceOnUse.

Setting Meaning of tile width On an 80-unit-wide shape On a 160-unit-wide shape
userSpaceOnUse, width="20" 20 user units Four tile widths Eight tile widths
objectBoundingBox, width="0.25" One quarter of the target's bounding-box width 20-unit tiles 40-unit tiles

Compare these settings directly. All four rectangles are 80 units high; the right-hand rectangles are twice as wide as the left-hand ones.

Fixed tiles versus shape-relative tiles Top row: dots repeat every 20 units on both rectangles. Bottom row: tile width doubles on the wider rectangle while the dots keep their size.
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="220"
     viewBox="0 0 300 220" role="img" aria-labelledby="pf-units-title pf-units-desc">
  <title id="pf-units-title">Fixed tiles versus shape-relative tiles</title>
  <desc id="pf-units-desc">Top row: dots repeat every 20 units on both rectangles. Bottom row: tile width doubles on the wider rectangle while the dots keep their size.</desc>
  <defs>
    <pattern id="pf-fixed" patternUnits="userSpaceOnUse" width="20" height="20">
      <circle cx="10" cy="10" r="3" fill="#1769aa"/>
    </pattern>
    <pattern id="pf-relative" patternUnits="objectBoundingBox" width="0.25" height="0.25">
      <circle cx="10" cy="10" r="3" fill="#1769aa"/>
    </pattern>
  </defs>
  <rect width="300" height="220" fill="#ffffff"/>
  <rect x="20" y="20" width="80" height="80" fill="url(#pf-fixed)"/>
  <rect x="120" y="20" width="160" height="80" fill="url(#pf-fixed)"/>
  <rect x="20" y="120" width="80" height="80" fill="url(#pf-relative)"/>
  <rect x="120" y="120" width="160" height="80" fill="url(#pf-relative)"/>
</svg>

Result: the top row has four repeats on the left and eight on the right. The bottom row has four on each, with horizontal spacing doubling on the right. Its dot radius stays 3 because the content still uses the default user-space coordinates.

Fixed user units are not fixed screen pixels: scaling the outer SVG or transforming the painted object also scales its fill. Display the first example at 600 by 240 CSS pixels, keeping its viewBox unchanged, and its 20-unit repeat becomes 40 CSS pixels wide. See how viewBox sizing works.

What does patternContentUnits change?

Think of two independent rulers: one measures the tile, and the other measures the artwork inside it. patternContentUnits defaults to userSpaceOnUse, even when the tile uses bounding-box units.

With patternContentUnits="objectBoundingBox", content coordinates refer to the whole target's bounding box, not a separate 0-to-1 square for each tile. For a quarter-width tile, its horizontal midpoint is therefore 0.125, not 0.5.

Changing the content coordinate system Top row: dots keep a 3-unit radius. Bottom row: normalized content stretches horizontally on the wider rectangle.
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="220"
     viewBox="0 0 300 220" role="img" aria-labelledby="pf-content-title pf-content-desc">
  <title id="pf-content-title">Changing the content coordinate system</title>
  <desc id="pf-content-desc">Top row: dots keep a 3-unit radius. Bottom row: normalized content stretches horizontally on the wider rectangle.</desc>
  <defs>
    <pattern id="pf-content-fixed" patternUnits="objectBoundingBox"
             width="0.25" height="0.25" patternContentUnits="userSpaceOnUse">
      <circle cx="10" cy="10" r="3" fill="#1769aa"/>
    </pattern>
    <pattern id="pf-content-relative" patternUnits="objectBoundingBox"
             width="0.25" height="0.25" patternContentUnits="objectBoundingBox">
      <circle cx="0.125" cy="0.125" r="0.04" fill="#1769aa"/>
    </pattern>
  </defs>
  <rect width="300" height="220" fill="#ffffff"/>
  <rect x="20" y="20" width="80" height="80" fill="url(#pf-content-fixed)"/>
  <rect x="120" y="20" width="160" height="80" fill="url(#pf-content-fixed)"/>
  <rect x="20" y="120" width="80" height="80" fill="url(#pf-content-relative)"/>
  <rect x="120" y="120" width="160" height="80" fill="url(#pf-content-relative)"/>
</svg>

Result: bottom-left dots have a 3.2-unit radius. Bottom-right dots become ellipses with horizontal radius 6.4 and vertical radius 3.2. Both rows still use four tiles across each rectangle; changing content units changes the motif, not the repeat count.

Choose this normalized approach when you want artwork to stretch with the target. Choose user-space content when a dot's radius should remain 3 as you change the rectangle's width from 80 to 160.

How does a pattern viewBox simplify the artwork?

A pattern viewBox lets you design in a convenient local coordinate system and fit that drawing into the tile. When it is present, patternContentUnits has no effect. The pattern element reference documents these attributes and the default preserveAspectRatio="xMidYMid meet".

A ten-unit drawing fitted into two tile sizes Left: round dots in 20-by-20 tiles. Right: horizontally stretched dots in 40-by-20 tiles using preserveAspectRatio none.
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="120"
     viewBox="0 0 300 120" role="img" aria-labelledby="pf-viewbox-title pf-viewbox-desc">
  <title id="pf-viewbox-title">A ten-unit drawing fitted into two tile sizes</title>
  <desc id="pf-viewbox-desc">Left: round dots in 20-by-20 tiles. Right: horizontally stretched dots in 40-by-20 tiles using preserveAspectRatio none.</desc>
  <defs>
    <pattern id="pf-square" patternUnits="userSpaceOnUse" width="20" height="20"
             viewBox="0 0 10 10">
      <circle cx="5" cy="5" r="2" fill="#1769aa"/>
    </pattern>
    <pattern id="pf-wide" patternUnits="userSpaceOnUse" width="40" height="20"
             viewBox="0 0 10 10" preserveAspectRatio="none">
      <circle cx="5" cy="5" r="2" fill="#1769aa"/>
    </pattern>
  </defs>
  <rect width="300" height="120" fill="#ffffff"/>
  <rect x="20" y="20" width="80" height="80" fill="url(#pf-square)"/>
  <rect x="120" y="20" width="160" height="80" fill="url(#pf-wide)"/>
</svg>

Result: the left tile doubles the 10-by-10 drawing, giving the dot a radius of 4. On the right, none stretches that same drawing to 40 by 20, producing radii of 8 and 4. Remove preserveAspectRatio="none" and the right-hand dots stay round, centered in the wider tile.

For predictable dots, start with patternUnits="userSpaceOnUse", explicit tile dimensions, and a matching-aspect-ratio viewBox. To rotate stripes without rotating the target outline, add patternTransform="rotate(45)" to the stripe pattern; compare pattern transforms with transforms applied to whole shapes.

Why is the pattern missing, tiny or clipped?

Check references first, then dimensions, then coordinates. The SVG 2 pattern rules specify zero default tile dimensions and default clipping at tile boundaries. A missing width or height can therefore leave the target unpainted.

Symptom Check Concrete repair
No fill ID and URL fragment differ Pair id="pf-repair" with fill="url(#pf-repair)"
No tiles Width or height is missing or zero Set both to 20 with userSpaceOnUse
Huge gaps or an apparently solid fill A tile number was interpreted as a bounding-box fraction Use 0.25 for one quarter, or switch to user units for a 20-unit repeat
Tiny or missing motif Content coordinates use the wrong ruler For a 20-by-20 tile, add viewBox="0 0 20 20" and set the dot to cx="10" cy="10" r="3"
Motif cut at the edge Artwork extends outside its tile In a 20-unit tile, center a radius-3 dot at 10,10, rather than 0,0
Wrong pattern appears after combining SVGs Two definitions share an ID Prefix each ID and update every matching URL reference

This working example applies one fill to both a <rect> and a rectangular <path>:

One working dot fill on a rectangle and a path Both shapes display round dots repeating every 20 units from one uniquely named pattern.
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="120"
     viewBox="0 0 300 120" role="img" aria-labelledby="pf-repair-title pf-repair-desc">
  <title id="pf-repair-title">One working dot fill on a rectangle and a path</title>
  <desc id="pf-repair-desc">Both shapes display round dots repeating every 20 units from one uniquely named pattern.</desc>
  <defs>
    <pattern id="pf-repair" patternUnits="userSpaceOnUse" width="20" height="20">
      <circle cx="10" cy="10" r="3" fill="#1769aa"/>
    </pattern>
  </defs>
  <rect width="300" height="120" fill="#ffffff"/>
  <rect x="20" y="20" width="80" height="80" fill="url(#pf-repair)"/>
  <path d="M120 20H280V100H120Z" fill="url(#pf-repair)"/>
</svg>

Result: both shapes have identical dot size and spacing. To isolate a blank fill, temporarily replace its target's fill with #1769aa: if the shape appears, restore the URL and inspect the pattern definition.

How do you check a pattern before exporting?

Keep the full SVG together, including <defs>. Copying only the painted rectangle omits the artwork its URL references.

  1. Paste the first complete stripe example into the editor's Source tab and choose Apply.
  2. Confirm four repeats on the left and eight on the right.
  3. Choose Export, then Download SVG.
  4. Open the downloaded file in a browser and compare both rectangles with the original.

In a Chromium check on November 17, 2026, this exact stripe example retained its pattern definition and fill references through the live editor's Source, Apply and Download SVG workflow, and the downloaded artwork matched the original rendering. This check covers that source-based workflow, not every pattern or visual paint edit; recheck the downloaded artwork after changing a patterned fill.

Try changing the first example's stripe width from 8 to 4, keeping its tile width at 20, then check the narrower stripe in the SVG Vector Lab editor.

Sources checked November 2026.