How SVG Fill Controls Vector Shape Interior Colors

The SVG fill property defines the interior color, gradient, pattern, or transparency of scalable vector shapes. By targeting the enclosed paths of elements such as <circle>, <rect>, <path>, and <polygon>, the fill property determines how vector geometry renders inside its boundary lines. This guide explains the core mechanisms of the fill property, including supported color values, CSS integration, opacity controls, and the fill-rule algorithm for complex shapes.

Understanding the Fill Mechanism

Vector graphics consist of coordinates and path commands that define borders. The fill property tells the SVG renderer what paint source to apply inside those enclosed coordinates. By default, most SVG shapes use a solid black fill (#000000) if no fill attribute is explicitly defined.

Supported Color Formats

The fill property accepts any standard CSS color value, special keywords, or references to SVG paint servers:

Presentation Attributes vs. CSS Styling

The fill property can be declared in two primary ways:

  1. As an SVG Presentation Attribute:

    <rect width="100" height="50" fill="#10b981" />
  2. Through Inline or External CSS:

    .my-shape {
      fill: #10b981;
      transition: fill 0.3s ease;
    }

    Applying fill through CSS overrides presentation attributes and allows interactive behaviors, such as dynamic state changes on :hover or smooth color transitions.

Managing Transparency with Fill Opacity

To control the transparency of a shape’s interior without altering the opacity of its outline (stroke), use the fill-opacity property:

<rect width="100" height="100" fill="#e11d48" fill-opacity="0.5" stroke="#000" stroke-width="4" />

Values range from 0.0 (fully transparent) to 1.0 (fully opaque). This keeps the stroke crisp and solid while making the inner area translucent.

Determining Complex Interiors with fill-rule

For complex paths that self-intersect or contain nested cutouts (such as donut shapes or icons), the browser uses the fill-rule property to determine which areas are considered “inside” the shape: