SVG Patterns

What You’ll Learn
SVG patterns let you define a small tile and repeat it to fill any shape. This is perfect for backgrounds, textures, charts, and decorative UI elements.
You’ll learn how to define <pattern> inside <defs>, control the tile size using width/height, choose the coordinate system with patternUnits, and apply the pattern with fill="url(#id)".
⚡ Quick Reference — SVG Pattern
<pattern>Define a repeating tile
url(#id)Use in fill (or stroke)
width / heightControls how often it repeats
patternUnitsuserSpaceOnUse or objectBoundingBox
patternTransformRotate/scale/skew the pattern
<svg>
<defs>
<pattern id="p" patternUnits="userSpaceOnUse" width="10" height="10">
<circle cx="5" cy="5" r="2" />
</pattern>
</defs>
<rect fill="url(#p)" />
</svg>👀 Live Preview — Dots & Stripes
Two common pattern tiles: polka dots and vertical stripes.
Polka Dot Pattern Fill
Define a small dot tile and repeat it across a larger rectangle.
<svg width="220" height="180" viewBox="0 0 220 180">
<defs>
<pattern id="dots" patternUnits="userSpaceOnUse" width="12" height="12">
<circle cx="6" cy="6" r="3" fill="#ef4444" />
</pattern>
</defs>
<rect x="10" y="10" width="200" height="160" rx="16" fill="url(#dots)" />
</svg>Striped Pattern (Multiple Shapes)
Patterns can contain multiple SVG elements. Here we combine two rectangles to form a repeating stripe tile.
<svg width="220" height="180" viewBox="0 0 220 180">
<defs>
<pattern id="stripes" patternUnits="userSpaceOnUse" width="20" height="20">
<rect width="10" height="20" fill="#3b82f6" />
<rect x="10" width="10" height="20" fill="#f8fafc" />
</pattern>
</defs>
<rect x="10" y="10" width="200" height="160" rx="16" fill="url(#stripes)" stroke="#e2e8f0" />
</svg>💡 Best Practices
Do
- Start with
patternUnits="userSpaceOnUse"for predictable sizing - Keep tile sizes small to reduce repetition artifacts
- Use
patternTransformfor rotation or scaling - Name ids clearly (e.g.
dots,stripes) - Test patterns on different shapes (rectangles, circles, paths)
Don’t
- Reuse the same pattern id across multiple inline SVGs on one page
- Use huge tile sizes for small textures (it looks repetitive)
- Forget that
objectBoundingBoxuses 0–1 coordinates - Overcomplicate patterns when a simple gradient works better
- Assume patterns will look the same across all resolutions without checking
❓ Frequently Asked Questions
<pattern> defines a small tile inside <defs>. When you use fill="url(#patternId)", the tile repeats to cover the shape.patternUnits controls the coordinate system for the pattern tile. userSpaceOnUse uses absolute SVG units; objectBoundingBox uses relative 0–1 units based on the target shape’s bounding box.stroke="url(#patternId)" to paint the outline with a repeating pattern (and set stroke-width as needed).objectBoundingBox units or when the tile size is mismatched to the target. Try patternUnits="userSpaceOnUse" and adjust width/height to control repetition.patternTransform, for example patternTransform="rotate(45)" or patternTransform="scale(0.5)".Draw a Polygon Next
Learn how to build triangle, pentagon, and custom polygon shapes using <polygon>.
4 people found this page helpful
