Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

path() — shape from SVG path data

`path()` creates a basic shape from one `SVG` path-data string and an optional fill rule.

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

Syntax

path( [ <'fill-rule'>, ]? <string> )

path() creates a basic shape from one SVG path-data string and an optional fill rule.

The example clips a box with path("M 10 10 L 90 10 L 50 90"): the inside and implicitly closed-edge pixels have alpha 255, while the outside pixel has 0. Empty path("") and percentage data path("M 0 0 L 100% 0 L 0 100%") both return false.

Grammar: path( [ <'fill-rule'>, ]? <string> ).

The string must be valid, non-empty SVG path data; for properties requiring a closed loop the UA adds a close operation. The fill rule defaults to nonzero, with evenodd as the alternative. Coordinates inherit SVG limitations and effectively use px: percentages and piecemeal variable construction inside the string are unavailable. Use shape() for responsive CSS units; smooth interpolation requires matching command sequences and fill rules.

Examples

const render=async value=>{const safe=value.replaceAll('"','&quot;');const svg='<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><foreignObject width="100" height="100"><div xmlns="http://www.w3.org/1999/xhtml" style="width:100px;height:100px;background:red;clip-path:'+safe+'"></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=canvas.height=100;const context=canvas.getContext('2d');context.drawImage(image,0,0);const alpha=(x,y)=>context.getImageData(x,y,1,1).data[3];return[alpha(50,40),alpha(15,80),alpha(50,85)]};
const pixels=await render('path("M 10 10 L 90 10 L 50 90")');
const empty=CSS.supports('clip-path','path("")');
const percentage=CSS.supports('clip-path','path("M 0 0 L 100% 0 L 0 100%")');
console.log('inside='+pixels[0]+'; outside='+pixels[1]+'; implicit-close='+pixels[2]+'; empty-path='+empty+'; percentage-data='+percentage);

Result

inside=255; outside=0; implicit-close=255; empty-path=false; percentage-data=false

`path()` creates a basic shape from one `SVG` path-data string and an optional fill rule.

Sources