CSS polygon() Function
polygon() function is a CSS basic shape value that's part of the CSS Shapes module.
Basic shapes such as
polygon() can be used as a value for properties such as
shape-outside to control the flow of content around the element, and
clip-path to clip the element's contents to the basic shape.
This means you can do things like, have text flowing around the element in the shape that you specify, have an image clipped to that shape, etc.
Here's an example of how the
polygon() function works with the
Here we have a
div floated to the left. And because I've applied the
polygon() function to it, the subsequent text flows around the shape as specified.
It's easier to visualize this concept by looking at the following example.
As you can see, the polygon is drawn within the floated box. The floated box actually protrudes out from the polygon area, into the surrounding text. In other words, the text is allowed to flow right up to where we've specified the polygon.
How to Specify the Polygon Dimensions
You must specify the dimensions of the polygon by providing arguments to the
polygon() function. The arguments are provided in comma-separated pairs, which represent the x and y axis coordinates of a given vertex of the polygon.
At least three vertices must be provided, otherwise you'll end up with an empty float area.
You can use percentages or length values (e.g.
em, etc) to specify the dimensions of the polygon.
Here's an example of using a percentage value.
Here's an example of using the CSS
polygon() function with the
If your browser supports the
clip-path property (and the
polygon() function), the above image should appear in the shape of the polygon area. Otherwise it will appear in the normal dimensions of the image.
At the time of writing, browser support for the
clip-path property is limited. None of the major browsers support it, however, webkit browsers have partial support using the
If you create a polygon that crosses back over itself multiple times, you'll end up with an inside area that could either be filled or empty. You can use a fill-rule to determine what to do about this.
The fill-rule can be added as the first argument of the
polygon() function. You can use either
nonzero (the default value).
polygon(evenodd, 0 40%, 100% 40%, 20% 100%, 50% 0, 80% 100%);
Here are two examples, that demonstrate the effect of the two fill-rule values.
nonzero (the default value):
The official syntax of the
polygon() function is as follows:
polygon() = polygon( [<fill-rule>,]? [<shape-arg> <shape-arg>]# )
<fill-rule> can be either
evenodd. The default value is
The syntax of
<shape-arg> = <length> | <percentage>
As indicated in the official syntax, the
polygon() function accepts the following values:
Specifies a fill-rule to use to determine the interior of the polygon. Can be either
evenodd. The default value is
This can be specified in either of the following ways:
- Specifies a length value to use (e.g.
vw, etc) for the radius.
- Specifies a percentage value to use. The percentage value uses the width and height of the reference box.
polygon()function is defined in CSS Shapes Module Level 1 (W3C Candidate Recommendation, 20 March 2014)
- It's also being further defined in CSS Shapes Module Level 2 (Editorâ€™s Draft)
The following table provided by Caniuse.com shows the level of browser support for this feature.