CSS stroke-opacity Property
The CSS stroke-opacity property controls the transparency of the stroke used to draw an SVG element's outline.
It allows a stroke to appear fully opaque, partially transparent, or completely transparent without changing the element's fill. The property is useful for SVG icons, charts, diagrams, illustrations, and layered graphics where the outline needs a different transparency level from the interior.
stroke-opacity Syntax
The stroke-opacity property accepts a number or percentage representing the stroke's opacity.
Basic Syntax
svg path {
stroke-opacity: 0.5;
}
Number Values
svg path {
stroke-opacity: 0;
}
svg path {
stroke-opacity: 0.25;
}
svg path {
stroke-opacity: 0.5;
}
svg path {
stroke-opacity: 0.75;
}
svg path {
stroke-opacity: 1;
}
Percentage Values
svg path {
stroke-opacity: 0%;
}
svg path {
stroke-opacity: 25%;
}
svg path {
stroke-opacity: 50%;
}
svg path {
stroke-opacity: 75%;
}
svg path {
stroke-opacity: 100%;
}
Numbers and percentages provide equivalent opacity levels. For example, 0.5 and 50% both represent 50% opacity.
CSS-Wide Keywords
svg path {
stroke-opacity: initial;
}
svg path {
stroke-opacity: inherit;
}
svg path {
stroke-opacity: unset;
}
svg path {
stroke-opacity: revert;
}
svg path {
stroke-opacity: revert-layer;
}
The initial value is 1, meaning the stroke is fully opaque unless another transparency setting affects it.
Property Values
| Value | Description |
|---|---|
<number> |
A numeric opacity value. Values from 0 to 1 represent the usual transparency range. |
<percentage> |
A percentage opacity value. 0% is fully transparent and 100% is fully opaque. |
initial |
Resets the property to 1. |
inherit |
Uses the computed stroke opacity value from the parent element. |
unset |
Uses the inherited value because the property is inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 1 |
| Inherited | Yes |
| Applies to | Applicable SVG shapes, paths, and text elements |
| Computed value | Specified number or percentage, clamped to the range from 0 to 1 |
| Animation type | By computed value type |
| Percentages | Relative to full opacity |
| Related properties | stroke, stroke-width, fill-opacity, opacity |
| Specification | SVG 2 |
Valid Examples
svg path {
stroke-opacity: 0.4;
}
svg circle {
stroke-opacity: 80%;
}
svg rect {
stroke-opacity: 1;
}
svg line {
stroke-opacity: calc(0.25 + 0.25);
}
svg polygon {
stroke-opacity: 150%;
}
Values outside the usual range are accepted by the property grammar but clamped to the supported opacity range. For example, 150% is treated as full opacity.
Invalid Examples
svg path {
stroke-opacity: 10px;
}
svg path {
stroke-opacity: auto;
}
svg path {
stroke-opacity: transparent;
}
svg path {
stroke-opacity: 0.5 0.8;
}
Lengths, color keywords, unsupported keywords, and multiple opacity values are invalid.
How It Works
An SVG element can have a fill, a stroke, or both. The stroke-opacity property controls only the transparency of the painted stroke.
Step 1: Create an SVG Shape
<svg viewBox="0 0 240 140">
<rect x="30" y="20" width="180" height="100"/>
</svg>
Step 2: Define the Fill and Stroke
rect {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 12;
}
The rectangle has a light blue fill and a darker blue outline.
Step 3: Reduce the Stroke Opacity
rect {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 12;
stroke-opacity: 0.4;
}
The stroke becomes 40% opaque while the fill retains its original opacity.
Complete SVG Example
<svg viewBox="0 0 240 140" role="img" aria-label="Light blue rectangle with a partially transparent blue outline">
<rect x="30" y="20" width="180" height="100" fill="#dbeafe" stroke="#2563eb" stroke-width="12" stroke-opacity="0.4"/>
</svg>
Important Consideration
Reducing stroke opacity changes the visibility of the outline without changing its width, color value, or underlying path geometry.
Understanding Opacity Levels
Opacity determines how much of the stroke's painted color remains visible when combined with the background and other graphics.
| Value | Percentage | Appearance |
|---|---|---|
0 |
0% | Fully transparent |
0.25 |
25% | Mostly transparent |
0.5 |
50% | Partially transparent |
0.75 |
75% | Mostly opaque |
1 |
100% | Fully opaque |
Fully Opaque Stroke
.opaque-stroke {
stroke: #2563eb;
stroke-opacity: 1;
}
The stroke uses its full opacity.
Partially Transparent Stroke
.transparent-stroke {
stroke: #2563eb;
stroke-opacity: 0.5;
}
The stroke blends with the colors behind it.
Fully Transparent Stroke
.invisible-stroke {
stroke: #2563eb;
stroke-opacity: 0;
}
The stroke is fully transparent, but the stroke paint and geometry remain defined.
Values Outside the Range
.below-range {
stroke-opacity: -0.5;
}
.above-range {
stroke-opacity: 1.5;
}
Values below 0 are clamped to 0, and values above 1 are clamped to 1.
Important Consideration
Opacity is not the same as brightness. A partially transparent stroke blends with whatever is behind it, so its final appearance depends on the background.
stroke-opacity vs. fill-opacity
The stroke-opacity and fill-opacity properties control separate painted parts of an SVG element.
| Property | Controls |
|---|---|
stroke-opacity |
The transparency of the stroke outline |
fill-opacity |
The transparency of the filled interior |
Transparent Stroke With Opaque Fill
.transparent-outline {
fill: #dbeafe;
fill-opacity: 1;
stroke: #2563eb;
stroke-width: 8;
stroke-opacity: 0.3;
}
The interior remains fully opaque while the outline is partially transparent.
Opaque Stroke With Transparent Fill
.transparent-interior {
fill: #dbeafe;
fill-opacity: 0.3;
stroke: #2563eb;
stroke-width: 8;
stroke-opacity: 1;
}
The outline remains fully opaque while the interior becomes partially transparent.
Both Fill and Stroke Transparent
.transparent-shape {
fill: #dbeafe;
fill-opacity: 0.5;
stroke: #2563eb;
stroke-width: 8;
stroke-opacity: 0.5;
}
Both painted components are partially transparent.
Important Consideration
These properties are independent. Changing stroke-opacity does not change the specified fill-opacity, and changing fill-opacity does not change the specified stroke-opacity.
stroke-opacity vs. opacity
The opacity property affects an entire rendered element or group, while stroke-opacity affects only the stroke paint.
Stroke Opacity
.stroke-only-transparency {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 8;
stroke-opacity: 0.5;
}
Only the stroke becomes partially transparent.
Element Opacity
.entire-shape-transparency {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 8;
opacity: 0.5;
}
The entire rendered shape, including its fill and stroke, becomes partially transparent.
Combining Both Properties
.combined-opacity {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 8;
stroke-opacity: 0.5;
opacity: 0.5;
}
The stroke is painted at 50% opacity, and the rendered element is then composited at 50% opacity. Where no other painted content contributes, the stroke's effective opacity is approximately 25%.
Property Comparison
| Property | Scope | Inherited |
|---|---|---|
stroke-opacity |
Stroke paint | Yes |
fill-opacity |
Fill paint | Yes |
opacity |
Entire rendered element or group | No |
Important Consideration
Use stroke-opacity when only the outline should become transparent. Use opacity when the entire element or group should be composited with reduced opacity.
Stroke Opacity and Color Alpha
CSS colors can include an alpha channel that defines transparency directly in the color value.
RGBA Stroke Color
.rgba-stroke {
stroke: rgb(37 99 235 / 50%);
stroke-width: 8;
}
The stroke color includes 50% alpha.
Separate Stroke Opacity
.separate-opacity {
stroke: #2563eb;
stroke-width: 8;
stroke-opacity: 0.5;
}
The stroke color is defined separately from its opacity.
Combining Color Alpha and Stroke Opacity
.combined-alpha {
stroke: rgb(37 99 235 / 50%);
stroke-width: 8;
stroke-opacity: 0.5;
}
The stroke color has 50% alpha, and stroke-opacity applies another 50% factor. The resulting stroke paint has an effective alpha of 25%.
Using currentColor
.current-color-stroke {
color: #2563eb;
stroke: currentColor;
stroke-width: 8;
stroke-opacity: 0.6;
}
The stroke uses the element's current text color while maintaining a separate opacity value.
Important Consideration
Color alpha and stroke-opacity can be combined, but using both may make transparency harder to manage. Choose a consistent approach when building reusable SVG components.
Inheritance and SVG Groups
The stroke-opacity property is inherited, allowing a parent SVG element or group to establish a default stroke opacity for its descendants.
Inherited Opacity
.icon-group {
stroke: #2563eb;
stroke-opacity: 0.5;
}
Descendant shapes inherit the stroke color and opacity unless they override those properties.
SVG Group Example
<svg viewBox="0 0 240 100">
<g stroke="#2563eb" stroke-width="8" stroke-opacity="0.5" fill="none">
<circle cx="60" cy="50" r="30"/>
<rect x="130" y="20" width="70" height="60"/>
</g>
</svg>
Both shapes inherit the group's stroke opacity.
Overriding an Inherited Value
.icon-group {
stroke-opacity: 0.4;
}
.icon-group .emphasized {
stroke-opacity: 1;
}
The emphasized child uses a fully opaque stroke instead of the inherited 40% opacity.
stroke-opacity vs. Group opacity
Setting stroke-opacity on a group establishes an inherited paint property. Setting opacity on the group composites the group's rendered contents together with reduced opacity.
These operations can produce different results when descendant shapes overlap.
Important Consideration
Use inherited stroke-opacity for consistent outline styling across multiple SVG shapes. Use group opacity when the complete rendered group should fade as a unit.
Animating Stroke Opacity
The stroke-opacity property supports animation, allowing SVG outlines to fade in, fade out, or change emphasis.
CSS Transition
.interactive-outline {
stroke: #2563eb;
stroke-opacity: 0.4;
transition: stroke-opacity 0.3s ease;
}
.interactive-outline:hover {
stroke-opacity: 1;
}
The outline becomes fully opaque when the SVG shape is hovered.
CSS Keyframe Animation
@keyframes stroke-fade {
0% {
stroke-opacity: 0.2;
}
50% {
stroke-opacity: 1;
}
100% {
stroke-opacity: 0.2;
}
}
.animated-outline {
stroke: #2563eb;
animation: stroke-fade 2s ease-in-out infinite;
}
The stroke repeatedly fades between 20% and 100% opacity.
Respect Reduced Motion Preferences
@media (prefers-reduced-motion: reduce) {
.animated-outline {
animation: none;
stroke-opacity: 1;
}
.interactive-outline {
transition: none;
}
}
Disabling unnecessary animation for users who prefer reduced motion improves accessibility.
Important Consideration
Animating stroke-opacity changes the visibility of the outline without changing its geometry or stroke width.
Practical Examples
Subtle SVG Outline
.subtle-outline {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 4;
stroke-opacity: 0.3;
}
Creates a softer outline around a filled shape.
Emphasized Chart Line
.chart-line {
fill: none;
stroke: #0d9488;
stroke-width: 4;
stroke-opacity: 1;
}
Uses a fully opaque stroke for an emphasized chart line.
Background Chart Lines
.background-chart-line {
fill: none;
stroke: #64748b;
stroke-width: 2;
stroke-opacity: 0.25;
}
Reduces the visual emphasis of supporting chart lines.
Transparent Decorative Border
.decorative-border {
fill: none;
stroke: #a855f7;
stroke-width: 10;
stroke-opacity: 0.4;
}
Creates a partially transparent decorative outline.
Dashed Transparent Stroke
.transparent-dashes {
fill: none;
stroke: #2563eb;
stroke-width: 6;
stroke-dasharray: 12 8;
stroke-opacity: 0.5;
}
Applies the same transparency to each painted dash.
Rounded Transparent Line
.rounded-transparent-line {
stroke: #0d9488;
stroke-width: 10;
stroke-linecap: round;
stroke-opacity: 0.6;
}
Creates a rounded line with a partially transparent stroke.
Inherited Stroke Transparency
.diagram-group {
stroke: #475569;
stroke-opacity: 0.5;
}
Applies consistent stroke transparency to descendant SVG shapes.
Reset Stroke Opacity
.opaque-outline {
stroke-opacity: initial;
}
Restores the initial value of 1.
stroke-opacity Example
This example compares four SVG rectangles with identical fill colors, stroke colors, and stroke widths. Only the stroke opacity changes.
HTML Example
<div class="stroke-opacity-demo">
<svg viewBox="0 0 600 180" role="img" aria-label="Four blue-outlined rectangles showing stroke opacity levels of 100, 75, 50, and 25 percent">
<rect class="opacity-100" x="30" y="30" width="110" height="110"/>
<rect class="opacity-75" x="175" y="30" width="110" height="110"/>
<rect class="opacity-50" x="320" y="30" width="110" height="110"/>
<rect class="opacity-25" x="465" y="30" width="110" height="110"/>
</svg>
</div>
CSS Example
.stroke-opacity-demo svg {
display: block;
width: 100%;
height: auto;
}
.stroke-opacity-demo rect {
fill: #dbeafe;
stroke: #2563eb;
stroke-width: 12;
}
.stroke-opacity-demo .opacity-100 {
stroke-opacity: 1;
}
.stroke-opacity-demo .opacity-75 {
stroke-opacity: 0.75;
}
.stroke-opacity-demo .opacity-50 {
stroke-opacity: 0.5;
}
.stroke-opacity-demo .opacity-25 {
stroke-opacity: 0.25;
}
The rectangles retain the same light blue fill. Their outlines become progressively more transparent from left to right.
Comparing the Results
| Rectangle | stroke-opacity | Appearance |
|---|---|---|
| First | 1 |
Fully opaque outline |
| Second | 0.75 |
75% opaque outline |
| Third | 0.5 |
50% opaque outline |
| Fourth | 0.25 |
25% opaque outline |
Using Percentage Values
.stroke-opacity-demo .opacity-50 {
stroke-opacity: 50%;
}
The result is equivalent to stroke-opacity: 0.5.
Changing the Fill Opacity
.stroke-opacity-demo rect {
fill-opacity: 0.5;
}
The interiors become partially transparent while each rectangle retains its individual stroke opacity.
Changing the Stroke Width
.stroke-opacity-demo rect {
stroke-width: 20;
}
The outlines become thicker without changing their assigned opacity values.
Making All Outlines Opaque
.stroke-opacity-demo .opacity-100,
.stroke-opacity-demo .opacity-75,
.stroke-opacity-demo .opacity-50,
.stroke-opacity-demo .opacity-25 {
stroke-opacity: 1;
}
All four rectangles now have fully opaque outlines.
Accessibility Considerations
Stroke transparency affects the visibility of SVG outlines and can influence how easily users recognize shapes, icons, and chart elements.
Maintain Sufficient Contrast
Partially transparent strokes may have reduced contrast against their backgrounds. Ensure important outlines remain distinguishable.
Avoid Overly Faint Essential Graphics
Do not use very low opacity for lines that communicate essential information, such as chart boundaries or diagram connections.
Consider Background Changes
A transparent stroke blends with the background. Test SVG graphics against different backgrounds when their appearance may vary.
Provide Accessible SVG Names
Informative SVG graphics should include accessible names or equivalent descriptions.
<svg viewBox="0 0 200 120" role="img" aria-label="Light blue rectangle with a semi-transparent blue outline">
<rect x="20" y="20" width="160" height="80" fill="#dbeafe" stroke="#2563eb" stroke-width="8" stroke-opacity="0.5"/>
</svg>
Avoid Opacity-Only Meaning
Do not rely solely on stroke opacity to distinguish important states or categories. Include labels, patterns, or other visual indicators when necessary.
Respect Reduced Motion Preferences
When animating stroke opacity, provide an appropriate reduced-motion alternative for users who request it.
Common Mistakes
Using Length Values
svg path {
stroke-opacity: 10px;
}
The property accepts numbers or percentages, not lengths.
Using Color Keywords
svg path {
stroke-opacity: transparent;
}
The transparent keyword is a color value, not a valid stroke opacity value. Use 0 for full transparency.
Confusing Stroke Opacity With Fill Opacity
Changing stroke-opacity does not change the specified transparency of the filled interior.
Confusing Stroke Opacity With Element Opacity
The opacity property affects the entire rendered element. Use stroke-opacity when only the outline should become transparent.
Forgetting to Define a Stroke
The initial stroke value is none. Changing stroke opacity has no visible effect when no stroke is painted.
Expecting Opacity to Change Stroke Width
Opacity changes transparency, not thickness. Use stroke-width to change the stroke's width.
Overlooking Color Alpha
If the stroke color already contains transparency, stroke-opacity applies an additional opacity factor.
Assuming Zero Opacity Removes the Stroke
A fully transparent stroke is not the same as stroke: none. Its geometry remains defined and can still affect pointer-event hit testing under applicable SVG pointer-events rules.
Ignoring Inheritance
Child SVG elements inherit stroke-opacity from their parent unless they specify another value.
Using Very Low Opacity for Important Lines
Extremely faint strokes may become difficult to distinguish from the background, particularly at small display sizes.
Important Considerations
Keep the following points in mind when using stroke-opacity:
- It controls the transparency of SVG stroke paint.
- The initial value is
1. - The property is inherited.
- It accepts numbers and percentages.
- A value of
0makes the stroke fully transparent. - A value of
1makes the stroke fully opaque. - Intermediate values create partial transparency.
- Percentage values correspond to equivalent numeric values.
- Values outside the normal opacity range are clamped.
- It affects the stroke without changing the specified fill opacity.
- The
fill-opacityproperty controls the filled interior. - The
opacityproperty affects the entire rendered element or group. - Stroke color alpha and stroke opacity can combine.
- It does not change stroke width or path geometry.
- It does not change the stroke color value itself.
- A painted stroke is required for a visible result.
- Fully transparent strokes may still participate in pointer-event hit testing.
- It can be inherited from SVG groups.
- It supports CSS transitions and animations.
- Transparent strokes blend with underlying colors.
- Overlapping transparent strokes may produce darker intersections.
- Low opacity can reduce visual contrast.
- Test transparency against different backgrounds and display sizes.
Browser Support
The CSS stroke-opacity property is widely supported for SVG graphics in modern browsers, including Chrome, Edge, Firefox, and Safari.
Numeric stroke opacity values have broad support. Percentage syntax is defined by SVG 2 and may require additional compatibility testing in older browsers.
For current compatibility information, visit Can I Use? and search for stroke-opacity.
Feature Detection
@supports (stroke-opacity: 0.5) {
.transparent-outline {
stroke-opacity: 0.5;
}
}
Feature queries can test whether a browser recognizes the property and value syntax.
Fallback Behavior
If a declaration is invalid or unsupported, the browser ignores it. The element uses another applicable value from the cascade or its inherited value, falling back to 1 when no other value applies.
Testing Recommendations
Test numeric and percentage values, inherited opacity, overlapping strokes, color alpha combinations, and animations in the browsers you support.
Specifications
The stroke-opacity property is defined in the SVG 2 specification.
SVG 2 - stroke-opacity Property
The specification defines stroke paint opacity, accepted values, inheritance, and how transparency is applied during SVG rendering.
For additional documentation and examples, consult MDN Web Docs.
