The left shape is a classic shape created using polygon() and the shape at the right is the inverted version (the cut-out shape). The idea is to take a rectangle and cut the left shape from it to get the right one.
The goal of this article is to learn how to easily get the inverted version of any shape you want.
Creating The Main Shape
For this part, I will redirect you to my previous article or to my guide for making CSS shapes. I will not focus on how to create shapes using clip-path but rather how to invert existing shapes.
Our starting point will be the polygon of the shape you want to invert:
clip-path: polygon(X0 Y0, X1 Y1, ... Xn Yn);
If you are new to clip-path, I highly recommend you read the articles I linked to familiarize yourself with the basics of creating shapes. You don’t need to know or master all the tricks related to clip-path but at least understand how the coordinates system works and how to create some common shapes. You can also grab the code of many CSS shapes from my online collection.
Inverting The Shape
Now that we have our main shape, let’s invert it! I am going to give the process of doing it then I will explain how it works. It’s like a recipe to follow that works for any kind of shape.
First, take the previous polygon and place it inside a CSS variable.
--shape: X0 Y0, X1 Y1, ... Xn Yn;
Second, copy the first value and place it at the end. We need to duplicate the first coordinate.
--shape: X0 Y0, X1 Y1, ... Xn Yn, X0 Y0;
After that let’s create a simple rectangle having the same dimension as the shape we want to invert:
.invert {
/* the size */
width: ...;
aspect-ratio: ...;
/**/
clip-path: polygon(0 0,100% 0,100% 100%,0 100%);
}
The polygon I am using is nothing but the coordinates of the 4 corners as I am explaining in my previous article.
Next, let’s duplicate the first value to get the following polygon:
clip-path: polygon(0 0,100% 0,100% 100%,0 100%, 0 0);
And finally, we append the CSS variable at the end of that polygon
.invert {
--shape: X0 Y0, X1 Y1, ... Xn Yn, X0 Y0;
clip-path: polygon(0 0,100% 0,100% 100%,0 100%, 0 0, var(--shape));
}
We are done! Our shape is inverted without a lot of effort.