Clip-rule - CSS Property

clip-rule

Summary of characteristics of the clip-rule property

Quick description
Defines how to handle hollow shapes in the context of a clipping.
Status
Standard
Applies to
All elements.
Predefined values
nonzero | evenodd
Percentages
Not applicable.
Initial value
nonzero
Inherited by default
Yes.
Discrète: during an animation, the clip-rule property passes from one value to another without transition.
Single: single value (no order).
W3C Module
CSS Masking Module
Document status: CR (Candidate Recommendation)

Syntax diagram of clip-rule.

Syntax diagram of clip-rule
Syntax diagram of the clip-rule property.
Download the diagram in SVG.

Description of the clip-rule property.

clip-rule defines how a shape should be filled when its outline overlaps itself. Basically, should the shape be hollow, or fully filled? clip-rule comes into play when the SVG shape is used to mask an element.
The drawings below show this using a star shape (defined in SVG) used to mask an element that contains the Firefox logo image.

Shape used for cutting (defined in SVG).
Example for clip-rule
clip-rule:nonzero
Example for clip-rule
clip-rule:evenodd

Warning! clip-rule must be applied to the object that defines the cut shape (most often an SVG object) and not to the element that uses this shape.

clip-rule has no effect if the shape used for the cut is a basic shape (circle(), inset(), polygon(), etc).

Special case of included forms.

If the shape is made up of two closed paths, with one inside the other, the outline never overlaps. The shape will be considered hollow if one path is drawn clockwise and the other counterclockwise. The shape will be hollow regardless of the value assigned to the clip-rule property. On the other hand, if both paths are in the same one, the property clip-rule determines if the shape is hollow.

Example for the clip-rule property
Both circles are drawn in the same direction: the shape will only be hollow if clip-rule has the value evenodd.
Example for the clip-rule property
The two circles aren't drawn in the same direction: the shape will always be hollow, no matter what the value of the property clip-rule is.

SVG code for the first figure:

svg xmlns="http://www.w3.org/2000/svg" clipPath id="star-evenodd" clip-rule="evenodd" path d="M100 10 L40 198 L190 78 L10 78 L160 198 Z" / /clipath /svg

SVG code for the second figure:

svg xmlns="http://www.w3.org/2000/svg" clipPath id="star-nonzero" clip-rule="nonzero" path d="M100 10 L40 198 L190 78 L10 78 L160 198 Z" / /clipath /svg

Values for clip-rule.

  • clip-rule: nonzero;

    Default value. The shape doesn’t have any holes, even if its outline overlaps.

  • clip-rule: evenodd;

    The shape can be pierced, depending on whether the outline overlaps itself or not, or depending on whether one shape is included within another.

  • clip-rule: initial; (nonzero) clip-rule: inherit; clip-rule: revert; clip-rule: revert-layer; clip-rule: revert-rule; clip-rule: unset;

    These values ​​are described in more detail on their respective page: initial, inherit, revert, revert-layer, revert-rule, unset.

Animation of the clip-rule property.

Because clip-rule accepts only two values, the animation is relatively simple.

Animation of the clip-rule property

Access the clip-rule property via a program

With Javascript, change the value of clip-rule.

The value of the property clip-rule can be changed in JavaScript. Here are two code examples, knowing that JS accepts the property name written in kebab-case, which means a dash to separate words (like in CSS), or in camel-case: a capital letter to separate words.

Javascript
let el = document.getElementById('id'); el.style['clip-rule'] = 'nonzero'; // or let el = document.getElementById('id'); el.style.clipRule = 'nonzero';

With Javascript, read the value of clip-rule.

The property must have been set on the element by the Javascript code above or directly in the HTML code using the style attribute, and not in CSS via a selector.

Javascript
let el = document.getElementById('id'); el.style['clip-rule'] = 'nonzero'; // or let el = document.getElementById('id'); el.style.clipRule = 'nonzero';

With Javascript, read the computed value of clip-rule.

The computed value is either the value applied to the property via the style attribute, or through a CSS selector, or the value determined by inheritance, or even the initial value of the property: nonzero for clip-rule. The computed value is always defined.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('clip-rule');

With JQuery, read or change the value of clip-rule.

JQuery is a Javascript library. It generally has shorter syntax than Javascript.

JQuery

// Read the value:
$('#id').prop('clip-rule');
// Write the value:
$('#id').prop('clip-rule', 'nonzero');
$('#id').prop('{clip-rule': 'nonzero'});

With JQuery, read the computed value of clip-rule.

JQuery
let value = $('#id').css('clip-rule');

Other code examples.

For other code examples on handling properties with JavaScript and jQuery, see the page Javascript and CSS.

Test it yourself.

The buttons below apply the entered value to the clip-rule property and then display either the value as it was applied or the computed value. In the case of clip-rule, it will be the same since the property doesn't accept numeric values. However, if you try to apply an unknown value, the assigned value will be empty, while the computed value will be the property's initial value.

Browsers compatibility with clip-rule.

The property clip-rule is properly handled by browsers. Internet Explorer   doesn't handle it, but that browser is no longer used.

Column 1
Support for the property clip-rule to determine if a shape is filled or not.
1
clip-rule
property
Estimated overall support.
95%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Safari

Safari sur IOS

Opéra

Chrome

Edge

Firefox

Chrome pour Androïd

Samsung Internet

Androïd Brower

Firefox pour Androïd

KaiOS Browser

Opéra mini

Historic of the clip-rule property.

  • CSS Masking Module Level 1

    This specification module describes two ways to partially or completely hide portions of visual elements: masking and clipping.
    Masking involves using another graphic element or an image as a mask.
    Clipping describes the visible region of visual elements. The region can be described using certain SVG graphic elements or simple shapes. Anything outside this region is not rendered.

    Regarding clip-rule Introduction of masking techniques in the CSS standard, and definition of the clip-rule property.
    WD
    November 15, 2012
    Working Draft.
    CR
    August 26, 2014
    Candidate Recommendation.
    PR
    REC

See also, about masking techniques.

The specification module that gathers everything related to masking is called CSS Masking Module. The property clip-rule and the properties below are described in this module:

Properties:

clip
Langue française
Cuts an element from a rectangle.
clip-path
Defines the visible part of the element, delimited by a geometric shape (rectangle, polygon, etc.).
Clip-rule
Defines how to handle hollow shapes in the context of a clipping.
mask
Langue française
A shorthand property that defines the masking parameters: mask dimensions, position, repeat, origin, etc.
mask-border
Langue française
Short hand property. Sets all the parameters of a mask for the border of an element.
mask-border-mode
Langue française
Defines which parameter the mask should act on (luminance, alpha).
mask-border-outset
Langue française
Defines how offset the border mask is from the clip.
mask-border-repeat
Langue française
Sets the pattern in which the mask repeats.
mask-border-slice
Langue française
Defines how the image used as a mask should be partitioned.
mask-border-source
Langue française
Refers to the image that will be used as a mask (border mask)
mask-border-width
Langue française
Sets the border thickness for applying a mask.
mask-clip
Langue française
Defines the area to be masked.
mask-composite
Langue française
Defines how masks will be combined when multiple are applied to an element.
mask-image
Langue française
Specifies the image or SVG file that will be used as a mask.
mask-mode
Langue française
Sets the masking mode (alpha channel, luminance...)
mask-origin
Langue française
Sets the reference for the positioning of the mask.
mask-position
Langue française
Defines the position of the mask.
mask-position-x
Langue française
Defines the horizontal position of the mask.
mask-position-y
Langue française
Defines the vertical position of the mask.
mask-repeat
Langue française
Sets how the mask repeats when the mask is smaller than the element to be masked.
mask-size
Langue française
Fixe les dimensions du masque.
mask-type
Langue française
Defines which mask parameter is used to hide the element.