A group of elements can be clipped against a clip path which is defined by a clipPath element:
<clipPath id="myClip"> <circle cx="350" cy="100" r="50"/> </clipPath> <g style="stroke:none;clip-path:url(#myClip)"> <rect style="fill:red" x="0" y="0" width="500" height="20" /> <rect style="fill:yellow" x="0" y="20" width="500" height="20" /> <rect style="fill:blue" x="0" y="40" width="500" height="20" /> <rect style="fill:red" x="0" y="60" width="500" height="20" /> <rect style="fill:yellow" x="0" y="80" width="500" height="20" /> <rect style="fill:blue" x="0" y="100" width="500" height="20" /> <rect style="fill:yellow" x="0" y="120" width="500" height="20" /> <rect style="fill:blue" x="0" y="160" width="500" height="20" /> <rect style="fill:red" x="0" y="180" width="500" height="20" /> <rect style="fill:yellow" x="0" y="200" width="500" height="20" /> <rect style="fill:blue" x="0" y="220" width="500" height="20" /> <rect style="fill:red" x="0" y="240" width="500" height="20" /> <rect style="fill:yellow" x="0" y="260" width="500" height="20" /> <rect style="fill:blue" x="0" y="280" width="500" height="20" /> <rect style="fill:red" x="0" y="300" width="500" height="20" /> <rect style="fill:yellow" x="0" y="320" width="500" height="20" /> </g>
The group of rectangles are clipped against the circle basic shape. The clipPath element has an id attribute and the g element has a style or attribute clip-path that specifies the path to be used for clipping. It is also possible to clip against a path or even text:
<clipPath id="myClip"> <path d="M 0 112 C 40 160 120 80 160 106 C 160 106 165 110 170 103 C 180 0 220 20 260 61 C 260 61 280 73 290 68 C 288 80 272 85 250 85 C 195 83 210 110 230 120 C 260 140 265 185 200 191 C 150 195 30 195 0 112 Z"/> </clipPath>
<clipPath id="myClip"> <text x="10" y="310" style="font-size:150px">DUCK</text> </clipPath>
For referenced items, such as clip paths, it is considered good practice to surround them with a defs element to emphasise that they are not rendered directly. Drawing elements, such as path are not rendered if they are defined within a defs element.
Figure below shows the results of the three clipping paths defined above.