Pattern

You can apply patterns as fills or strokes.

Use the <pattern> element to create a pattern.

You would declare patterns inside the <defs> element, and you must give the gradient an id attribute so you can reference it.

SVG elements

<pattern>

Pattern tile
  • widthwidth of the pattern tile
  • heightheight of the pattern tile
  • xx coordinate offset of the pattern tile
  • yy coordinate offset of the pattern tile
Units
  • patternUnitsdefines the coordinate system for attributes x, y, width, height; specify userSpaceOnUse (use viewport/absolute coordinate) or objectBoundingBox (use the bounding box of the element that references this pattern, coordinate values will range from 0 to 1)
  • patternContentUnitsdefines the coordinate system for children elements of <pattern>; specify userSpaceOnUse (use viewport/absolute coordinate) or objectBoundingBox (use the bounding box of the element that references this pattern, coordinate values will range from 0 to 1)
View box
  • viewBoxthe position and dimension of the viewbox of the pattern, e.g. 0 0 100 100 (if the viewbox's aspect ratio does not match the pattern's width and height, parts outside the viewbox will be visible in the pattern tile)
  • preserveAspectRatiohow to fit the viewBox if the viewbox's aspect ratio does not match the pattern's width and height, e.g. none (stretch) or xMidYMid (preserve aspect ratio and align at center)
Others
  • hrefreference to another <pattern> that will be used as a template
  • patternTransformspace-separated CSS transform functions, e.g. skewX(20) translate(-35, 0)
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <defs>
    <!-- Gradient definition -->
    <linearGradient id="Gradient1">
      <stop offset="5%" stop-color="white" />
      <stop offset="95%" stop-color="blue" />
    </linearGradient>
    <linearGradient id="Gradient2" x1="0" x2="0" y1="0" y2="1">
      <stop offset="5%" stop-color="red" />
      <stop offset="95%" stop-color="orange" />
    </linearGradient>
    <!-- Pattern definition -->
    <pattern id="Pattern" x="0" y="0" width=".25" height=".25">
      <rect x="0" y="0" width="50" height="50" fill="skyblue" />
      <rect x="0" y="0" width="25" height="25" fill="url(#Gradient2)" />
      <circle cx="25" cy="25" r="20" fill="url(#Gradient1)" fill-opacity="0.5" />
    </pattern>
  </defs>
  <rect fill="url(#Pattern)" stroke="black" width="200" height="200"/>
</svg>
| LIVE PREVIEW

References