A pattern is a design that is used repeatedly as a fill for another object. The object can be a simple shape element, or a complex group of shapes, lines and paths as in the example below. We'll do a quick overview of how it works before diving into the details.
The repeating design used as the basis for a pattern is defined in <defs> section.
First we'll create a simple design. We'll add a group tag with an id of "target" so that we can refer to and apply the design later.
1
2
3
4
5
6
7
8
9
10
11
12
13
<defs>
<g id="target" stroke="black" fill="none">
<rect x="0" y="0" width="50" height="50" />
<circle cx="25" cy="25" r="25" />
<circle cx="25" cy="25" r="20" />
<circle cx="25" cy="25" r="15" />
<circle cx="25" cy="25"r="10"
fill="black"
fill-opacity=".2"
stroke="black"
/>
</g>
</defs>
This is what our design looks like:
Next we create the pattern tile using the <pattern> element. There are a lot of attributes to this element (patternUnits, width, height, etc.) which we'll go over later, but for now you should notice the id, which we'll use to apply the pattern as a fill, and the <use> element using the graphic we created earlier.
1
2
3
4
5
6
7
8
9
10
11
12
<defs>
<g id="target" stroke="black" fill="none">
// more code...
</g>
<pattern
id="tile"
x="0" y="0"
width="50" height="50"
patternUnits="userSpaceOnUse">
<use href="#target" x="0" y="0" />
</pattern>
</defs>
Finally, the pattern tile is used as a fill by referencing its id.
fill="url(#tile)"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<defs>
<g id="target" stroke="black" fill="none">
// graphics making up target
</g>
<pattern
id="tile"
x="0" y="0" width="50" height="50" patternUnits="userSpaceOnUse">
<use href="#target" x="0" y="0"></use>
</pattern>
</defs>
<rect x="0" y="0"
width="500" height="300"
fill="url(#tile)">
</rect>
id: Used to identify the pattern so it can be used as a fill or stroke of an object.
width, height: The width and height are used to define the size of the pattern tile,
patternUnits: defines the coordinate system used for the pattern's width and height. There are two possible values:
patternContentUnits: similar to patternUnits, except it defines the units of measure used when setting the geometric values of the pattern content.
Defines the unit of measure used when setting the geometric value of the pattern tile.
In this example we're using userSpaceOnUse as our pattern unit, so the pattern uses the same coordinate system as the rest of the SVG. Each tile is the same size as the graphic used in the tile. (50x50)
<pattern ... patternUnits="userSpaceOnUse"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<circle id="circle_design"
cx="25" cy="25" r="25"
stroke="grey" fill="#eaeaea" />
<pattern
id="circle_tile"
x="0"
y="0"
width="50"
height="50"
patternUnits="userSpaceOnUse"
>
<use href="#circle_design" x="0" y="0"></use>
</pattern>
<rect x="0" y="0" width="600" height="300" fill="url(#circle_tile)">
</rect>
Now lets keep everything the same, but change the width and height of the pattern to 100x100.
Let's change the width and height of the pattern tile again - this time to 25x25.
When patternUnits is set to objectBoundingBox the tile width and height are defined as a percentage or fraction of the filled object’s bounding box. In this example, width and height of the tile are 20% of the width and height of the rectangle’s bounding box. So .2 x 500 = 100.
<pattern ... patternUnits="objectBoundingBox"
1
2
3
4
5
6
7
8
<pattern
id="tile"
x="0" y="0"
width=".2" height=".2"
patternUnits="objectBoundingBox"
>
// more code
</pattern>
Similar to patternUnits, except it defines the units of measure used when setting the geometric values of the actual pattern content - the width, height, etc of the paths, circles, rects and other elements used in the pattern design itself.
The size values for the content that defines the pattern uses the same coordinate system as the element that uses the pattern.
1
2
3
4
5
<pattern id="tile" x="0" y="0" width="50" height="50"
patternUnits="userSpaceOnUse"
patternContentUnits="userSpaceOnUse">
<circle cx="25" cy="25" r="25" ... />
</pattern>
The size values for the content are defined as percentage values of the object bounding box. So if we want to re-create the same pattern as directly above, we need to set the radius, cx and cy values to .05
1
2
3
4
5
6
7
8
9
<pattern id="tile" x="0" y="0" width="20" height="20"
patternUnits="userSpaceOnUse"
patternContentUnits="objectBoundingBox">
<circle cx=".05" cy=".05" r=".05" ... />
</pattern>
<rect x="0" y="0"
width="500" height="500"
fill="url(#tile)"... />
Since the size of the pattern content is expressed as a percentage of the bounding box, it scales as the bounding box gets bigger or smaller.