CSS

clip-path

clip-path

这是一种实验技术

由于该技术的规格不稳定,请查看各种浏览器的兼容性表格以供使用。另外请注意,随着规范的变化,实验技术的语法和行为在未来版本的浏览器中可能会发生变化。

clip-path属性创建一个裁剪区域,该区域定义应显示元素的哪个部分。区域内的部分显示,而外部的部分被隐藏。裁剪区域是指定为引用内联或外部SVG的URL或形状(如圆圈%28%29...

历史注意:剪辑路径属性替换了弃用的clip属性。

/* Keyword values */ clip-path: none; /* Image values */ clip-path: url(resources.svg#c1 /* Box values */ clip-path: fill-box; clip-path: stroke-box; clip-path: view-box; clip-path: margin-box; clip-path: border-box; clip-path: padding-box; clip-path: content-box; /* Geometry values */ clip-path: inset(100px 50px clip-path: circle(50px at 0 100px clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50% /* Box and geometry values combined */ clip-path: padding-box circle(50px at 0 100px /* Global values */ clip-path: inherit; clip-path: initial; clip-path: unset;

初始值 none
适用于所有元素; 在SVG中,它适用于不包含<defs>元素和所有图形元素的容器元素
遗传没有
百分比指定时参考参考框,否则为边框
媒体 visual
计算值如指定的,但与绝对的url值
动画类型是的,如<basic-shape>所指定的,否则不
规范的顺序形式语法定义的独特的非模糊顺序

语法

clip-path属性被指定为下面列出的值中的一个或其组合。

url()表示引用裁剪路径元素的URL。

形式语法

<clip-source> | [ <basic-shape> || <geometry-box> ] | nonewhere <clip-source> = <url> <basic-shape> = <inset()> | <circle()> | <ellipse()> | <polygon()> <geometry-box> = <shape-box> | fill-box | stroke-box | view-box where <inset()> = inset( <length-percentage>{1,4} [ round <border-radius> ]? ) <circle()> = circle( [ <shape-radius> ]? [ at <position> ]? ) <ellipse()> = ellipse( [ <shape-radius>{2} ]? [ at <position> ]? ) <polygon()> = polygon( <fill-rule>? , [ <length-percentage> <length-percentage> ]# ) <shape-box> = <box> | margin-box where <length-percentage> = <length> | <percentage> <shape-radius> = <length-percentage> | closest-side | farthest-side <position> = [[ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] | [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]] <fill-rule> = nonzero | evenodd <box> = border-box | padding-box | content-box

实例

/* Inline SVG */ .target { clip-path: url(#c1 } /* External SVG */ .anothertarget { clip-path: url(resources.svg#c1 } /* Circle shape */ .circleClass { clip-path: circle(15px at 20px 20px }

示例

HTML

<img id="clipped" src="https://mdn.mozillademos.org/files/12668/MDN.svg" alt="MDN logo"> <svg height="0" width="0"> <defs> <clipPath id="cross"> <rect y="110" x="137" width="90" height="90"/> <rect x="0" y="110" width="90" height="90"/> <rect x="137" y="0" width="90" height="90"/> <rect x="0" y="0" width="90" height="90"/> </clipPath> </defs> </svg> <select id="clipPath">   <option value="none">none</option>   <option value="circle(100px at 110px 100px)">circle</option>   <option value="url(#cross)" selected>cross</option>   <option value="inset(20px round 20px)">inset</option> </select>

CSS

#clipped { margin-bottom: 20px; clip-path: url(#cross }

结果

规范

SpecificationStatusComment
CSS Masking Module Level 1The definition of 'clip-path' in that specification.Candidate RecommendationExtends its application to HTML elements.
Scalable Vector Graphics (SVG) 1.1 (Second Edition)The definition of 'clip-path' in that specification.RecommendationInitial definition (applies to SVG elements only).

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
On HTML elements24-webkit1 55.0No support23.5 (1.9.1)No support(Yes)-webkit1No support3
<basic-shape>55.0No support254 (54)5No support42.0No support3
On SVG content55.020 (12.10240)452 (52)6(Yes)442.0No support3
inset()55.0No support254 (54)5No support42.0No support3
Animations55.0No support249 (49)No support42.0No support3
fill-box and stroke-boxNo supportNo support251 (51)7No supportNo supportNo support3

FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari MobileChrome for Android
On HTML elements55.055.0No support1.0 (1.9.1)No support?No support3(Yes) -webkit 55.0
<basic-shape>55.055.0No support54.0 (54)5No support?No support355.0
On SVG content55.055.020 (12.10240)452.0 (52)6No support?No support355.0
inset()55.055.0No support54.0 (54)5No support?No support355.0
Animations55.055.0No support49.0 (49)No support?No support355.0
fill-box and stroke-boxNo supportNo supportNo support51 (51)7No support?No support3No support