Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

polygon() — polygon from coordinate pairs

`polygon()` joins length-percentage coordinate pairs into a closed basic shape relative to its reference box.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

polygon( <'fill-rule'>? [ round <length> ]?, [ <length-percentage> <length-percentage> ]# )

polygon() joins length-percentage coordinate pairs into a closed basic shape relative to its reference box.

polygon(0 0, 100% 0, 0 100%) in a 100×60 box yields inside/outside sample 255/0; after widening to 200×60 it still yields 255/0, showing percentage scaling. Rounded polygon(round 10px, 0 0, 100% 0, 0 100%) reports support true.

Grammar: polygon( <'fill-rule'>? [ round <length> ]?, [ <length-percentage> <length-percentage> ]# ).

The first percentage coordinate resolves against width and the second against height; the UA always connects the last vertex to the first. The fill rule defaults to nonzero, while evenodd changes the interior of self-intersecting figures; the round option rounds vertices. The function serves clip-path and shape-outside. Polygon interpolation requires the same vertex count and fill rule.

Examples

const render=async(value,width,height,points)=>{const svg='<svg xmlns="http://www.w3.org/2000/svg" width="'+width+'" height="'+height+'"><foreignObject width="'+width+'" height="'+height+'"><div xmlns="http://www.w3.org/1999/xhtml" style="width:'+width+'px;height:'+height+'px;background:blue;clip-path:'+value+'"></div></foreignObject></svg>';const image=new Image();image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg);await image.decode();const canvas=document.createElement('canvas');canvas.width=width;canvas.height=height;const context=canvas.getContext('2d');context.drawImage(image,0,0);return points.map(([x,y])=>context.getImageData(x,y,1,1).data[3])};
const value='polygon(0 0, 100% 0, 0 100%)';
const small=await render(value,100,60,[[10,10],[80,40]]);
const wide=await render(value,200,60,[[100,10],[180,40]]);
const rounded=CSS.supports('clip-path','polygon(round 10px, 0 0, 100% 0, 0 100%)');
console.log('small-in/out='+small.join('/')+'; wide-in/out='+wide.join('/')+'; rounded-supported='+rounded);

Result

small-in/out=255/0; wide-in/out=255/0; rounded-supported=true

`polygon()` joins length-percentage coordinate pairs into a closed basic shape relative to its reference box.

Sources