CSS fill-opacity Property

The fill-opacity property controls the opacity of the paint used to fill the interior of an SVG shape or text element.

It accepts numbers or percentages to determine how transparent the fill appears. Unlike the opacity property, fill-opacity affects only the fill paint, allowing the outline of a shape to remain fully visible.

fill-opacity Syntax

The fill-opacity property accepts a number or percentage representing the opacity of the fill paint.

Basic Syntax

selector {
  fill-opacity: 0.5;
}

Number Values

.shape {
  fill-opacity: 1;
}
.shape {
  fill-opacity: 0.75;
}
.shape {
  fill-opacity: 0.5;
}
.shape {
  fill-opacity: 0;
}

Percentage Values

.shape {
  fill-opacity: 100%;
}
.shape {
  fill-opacity: 75%;
}
.shape {
  fill-opacity: 50%;
}
.shape {
  fill-opacity: 0%;
}

CSS-Wide Keywords

.shape {
  fill-opacity: inherit;
}
.shape {
  fill-opacity: initial;
}
.shape {
  fill-opacity: unset;
}
.shape {
  fill-opacity: revert;
}
.shape {
  fill-opacity: revert-layer;
}

The initial value is 1, equivalent to 100%, which makes the fill paint fully opaque.

Property Values

The fill-opacity property uses numeric values or percentages to control fill transparency.

Value Description
<number> A numeric opacity value, where 0 is fully transparent and 1 is fully opaque.
<percentage> A percentage opacity value, where 0% is fully transparent and 100% is fully opaque.

Common Values

Number Percentage Appearance
1 100% Fully opaque fill
0.75 75% Slightly transparent fill
0.5 50% Half-transparent fill
0.25 25% Mostly transparent fill
0 0% Fully transparent fill

CSS-Wide Values

Value Description
inherit Uses the computed value of the parent element.
initial Restores the initial value, 1.
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 1
Inherited Yes
Applies to SVG shapes and text content elements
Computed value Specified number or percentage
Animation type By computed value type
Percentages Relative to the opacity range from 0 to 1

For rendering, values outside the range from 0 to 1, or 0% to 100%, are clamped to that range.

How It Works

The fill-opacity property adjusts the transparency of the paint applied by fill.

Step 1: Create an SVG Shape

<svg viewBox="0 0 200 120" xmlns="http://www.w3.org/2000/svg">
  <rect class="example-shape" x="20" y="20" width="160" height="80"/>
</svg>

Step 2: Apply a Fill Color

.example-shape {
  fill: #336699;
}

The rectangle is filled with blue at full opacity.

Step 3: Reduce the Fill Opacity

.example-shape {
  fill: #336699;
  fill-opacity: 0.5;
}

The rectangle becomes half-transparent, allowing the background behind it to show through.

Step 4: Add an Opaque Outline

.example-shape {
  fill: #336699;
  fill-opacity: 0.5;
  stroke: #1f4568;
  stroke-width: 4;
}

The fill remains half-transparent while the outline retains its normal opacity.

Important Distinction

The property changes the opacity of fill paint, not the shape's geometry, dimensions, or stroke opacity.

Number Values

Number values provide a convenient way to specify fill opacity using decimals.

Fully Opaque

.shape {
  fill-opacity: 1;
}

The fill paint is fully opaque, assuming the fill color or paint source is itself opaque.

Half-Transparent

.shape {
  fill-opacity: 0.5;
}

The fill paint uses 50% opacity.

Mostly Transparent

.shape {
  fill-opacity: 0.2;
}

The fill paint uses 20% opacity.

Fully Transparent

.shape {
  fill-opacity: 0;
}

The fill paint becomes fully transparent.

Values Outside the Range

.shape {
  fill-opacity: 1.5;
}

A value greater than 1 is clamped to full opacity for rendering. Negative values are similarly clamped to zero.

Recommended Approach

Use values between 0 and 1 to make the intended opacity clear.

Percentage Values

Percentage values provide an alternative way to express fill opacity.

Fully Opaque

.shape {
  fill-opacity: 100%;
}

Half-Transparent

.shape {
  fill-opacity: 50%;
}

Mostly Transparent

.shape {
  fill-opacity: 20%;
}

Fully Transparent

.shape {
  fill-opacity: 0%;
}

Number and Percentage Equivalents

Number Percentage
1 100%
0.8 80%
0.6 60%
0.4 40%
0.2 20%
0 0%

Important Consideration

Number and percentage values produce equivalent results when they represent the same opacity. Percentage syntax may require a more recent browser implementation than traditional numeric syntax.

fill-opacity and fill

The fill property selects the paint, while fill-opacity determines how opaque that paint appears.

Solid Fill with Reduced Opacity

.shape {
  fill: #336699;
  fill-opacity: 0.5;
}

Different Fill Color

.shape {
  fill: #e67e22;
  fill-opacity: 0.5;
}

No Fill

.shape {
  fill: none;
  fill-opacity: 0.5;
}

When fill is none, no interior paint is applied. Changing fill-opacity does not create a visible fill.

Comparison

Property Purpose
fill Determines the paint used inside the SVG shape.
fill-opacity Controls the opacity of that fill paint.

Important Consideration

The visible result also depends on the alpha channel of the fill color and any additional opacity applied to the element or its ancestors.

fill-opacity vs. opacity

The fill-opacity and opacity properties both affect transparency, but they operate at different stages of rendering.

fill-opacity

.shape {
  fill: #336699;
  fill-opacity: 0.5;
  stroke: #1f4568;
  stroke-width: 4;
}

Only the fill paint is reduced to 50% opacity. The stroke remains fully opaque.

opacity

.shape {
  fill: #336699;
  stroke: #1f4568;
  stroke-width: 4;
  opacity: 0.5;
}

The entire rendered element is composited at 50% opacity, including both its fill and stroke.

Comparison

Behavior fill-opacity opacity
Affects fill paint Yes Yes
Affects stroke paint No Yes
Inherited Yes No
Applies to ordinary HTML elements No visual effect on ordinary HTML boxes Yes

Recommended Approach

Use fill-opacity when only the interior paint should be transparent. Use opacity when the complete rendered element should be transparent.

fill-opacity vs. stroke-opacity

The fill-opacity and stroke-opacity properties independently control the transparency of the interior and outline of an SVG shape.

Transparent Fill

.shape {
  fill: #336699;
  fill-opacity: 0.3;
  stroke: #1f4568;
  stroke-opacity: 1;
  stroke-width: 4;
}

Transparent Stroke

.shape {
  fill: #336699;
  fill-opacity: 1;
  stroke: #1f4568;
  stroke-opacity: 0.3;
  stroke-width: 4;
}

Both Transparent

.shape {
  fill: #336699;
  fill-opacity: 0.5;
  stroke: #1f4568;
  stroke-opacity: 0.5;
  stroke-width: 4;
}

Comparison

Property Purpose
fill-opacity Controls the opacity of the interior paint.
stroke-opacity Controls the opacity of the outline paint.
opacity Controls the opacity of the rendered element as a whole.

Practical Uses

Independent fill and stroke opacity settings are useful for charts, diagrams, outlined icons, and overlapping SVG shapes.

Transparent Fill Colors

CSS color values can include an alpha channel that controls their transparency. This can be combined with fill-opacity.

RGBA-Style Color

.shape {
  fill: rgb(51 102 153 / 50%);
}

Fill Opacity

.shape {
  fill: #336699;
  fill-opacity: 0.5;
}

Combining Both

.shape {
  fill: rgb(51 102 153 / 50%);
  fill-opacity: 0.5;
}

In this example, the color's alpha channel and the fill opacity are combined, producing an effective fill alpha of 25% before any additional element opacity is applied.

Transparent vs. No Fill

.transparent-shape {
  fill: transparent;
}
.unpainted-shape {
  fill: none;
}

A transparent fill still specifies paint with zero alpha, while none specifies no fill paint. This difference can matter for SVG pointer-event hit testing.

Important Consideration

When both color alpha and fill-opacity are used, remember that their effects combine. Avoid unintentionally making a fill more transparent than intended.

Gradients and Patterns

The fill-opacity property can also reduce the opacity of SVG gradients and patterns used as fill paint.

Gradient Example

<svg viewBox="0 0 220 120" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="opacity-gradient">
      <stop offset="0%" stop-color="#336699"/>
      <stop offset="100%" stop-color="#66ccff"/>
    </linearGradient>
  </defs>
  <rect class="gradient-shape" x="20" y="20" width="180" height="80"/>
</svg>
.gradient-shape {
  fill: url("#opacity-gradient");
  fill-opacity: 0.5;
}

Pattern Example

.pattern-shape {
  fill: url("#my-pattern");
  fill-opacity: 0.4;
}

How It Works

The opacity is applied to the resulting fill paint, whether that paint comes from a solid color, gradient, or pattern.

Gradient Stop Opacity

<linearGradient id="transparent-gradient">
  <stop offset="0%" stop-color="#336699" stop-opacity="1"/>
  <stop offset="100%" stop-color="#66ccff" stop-opacity="0.2"/>
</linearGradient>

The SVG stop-opacity property controls individual gradient stops, while fill-opacity controls the opacity of the resulting fill paint.

Important Consideration

When gradient stops already contain transparency, fill-opacity further reduces their resulting opacity.

Inheritance

The fill-opacity property is inherited, allowing parent SVG elements and groups to establish a fill opacity for their descendants.

Parent SVG Element

svg {
  fill: #336699;
  fill-opacity: 0.5;
}

Group Inheritance

.shape-group {
  fill-opacity: 0.4;
}

HTML Example

<svg viewBox="0 0 220 120" xmlns="http://www.w3.org/2000/svg">
  <g class="shape-group">
    <circle cx="65" cy="60" r="40" fill="#336699"/>
    <rect x="115" y="20" width="80" height="80" fill="#e67e22"/>
  </g>
</svg>

Override an Individual Shape

.shape-group {
  fill-opacity: 0.4;
}
.shape-group .highlight {
  fill-opacity: 1;
}

Important Distinction

Inherited fill-opacity is applied to the fill paint of each descendant. This differs from applying opacity to a group, which composites the rendered group as a whole.

CSS vs. SVG Presentation Attribute

The fill opacity of an SVG element can be specified through CSS or through the SVG fill-opacity presentation attribute.

SVG Presentation Attribute

<circle cx="50" cy="50" r="35" fill="#336699" fill-opacity="0.5"/>

CSS Property

circle {
  fill-opacity: 0.75;
}

Combined Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle class="styled-circle" cx="50" cy="50" r="35" fill="#336699" fill-opacity="0.5"/>
</svg>
.styled-circle {
  fill-opacity: 0.8;
}

How the Cascade Works

SVG presentation attributes participate in the CSS cascade. A normal author CSS declaration can override a corresponding presentation attribute.

In this example, the CSS declaration overrides the attribute, so the circle uses a fill opacity of 0.8.

Recommended Approach

Use CSS when opacity needs to change through hover states, classes, media queries, or animations. Presentation attributes remain useful for self-contained SVG graphics.

Animations and Transitions

The fill-opacity property can be animated to create gradual changes in SVG fill transparency.

Hover Transition

.interactive-shape {
  fill: #336699;
  fill-opacity: 1;
  transition: fill-opacity 0.3s ease;
}
.interactive-shape:hover {
  fill-opacity: 0.4;
}

Keyframe Animation

.animated-shape {
  animation: fade-fill 2s ease-in-out infinite alternate;
}
@keyframes fade-fill {
  from {
    fill-opacity: 1;
  }
  to {
    fill-opacity: 0.2;
  }
}

Combined Fill and Opacity Transition

.interactive-shape {
  fill: #336699;
  fill-opacity: 1;
  transition: fill 0.3s ease, fill-opacity 0.3s ease;
}
.interactive-shape:hover {
  fill: #e67e22;
  fill-opacity: 0.5;
}

Reduced Motion

@media (prefers-reduced-motion: reduce) {
  .animated-shape {
    animation: none;
  }
  .interactive-shape {
    transition: none;
  }
}

Important Consideration

Opacity animations should remain subtle and should not cause important graphical information to become unreadable or disappear unexpectedly.

fill-opacity Example

This example compares four SVG circles with different fill opacity values. Each circle has the same fill color and an opaque outline.

HTML Example

<div class="fill-opacity-demo">
  <figure>
    <svg viewBox="0 0 120 100" role="img" aria-label="Circle with 100 percent fill opacity">
      <circle class="opacity-100" cx="60" cy="50" r="35"/>
    </svg>
    <figcaption>100% Opacity</figcaption>
  </figure>
  <figure>
    <svg viewBox="0 0 120 100" role="img" aria-label="Circle with 75 percent fill opacity">
      <circle class="opacity-75" cx="60" cy="50" r="35"/>
    </svg>
    <figcaption>75% Opacity</figcaption>
  </figure>
  <figure>
    <svg viewBox="0 0 120 100" role="img" aria-label="Circle with 50 percent fill opacity">
      <circle class="opacity-50" cx="60" cy="50" r="35"/>
    </svg>
    <figcaption>50% Opacity</figcaption>
  </figure>
  <figure>
    <svg viewBox="0 0 120 100" role="img" aria-label="Circle with 25 percent fill opacity">
      <circle class="opacity-25" cx="60" cy="50" r="35"/>
    </svg>
    <figcaption>25% Opacity</figcaption>
  </figure>
</div>

CSS Example

.fill-opacity-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 15px;
}
.fill-opacity-demo figure {
  margin: 0;
  text-align: center;
}
.fill-opacity-demo svg {
  display: block;
  width: 100%;
  max-width: 120px;
  margin: 0 auto;
}
.fill-opacity-demo circle {
  fill: #336699;
  stroke: #1f4568;
  stroke-width: 4;
}
.fill-opacity-demo .opacity-100 {
  fill-opacity: 1;
}
.fill-opacity-demo .opacity-75 {
  fill-opacity: 0.75;
}
.fill-opacity-demo .opacity-50 {
  fill-opacity: 0.5;
}
.fill-opacity-demo .opacity-25 {
  fill-opacity: 0.25;
}
.fill-opacity-demo figcaption {
  margin-top: 8px;
  font-size: 0.9rem;
}
Browser Result
100% Opacity
75% Opacity
50% Opacity
25% Opacity

All four circles use the same blue fill and dark blue stroke. As the fill-opacity value decreases, the interior becomes more transparent while the outline remains fully opaque.

This demonstrates how fill transparency can be adjusted independently of the stroke.

Accessibility

The fill-opacity property changes the appearance of SVG graphics but does not alter their semantic meaning.

Maintain Sufficient Contrast

Reducing fill opacity can lower contrast against the background. Ensure that important shapes and graphical indicators remain distinguishable.

Do Not Rely on Transparency Alone

When opacity communicates a state such as disabled, selected, or inactive, provide another visual or textual indicator.

Accessible SVG Graphics

Informative SVG graphics should include an accessible name or equivalent text alternative.

Decorative Graphics

Decorative SVG elements can use aria-hidden="true" when they do not convey additional information.

Interactive Graphics

Ensure that interactive SVG elements remain keyboard accessible and have visible focus indicators.

Avoid Disappearing Content

Do not reduce the opacity of essential information so far that visitors cannot reliably perceive it.

Animation Considerations

Avoid rapid flashing or repeated opacity changes that could distract visitors or create accessibility problems.

Important Considerations

Keep the following points in mind when using fill-opacity:

  • Purpose: Controls the opacity of SVG fill paint.
  • Initial value: 1, equivalent to 100%.
  • Inheritance: The property is inherited.
  • Number values: Commonly range from 0 to 1.
  • Percentage values: Commonly range from 0% to 100%.
  • Clamping: Values outside the valid opacity range are clamped for rendering.
  • Fully opaque: 1 applies full fill opacity.
  • Fully transparent: 0 makes the fill paint fully transparent.
  • fill: Determines the paint whose opacity is controlled.
  • fill: none: Produces no fill paint regardless of the fill opacity value.
  • stroke: Remains unaffected by fill-opacity.
  • stroke-opacity: Controls outline opacity separately.
  • opacity: Affects the rendered element as a whole.
  • Color alpha: Combines with fill opacity to determine the resulting transparency.
  • Gradients: Fill opacity can reduce the opacity of gradient paint.
  • Patterns: Fill opacity can reduce the opacity of pattern paint.
  • Inheritance: SVG descendants can inherit fill opacity from a parent or group.
  • Animations: Numeric opacity values can transition smoothly.
  • Accessibility: Reduced opacity should not make important information difficult to perceive.

Use fill-opacity when the interior of an SVG shape should be transparent without changing its outline. For transparency affecting the complete element, use the opacity property instead.

Browser Support

The CSS fill-opacity property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.

Numeric opacity values have longstanding support. Percentage values are supported in modern implementations, although older browsers may require numeric syntax.

Supported Example

svg circle {
  fill: #336699;
  fill-opacity: 0.5;
}

Feature Detection

@supports (fill-opacity: 50%) {
  svg circle {
    fill-opacity: 50%;
  }
}

Compatibility Considerations

For maximum compatibility, use numeric values such as 0.5 instead of percentages when older browser support is important.

For current compatibility information, visit Can I Use? .

Specifications

The fill-opacity property is defined in SVG 2, which describes how opacity is applied to SVG fill paint.

SVG 2: fill-opacity Property

The CSS Fill and Stroke Module also describes fill and stroke paint behavior, including opacity.

CSS Fill and Stroke Module Level 3

These specifications describe opacity values, inheritance, paint rendering, and the relationship between CSS properties and SVG presentation attributes.