CSS fill-rule Property
The fill-rule property determines which regions of an SVG shape are considered inside the shape and receive fill paint.
It is particularly useful for complex paths containing overlapping contours, nested shapes, or holes. The property supports two values, nonzero and evenodd, which use different methods to determine whether a point is inside or outside the filled region.
fill-rule Syntax
The fill-rule property accepts either nonzero or evenodd.
Basic Syntax
selector {
fill-rule: nonzero;
}
Supported Values
.shape {
fill-rule: nonzero;
}
.shape {
fill-rule: evenodd;
}
CSS-Wide Keywords
.shape {
fill-rule: inherit;
}
.shape {
fill-rule: initial;
}
.shape {
fill-rule: unset;
}
.shape {
fill-rule: revert;
}
.shape {
fill-rule: revert-layer;
}
The initial value is nonzero. If no fill rule is specified or inherited, SVG shapes use the nonzero winding rule.
Property Values
The fill-rule property provides two algorithms for determining which regions of a path are filled.
| Value | Description |
|---|---|
nonzero |
Uses the winding number of the path around a point. Regions with a nonzero winding number are filled. |
evenodd |
Counts path boundary crossings. Regions with an odd number of crossings are filled. |
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed fill rule of the parent element. |
initial |
Restores the initial value, nonzero. |
unset |
Uses the inherited value because the property is inherited. |
revert |
Reverts the property according to the CSS cascade origins. |
revert-layer |
Reverts the property according to the CSS cascade layers. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | nonzero |
| Inherited | Yes |
| Applies to | SVG shapes and text content elements |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentages | Not applicable |
The property is most useful when a single SVG element contains multiple contours or self-intersecting paths. For simple shapes, both values often produce the same result.
How It Works
When an SVG path contains multiple contours, the browser must determine which areas belong to the interior of the shape.
Step 1: Define a Complex Path
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<path class="example-path" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</svg>
This path contains two nested rectangular contours within one SVG path element.
Step 2: Apply Fill Paint
.example-path {
fill: #336699;
}
Step 3: Select a Fill Rule
.example-path {
fill: #336699;
fill-rule: evenodd;
}
The evenodd rule treats the area inside the inner rectangle as a hole.
Step 4: Change to nonzero
.example-path {
fill: #336699;
fill-rule: nonzero;
}
Because both rectangular contours in this example travel in the same direction, the inner region is filled when using nonzero.
Important Distinction
The fill rule changes which regions are painted. It does not change the path's geometry or the color specified by fill.
nonzero
The nonzero value uses a winding-number calculation to determine whether a point lies inside a path.
Basic Example
path {
fill-rule: nonzero;
}
How It Works
Imagine drawing a ray from a point to infinity. Each time a path crosses the ray, the winding number increases or decreases according to the direction of the crossing.
If the final winding number is not zero, the point is considered inside the path and receives fill paint.
Nested Contours
When nested contours travel in the same direction, their winding contributions reinforce each other, so the inner region remains filled.
When the inner contour travels in the opposite direction, its winding contribution can cancel the outer contour, producing a hole.
Example
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<path class="nonzero-shape" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</svg>
.nonzero-shape {
fill: #336699;
fill-rule: nonzero;
}
Both contours travel in the same direction, so the entire outer rectangle, including its center, is filled.
Practical Uses
The nonzero rule is useful for paths whose contour directions have been deliberately arranged to control filled regions and holes.
Important Consideration
The direction of each contour matters. Changing the direction of a subpath can change which regions are filled.
evenodd
The evenodd value determines whether a point is inside a shape by counting how many times a ray crosses the path boundary.
Basic Example
path {
fill-rule: evenodd;
}
How It Works
Imagine drawing a ray from a point to infinity and counting its crossings with the path boundary.
If the number of crossings is odd, the point is inside the filled region. If the number is even, the point is outside.
Nested Contours
For two nested, nonintersecting contours, the region between them is filled while the innermost region becomes a hole.
Example
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<path class="evenodd-shape" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</svg>
.evenodd-shape {
fill: #336699;
fill-rule: evenodd;
}
The inner rectangle becomes a hole because points inside it cross two contour boundaries when moving outward.
Practical Uses
The evenodd rule is convenient for creating holes in icons, symbols, and complex illustrations without needing to reverse the direction of individual contours.
Important Consideration
The evenodd rule depends on the number of crossings, not their direction.
nonzero vs. evenodd
Both values determine the interior of an SVG path, but they use different calculations.
| Behavior | nonzero | evenodd |
|---|---|---|
| Default value | Yes | No |
| Calculation | Winding number | Crossing count |
| Uses contour direction | Yes | No |
| Nested contours in the same direction | Inner region remains filled | Inner region becomes a hole |
| Nested contours in opposite directions | Inner region becomes a hole | Inner region becomes a hole |
| Typical use | Direction-controlled paths | Paths with nested holes |
When to Use nonzero
Use nonzero when path winding direction is important or when the default SVG fill behavior is appropriate.
When to Use evenodd
Use evenodd when nested contours should alternate between filled and unfilled regions regardless of contour direction.
Important Consideration
Neither rule is universally better. The correct choice depends on the geometry of the path and the intended appearance.
Path Direction and Winding
Path direction is important when using the nonzero fill rule.
Same-Direction Contours
<path d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
Both contours travel in the same direction. With nonzero, their winding contributions reinforce each other.
Opposite-Direction Contours
<path d="M20 20 H180 V180 H20 Z M60 60 V140 H140 V60 Z"/>
The inner contour travels in the opposite direction, allowing its winding contribution to cancel the outer contour within the inner region.
CSS Example
.direction-shape {
fill: #336699;
fill-rule: nonzero;
}
How Direction Affects the Result
With the nonzero rule, reversing the direction of an inner contour can turn a filled region into a hole or restore a previously unfilled region.
Important Consideration
With evenodd, reversing the direction of a contour does not change the result because only the number of boundary crossings matters.
Holes and Overlapping Paths
The fill-rule property is especially useful for compound paths that contain holes or overlapping contours.
Creating a Hole
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<path class="shape-with-hole" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</svg>
.shape-with-hole {
fill: #336699;
fill-rule: evenodd;
}
The inner rectangle is excluded from the filled region, creating a transparent opening.
Overlapping Contours
When multiple contours overlap within a single path, the fill rule determines whether the overlapping regions are painted.
Nested Contours
With evenodd, successive nested contours alternate between filled and unfilled regions.
Self-Intersecting Paths
Self-intersecting paths can produce different results under the two fill rules because their winding numbers and boundary crossing counts may differ.
Important Distinction
The fill rule is evaluated for the contours of an individual SVG element. It does not combine separate sibling SVG shapes into one compound path.
fill and fill-rule
The fill and fill-rule properties work together but control different aspects of SVG rendering.
fill
.shape {
fill: #336699;
}
Specifies the paint used inside the shape.
fill-rule
.shape {
fill-rule: evenodd;
}
Determines which regions of the path are considered inside the shape.
Combined Example
.shape {
fill: #336699;
fill-opacity: 0.8;
fill-rule: evenodd;
}
Comparison
| Property | Purpose |
|---|---|
fill |
Controls the interior paint. |
fill-opacity |
Controls the opacity of the interior paint. |
fill-rule |
Determines which regions receive the interior paint. |
Important Consideration
If fill is set to none, changing the fill rule will not produce a visible fill. The rule still defines which regions would be considered inside the shape.
fill-rule vs. clip-rule
The fill-rule and clip-rule properties use similar winding algorithms but serve different purposes.
fill-rule
.shape {
fill-rule: evenodd;
}
Determines which regions of a shape receive fill paint.
clip-rule
.clipping-shape {
clip-rule: evenodd;
}
Determines the interior of a shape when it is used to construct an SVG clipping path.
SVG Clipping Example
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="example-clip">
<path class="clipping-shape" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</clipPath>
</defs>
<rect width="200" height="200" fill="#336699" clip-path="url(#example-clip)"/>
</svg>
.clipping-shape {
clip-rule: evenodd;
}
Comparison
| Property | Purpose |
|---|---|
fill-rule |
Controls the filled interior of SVG shapes. |
clip-rule |
Controls the interior of shapes used within SVG clipping paths. |
Important Consideration
Both properties support nonzero and evenodd, but clip-rule applies to clipping geometry rather than ordinary fill painting.
Inheritance
The fill-rule property is inherited, allowing parent SVG elements or groups to establish a fill rule for their descendants.
Parent SVG Element
svg {
fill-rule: evenodd;
}
Group Inheritance
.compound-shapes {
fill-rule: evenodd;
}
HTML Example
<svg viewBox="0 0 220 120" xmlns="http://www.w3.org/2000/svg">
<g class="compound-shapes">
<path d="M10 10 H100 V110 H10 Z M35 35 H75 V85 H35 Z"/>
<path d="M120 10 H210 V110 H120 Z M145 35 H185 V85 H145 Z"/>
</g>
</svg>
Override an Individual Shape
.compound-shapes {
fill-rule: evenodd;
}
.compound-shapes .solid-shape {
fill-rule: nonzero;
}
Important Consideration
Although the property is inherited, each SVG shape evaluates its own path geometry independently. A parent group does not merge the contours of separate child shapes into a single path.
CSS vs. SVG Presentation Attribute
The fill rule can be specified through CSS or through the SVG fill-rule presentation attribute.
SVG Presentation Attribute
<path d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z" fill-rule="evenodd"/>
CSS Property
path {
fill-rule: evenodd;
}
Combined Example
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<path class="styled-path" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z" fill-rule="nonzero"/>
</svg>
.styled-path {
fill: #336699;
fill-rule: evenodd;
}
How the Cascade Works
SVG presentation attributes participate in the CSS cascade. A normal author CSS declaration can override the corresponding presentation attribute.
In this example, the CSS declaration overrides fill-rule="nonzero", so the path uses evenodd.
Recommended Approach
Use CSS when fill rules need to be controlled through shared styles, classes, or other stylesheet rules. Presentation attributes remain useful for self-contained SVG graphics.
fill-rule Example
This example compares the nonzero and evenodd fill rules using identical compound paths. Both paths contain an outer rectangle and an inner rectangle whose contours travel in the same direction.
HTML Example
<div class="fill-rule-demo">
<figure>
<svg viewBox="0 0 200 200" role="img" aria-label="Nonzero fill rule with the center filled">
<path class="nonzero-example" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</svg>
<figcaption>nonzero</figcaption>
</figure>
<figure>
<svg viewBox="0 0 200 200" role="img" aria-label="Evenodd fill rule with a transparent center">
<path class="evenodd-example" d="M20 20 H180 V180 H20 Z M60 60 H140 V140 H60 Z"/>
</svg>
<figcaption>evenodd</figcaption>
</figure>
</div>
CSS Example
.fill-rule-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 20px;
}
.fill-rule-demo figure {
margin: 0;
text-align: center;
}
.fill-rule-demo svg {
display: block;
width: 100%;
max-width: 200px;
margin: 0 auto;
}
.fill-rule-demo path {
fill: #336699;
stroke: #1f4568;
stroke-width: 3;
}
.fill-rule-demo .nonzero-example {
fill-rule: nonzero;
}
.fill-rule-demo .evenodd-example {
fill-rule: evenodd;
}
.fill-rule-demo figcaption {
margin-top: 8px;
font-size: 0.9rem;
font-weight: bold;
}
The nonzero example fills the center because both contours travel in the same direction. The evenodd example leaves the center transparent because the inner region lies inside two nested contours.
The paths have identical geometry. Only the fill-rule value changes the visible result.
Accessibility
The fill-rule property changes the visible filled regions of SVG graphics but does not change their semantic meaning.
Provide Accessible Names
Informative SVG graphics should have an accessible name or an equivalent text alternative.
Maintain Recognizable Shapes
Choose fill rules that preserve the intended appearance of icons, symbols, and other meaningful graphics.
Maintain Sufficient Contrast
Ensure that filled regions remain distinguishable from surrounding backgrounds.
Decorative Graphics
Decorative SVG graphics can use aria-hidden="true" when they do not convey additional information.
Interactive Graphics
Interactive SVG elements should remain keyboard accessible and provide visible focus indicators.
Avoid Relying on Shape Alone
If a graphical shape communicates important information, provide supporting text or another accessible indication when appropriate.
Important Consideration
Changing a fill rule can create or remove visible holes. Verify that the resulting graphic remains understandable at small sizes and under different display conditions.
Important Considerations
Keep the following points in mind when using fill-rule:
- Purpose: Determines which regions of an SVG path receive fill paint.
- Initial value:
nonzero. - Inheritance: The property is inherited.
- nonzero: Uses winding numbers to determine the interior.
- evenodd: Uses odd and even boundary crossing counts.
- Path direction: Affects
nonzerobut notevenodd. - Nested contours: Can produce filled regions or holes depending on the rule.
- Self-intersecting paths: May produce different results under the two rules.
- Simple shapes: Often look identical with either value.
- Compound paths: Multiple contours within one SVG element are evaluated together.
- Separate shapes: Sibling SVG elements do not become one compound path.
- fill: Determines the paint applied to the regions selected by the fill rule.
- fill-opacity: Controls the opacity of that paint.
- clip-rule: Uses similar algorithms for SVG clipping geometry.
- Presentation attributes: SVG
fill-ruleattributes participate in the CSS cascade. - Animations: Changes between the two keyword values are discrete.
- Accessibility: Ensure that the resulting shapes remain recognizable and understandable.
Use evenodd when nested contours should create holes regardless of direction. Use nonzero when winding direction is part of the intended path design.
Browser Support
The CSS fill-rule property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.
Both nonzero and evenodd have longstanding support in SVG rendering.
Supported Example
svg path {
fill-rule: evenodd;
}
Feature Detection
@supports (fill-rule: evenodd) {
svg path {
fill-rule: evenodd;
}
}
Compatibility Considerations
The fill rule is most relevant to SVG paths with multiple contours or self-intersections. Support for basic SVG fill rules is consistent across modern browsers, but complex graphics should still be tested to confirm their intended appearance.
For current compatibility information, visit Can I Use? .
Specifications
The fill-rule property is defined in SVG 2, which describes how winding rules determine the interior of SVG shapes.
The CSS Fill and Stroke Module also describes fill painting and related behavior.
CSS Fill and Stroke Module Level 3
These specifications define the nonzero and evenodd algorithms, inheritance, and the relationship between CSS properties and SVG presentation attributes.
