Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

shape() — responsive path made from CSS commands

`shape()` describes a basic shape with `CSS` commands, retaining units, calculations, and variables unavailable inside a `path()` string.

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

Syntax

shape( <'fill-rule'>? from <coordinate-pair>, <shape-command># )

shape() describes a basic shape with CSS commands, retaining units, calculations, and variables unavailable inside a path() string.

shape(from 0 0, line to 100% 0, line to var(--cut) 100%, close) with --cut: 20% produces inside/outside alpha 255/0 at 100 px width and 255/0 at 200 px; support is true. Scaling proves that the percentage and variable resolve against the reference box.

Grammar: shape( <'fill-rule'>? from <coordinate-pair>, <shape-command># ).

from establishes the start; to supplies a reference-box point and by a segment-relative offset. Commands include move, line, hline, vline, curve, smooth, arc, and close. An x percentage uses width and y uses height. Fill rule defaults to nonzero, with evenodd available; var() and calc() work, but command lists must match for interpolation.

Examples

const render=async(width,cut,points)=>{const value='shape(from 0 0, line to 100% 0, line to '+cut+' 100%, close)';const svg='<svg xmlns="http://www.w3.org/2000/svg" width="'+width+'" height="100"><foreignObject width="'+width+'" height="100"><div xmlns="http://www.w3.org/1999/xhtml" style="--cut:'+cut+';width:'+width+'px;height:100px;background:green;clip-path:shape(from 0 0, line to 100% 0, line to var(--cut) 100%, close)"></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=100;const context=canvas.getContext('2d');context.drawImage(image,0,0);return points.map(([x,y])=>context.getImageData(x,y,1,1).data[3])};
const first=await render(100,'20%',[[20,80],[80,80]]);const second=await render(200,'20%',[[40,80],[160,80]]);
const value='shape(from 0 0, line to 100% 0, line to var(--cut) 100%, close)';
console.log('100-in/out='+first.join('/')+'; 200-in/out='+second.join('/')+'; supported='+CSS.supports('clip-path',value));

Result

100-in/out=255/0; 200-in/out=255/0; supported=true

`shape()` describes a basic shape with `CSS` commands, retaining units, calculations, and variables unavailable inside a `path()` string.

Sources