Clipping in CSS and SVG — The clip-path Property and <clipPath> Element
CSS and SVG have a lot in common. A lot of the features that we have in CSS today were imported from SVG. One of these features is the Clipping operation. Both CSS and SVG allow us to "clip" elements into custom non-rectangular shapes. In this article we will go over the clipping techniques in both CSS and SVG, covering everything you need to know to get started.
Please note that the demos in this article may not work in your browser. You should check this compatibility table out for more information. You don't need to view the live demos to follow up with the article. Not all clipping features are implemented and some features may be buggy. The purpose of this article is to go over how clipping works in CSS and SVG, and serves as a reference for when these features are fully implemented. I'm also not including any vendor prefixes in the code samples here, but they are included in the live demos.
What is clipping?
Clipping is a graphical operation that allows you to fully or partially hide portions of an element. The clipped element can be any container or graphics element. The portions of the element that are shown or hidden are determined by a clipping path.
A clipping path defines a region where everything on the “inside” of this region is allowed to show through but everything on the outside is “clipped out” and does not appear on the canvas. This region is known as the clipping region. Any parts of the element that lie outside of a clipping region are not drawn. This includes any content, background, borders, text decoration, outline and visible scrolling mechanism of the element to which the clipping path is applied, and those of its descendants.
The clipped element can be any container or graphics element.
A clipping path is conceptually equivalent to a custom viewport for the element it applies to. It influences what parts of the element are rendered on the screen, but it does not affect the element’s inherent geometry—the element will affect the flow around it as it normally would, and every other element on the page will still see and treat the element as if it were still rectangular, even if it’s clipped to a non-rectangular shape. If you want to change the way the content around the element flows and have it respond to the defined shape of the clip path, you can use the CSS Shapes properties. If you want to learn more about how to do that, you can check thearticles I wrote about this topic.
Clipping in CSS – The clip-path Property
The clip-path property is part of the CSS Masking Module. The clipping operation has been a part of SVG since 2000, and has moved into the CSS Masking module so that it now allows clipping HTML elements as well as SVG elements.
The clip-path property is used to specify a clipping path that is to be applied to an element. Using clip-path, you can apply an SVG <clipPath> to an element by referencing that path in the property value. You can also define a clipping path using one of the basic shapes defined in the CSS Shapes module. These shapes can be created using shape functions. The shape functions available are polygon(), circle(), inset() (used to define inset rectangles), and ellipse().
Applying a clipping path to an element using the clip-path property is very simple and straightforward:
/* Referencing an SVG clipPath */ .element{ clip-path:url(#svgClipPathID); }
/* Using a CSS basic shape function */ .element{ clip-path:polygon(...);/* or one of the other shape functions */ }
For example, if we were to define a polygonal clipping path using the polygon() function, and then apply it to an image, the code would look like the following:
The basic shape functions allow us to create a limited number of shapes; the most complex of these shapes is a polygon. If you want to use a more complex shape that looks like more than just a group of connected straight lines, you can use the SVG <clipPath> element. As the name <clipPath> implies, you can clip to any arbitrary path. This means that you can use the <path> element to create any arbitrary path and use that as a clipping path.
In our second example, we’re going to define and use an SVG clipPath. The code for the clip path looks like the following:
Indeed, the <clipPath> element can contain any number of basic shapes (<rect>, <circle>, etc.), <path> elements, or even <text> elements.
If you specify a piece of <text> inside a <clipPath>, that text will be used as a clipping path. Whatever’s under the text will be visible “through” it, and anything outside the text area will not be rendered.
Note here that you can clip anything to the text. This opens a door for a lot of possibilities and effects. You can use animated images (such as GIFs) or even videos, and then clip them to some text of your choice. The sky is the limit here.
The following is an example of a piece of text used as a clipping path.
```
The cool thing about SVG `` is that it can be displayed using a custom font, just like HTML text can. In this example I'm using the [Vollkorn font](http://www.google.com/fonts/specimen/Vollkorn) from Google Web Fonts. I've set the width of the text to be the same as the width of the image, using the `textLength` attribute, and positioned the text using the `x` and `y` coordinates. Note here that the `x` and `y` coordinates determine the position of the bottom left corner of the text (where the bottom stands for the baseline of the text).
The result of applying the above text clip path to the image looks like so:
And as we mentioned, you can also use multiple basic shapes inside ``. We'll dig into `` and its contents in the next section, so, for now, we'll keep it simple. In this example I'm using multiple ``s, each with a different size and position.
```
The image will show through these circles combined, but will not be rendered outside them.
As we mentioned at the beginning of this article, you can apply clip paths using the `clip-path` property to SVG elements too. In all of the above examples, the clipping paths were applied to an HTML ``. In the following example, a clipping path is applied to the root `