HTML SVG <pattern>

Beginner
9 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

The SVG <pattern> element defines a small graphic tile that repeats across a shape. Put it in <defs>, give it an id, then paint with fill="url(#id)" or stroke="url(#id)".

Great for dots, grids, and stripes. For smooth color blends, use linear or radial gradients instead.

Baseline · Widely available.

<pattern> paint servers are stable across modern browsers since about July 2015.

Zero tile size hides the pattern.

Default width and height are 0. Always set positive tile dimensions.

Deprecated for new work: xlink:href.

Prefer a full local <pattern> definition, or modern href when you need a template reference.

Syntax

pattern.html
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Rectangle filled with a dot pattern">
  <defs>
    <pattern id="dots" width="20" height="20"
             patternUnits="userSpaceOnUse">
      <circle cx="4" cy="4" r="3" fill="#2563eb" />
    </pattern>
  </defs>
  <rect x="20" y="20" width="180" height="100" rx="8" fill="url(#dots)" />
</svg>

Pattern: define a tile → unique id → apply with url(#id).

Attributes

Core <pattern> attributes:

AttributeDescriptionTry it
idHandle for url(#…)Try it
width / heightTile size (must be > 0)Try it
patternUnitsobjectBoundingBox (default) or userSpaceOnUseTry it
patternContentUnitsUnits for graphics inside the tileTry it
patternTransformRotate, scale, or skew the tileTry it
viewBoxOptional viewport for the tile contentsTry it
fill / stroke url(#id)Apply the pattern to a shapeTry it

Global Attributes

The <pattern> tag also supports the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Change the tile size. Smaller tiles mean denser dots.

20

patternUnits="userSpaceOnUse" keeps tile size in SVG units.

SVG

width="20" height="20"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Dot Pattern Fill

One circle per tile repeats across a rectangle.

HTML
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Dot pattern fill">
  <defs>
    <pattern id="dots" width="20" height="20"
             patternUnits="userSpaceOnUse">
      <circle cx="4" cy="4" r="3" fill="#2563eb" />
    </pattern>
  </defs>
  <rect x="20" y="20" width="180" height="100" rx="8" fill="url(#dots)" />
</svg>
Try It Yourself

How It Works

  1. Define <pattern id="dots"> in <defs>.
  2. Set tile width/height to the repeat spacing.
  3. Draw one motif (a small circle) inside the tile.
  4. Apply with fill="url(#dots)".

Example 2 — Stripe Pattern

A thin rect in each tile creates stripes.

HTML
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Vertical stripe pattern">
  <defs>
    <pattern id="stripes" width="12" height="12"
             patternUnits="userSpaceOnUse">
      <rect width="6" height="12" fill="#16a34a" />
    </pattern>
  </defs>
  <rect x="20" y="20" width="180" height="100" rx="8" fill="url(#stripes)" />
</svg>
Try It Yourself

How It Works

  1. Tile is 12×12 units.
  2. Half-width rect paints a stripe; the rest stays empty.
  3. Repeating tiles form a stripe field.
  4. Change width to change stripe density.

Example 3 — Pattern on Stroke

Paint a thick outline with the same tile.

HTML
<svg width="200" height="160" viewBox="0 0 200 160"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Circle stroked with a pattern">
  <defs>
    <pattern id="ringDots" width="10" height="10"
             patternUnits="userSpaceOnUse">
      <circle cx="2" cy="2" r="2" fill="#ea580c" />
    </pattern>
  </defs>
  <circle cx="100" cy="80" r="45"
          fill="none" stroke="url(#ringDots)" stroke-width="16" />
</svg>
Try It Yourself

How It Works

  1. Define the pattern as usual.
  2. Set fill="none" on the shape.
  3. Use stroke="url(#id)" instead of fill.
  4. Widen stroke-width so the tile shows on the outline.

Example 4 — Rotated Pattern

Tilt tiles with patternTransform.

HTML
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Rotated stripe pattern">
  <defs>
    <pattern id="diag" width="14" height="14"
             patternUnits="userSpaceOnUse"
             patternTransform="rotate(45)">
      <rect width="7" height="14" fill="#7c3aed" />
    </pattern>
  </defs>
  <rect x="20" y="20" width="180" height="100" rx="8" fill="url(#diag)" />
</svg>
Try It Yourself

How It Works

  1. Build a stripe tile first.
  2. Add patternTransform="rotate(45)".
  3. The whole tiling grid tilts, not just one stripe.
  4. Also try scale() or translate() in the same attribute.

Example 5 — Grid with viewBox

Use viewBox on the pattern for neat line grids.

HTML
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Grid pattern fill">
  <defs>
    <pattern id="grid" width="20" height="20"
             patternUnits="userSpaceOnUse"
             viewBox="0 0 20 20">
      <path d="M 20 0 L 0 0 0 20"
            fill="none" stroke="#64748b" stroke-width="1" />
    </pattern>
  </defs>
  <rect x="20" y="20" width="180" height="100" rx="8"
        fill="url(#grid)" stroke="#cbd5e1" />
</svg>
Try It Yourself

How It Works

  1. Match viewBox to the tile size.
  2. Draw two edges of a cell with a path.
  3. Repeating cells form a full grid.
  4. Note: with viewBox set, patternContentUnits is ignored.

Edge Cases & Pitfalls

Most missing patterns are zero tile size, wrong ids, or unit surprises.

size

width/height must be positive

Defaults are 0. A zero tile never paints.

units

objectBoundingBox can stretch

Default units scale with the shape. Prefer userSpaceOnUse for fixed pixel-like tiles while learning.

id

url(#id) must match

Typos fall back to no pattern fill. Keep ids unique in the document.

defs

Pattern is not painted alone

Nothing shows until a shape references the pattern with fill or stroke url(#…).

Browser Support

SVG <pattern> is supported in every modern browser.

✓ Baseline · Widely available

SVG <pattern>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support pattern tiles and url(#id) paint. Internet Explorer 9+ supports basic SVG patterns.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
<pattern> Widely supported

Bottom line: Safe for production textures and chart fills. Start with userSpaceOnUse and explicit tile sizes for predictable results.

Key Takeaways

  • Define a tile in <defs> with an id.
  • Size: positive width and height.
  • Apply: fill or stroke url(#id).
  • Transform with patternTransform when needed.

One line: tile in <pattern>, paint with url(#id).

Default CSS Settings

Patterns are paint servers — they have no visible box of their own:

defaults.css
/* Conceptual defaults */
pattern {
  /* width/height default to 0 → invisible until set */
  /* patternUnits: objectBoundingBox */
  /* patternContentUnits: userSpaceOnUse */
}

.shape {
  fill: url(#dots);
}

Prefer presentation attributes in these lessons so the paint stays inside the SVG markup.

Frequently Asked Questions

Define a <pattern> in <defs> with an id and tile graphics. Apply it with fill="url(#id)" or stroke="url(#id)" so the tile repeats across the shape.
They set the size of one tile. Default is 0, which draws nothing — always set positive width and height.
It sets the coordinate system for x, y, width, and height. Default objectBoundingBox uses the target shape box; userSpaceOnUse uses absolute SVG units.
Yes. Use stroke="url(#id)" and a large enough stroke-width so the tile is visible along the outline.
Often objectBoundingBox units or a mismatched tile size. Try userSpaceOnUse and tune width/height.
Use patterns for repeating textures and grids. Use linear or radial gradients for smooth color blends.

Next: SVG Linear Gradients

Learn <linearGradient> and color stops for smooth fills.

SVG Linear Gradients →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful