Chapter 24: SVG Patterns

What is an SVG Pattern really?

A pattern is a small piece of SVG artwork (a “tile”) that gets repeated infinitely in both directions (like tiles on a bathroom floor) to fill a shape.

  • You define the tile once inside <defs>
  • You give the pattern an id
  • Then any shape can use it with fill=”url(#myPattern)”
  • The browser automatically repeats the tile to cover the entire area of the shape

This is vector-based repeating fill — perfect when you want something more interesting than solid color or gradient.

Basic structure – The <pattern> element

HTML

Most important attributes on <pattern>

Attribute Meaning / Purpose Common values Default Very important?
id Name to reference it “dots”, “stripes”, “hearts” ★★★★★
width / height Size of one tile (the repeating unit) 20, 40, “10%”, 50 0 (invisible!) ★★★★★
patternUnits Units for width/height/x/y “objectBoundingBox” or “userSpaceOnUse” userSpaceOnUse ★★★★☆
patternContentUnits Units for the content inside the pattern “objectBoundingBox” or “userSpaceOnUse” userSpaceOnUse ★★★☆☆
x / y Offset of the first tile (rarely needed) 0, 5, “10%” 0 ★★☆☆☆
patternTransform Rotate/scale/skew the entire pattern “rotate(45)” none ★★☆☆☆

Golden rule for beginners (use this 90% of the time):

text

Example 1 – Classic polka dots pattern

HTML

Result: Repeating orange dots every 40×40 units.

Try changing:

  • r=”4″ → smaller dots
  • width=”60″ height=”30″ → rectangular spacing
  • Add more circles in different colors

Example 2 – Diagonal stripes (very popular)

HTML

Alternative cleaner way (only lines):

XML

Example 3 – Checkerboard pattern

HTML

Example 4 – Pattern with text or small icon (creative!)

HTML

Quick Cheat Sheet – Pattern Units Decision

Goal / Situation Use this setting Why?
Pattern size fixed (pixels) patternUnits=”userSpaceOnUse” Most common – predictable size
Pattern scales with shape size patternUnits=”objectBoundingBox” Width/height in 0–1 range (like gradients)
Content inside also scales with shape patternContentUnits=”objectBoundingBox” Rare – usually leave as userSpaceOnUse
Want rotated pattern Add patternTransform=”rotate(45)” Nice for diagonal stripes

Common beginner mistakes

  • Forget width & height → pattern invisible
  • width=”0″ or very small → nothing or tiny repeated tile
  • Use percentages without objectBoundingBox → unexpected size
  • No fill/stroke inside pattern → transparent tiles
  • Pattern too large → looks like solid color

Mini practice tasks

  1. Create a green grid pattern (thin black lines every 30 units)
  2. Make red diagonal candy-cane style stripes
  3. Design a pattern with small yellow stars repeating

Paste your code here if you want feedback — I’ll review it like we’re pair-programming together 😄

Patterns are incredibly powerful once you get comfortable — you can literally fill shapes with any repeating design you can draw in SVG.

Next we can cover repeating patterns with images (<image> inside pattern), animated patterns, or combining patterns with gradients/filters.

Any part confusing? Tile size? Units difference? Creating your own tiles? Just tell me — we’ll zoom in with more examples until it feels easy and fun! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *