clip-rule

Baseline 已广泛支持

此特性已经十分成熟,可在许多设备和浏览器版本上使用。自 2020 年 7 月以来,它已在各大浏览器中可用。

clip-rule 属性仅适用于包含在 <clipPath> 元素内的图形元素。clip-rule 属性的工作方式基本与 fill-rule 属性相同,只是它适用于 <clipPath> 定义。

注意: 作为一个呈现属性,clip-rule 还有一个对应的 CSS 属性:clip-rule。当两者都被指定时,CSS 属性具有更高的优先级。

以下代码片段将因为在定义裁剪形状的 <path> 元素上指定了 clip-rule 而导致应用 evenodd 裁剪规则。

html
<g>
  <clipPath id="MyClip">
    <path d="..." clip-rule="evenodd" />
  </clipPath>
  <rect clip-path="url(#MyClip)" ... />
</g>

而以下代码片段不会应用 evenodd 裁剪规则,因为 clip-rule 是在引用元素上指定的,而不是在定义裁剪形状的对象上。

html
<g>
  <clipPath id="MyClip">
    <path d="..." />
  </clipPath>
  <rect clip-path="url(#MyClip)" clip-rule="evenodd" ... />
</g>

用法说明

nonzero | evenodd | inherit
默认值 nonzero
可动画的
nonzero

请参阅 fill-rule 属性的描述。

evenodd

请参阅 fill-rule 属性的描述。

示例

html
<svg
  width="100"
  viewBox="0 0 100 90"
  xmlns="http://www.w3.org/2000/svg"
  version="1.1">
  <!-- Define star path -->
  <defs>
    <path d="M50,0 21,90 98,35 2,35 79,90z" id="star" />
  </defs>

  <!-- Left: evenodd -->
  <clipPath id="emptyStar">
    <use href="#star" clip-rule="evenodd" />
  </clipPath>
  <rect clip-path="url(#emptyStar)" width="50" height="90" fill="blue" />

  <!-- Right: nonzero -->
  <clipPath id="filledStar">
    <use href="#star" clip-rule="nonzero" />
  </clipPath>
  <rect clip-path="url(#filledStar)" width="50" height="90" x="50" fill="red" />
</svg>

元素

以下元素可以使用 clip-rule 属性,但前提是它们必须位于 <clipPath> 元素内部。

规范

规范
CSS 蒙版模块 Level 1
# the-clip-rule

浏览器兼容性

另见