Tillitsdone
down Scroll to discover

CSS Stroke-Opacity Control SVG Stroke Transparency

Discover how to use the CSS stroke-opacity property to control the transparency of SVG strokes.

Learn about its available options and create visually appealing designs.
thumbnail

Introduction

The stroke-opacity property in CSS controls the transparency of strokes in SVG shapes. Unlike the opacity property, which affects the entire element, stroke-opacity targets the stroke specifically. This property is widely supported across modern browsers, ensuring your designs look consistent across different devices.

Specification

The stroke-opacity property is defined in the CSS Fill and Stroke Module Level 3 specification. It provides a standardized way to control the appearance of strokes and fills in SVG graphics.

Description

The stroke-opacity property controls the transparency of the stroke applied to SVG shapes. It allows for more granular control over the appearance of SVG shapes without affecting the entire element.

Syntax and Values

The stroke-opacity property in CSS is straightforward to use. Here’s the basic syntax:

/* Numeric and percentage values */
stroke-opacity: 1;
stroke-opacity: 0.3;
stroke-opacity: 50%;
/* Global values */
stroke-opacity: inherit;
stroke-opacity: initial;
stroke-opacity: revert;
stroke-opacity: revert-layer;
stroke-opacity: unset;

Values

  1. <number>:

    • Any real number from 0 to 1.
    • 0 makes the stroke completely transparent.
    • 1 makes the stroke completely opaque.
    • Values outside the range 0–1 are clipped to the nearest end of that range.
  2. <percentage>:

    • The same as <number>, but the allowed range is 0% to 100%.
    • Clipping is done with regard to that range.

Global Values

  • inherit: Inherits the value from its parent element.
  • initial: Sets the property to its default value, which is 1.
  • revert: Reverts the value to the user agent’s default.
  • revert-layer: Reverts the value to the user agent’s default for the cascade layer.
  • unset: Resets the property to its natural value, which means it behaves like inherit if the property is inherited, or like initial if not.

Example

Here’s a simple example to illustrate the usage of the stroke-opacity property:

HTML

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" />
</svg>

CSS

svg circle {
stroke-opacity: 0.5; /* 50% opacity */
}

In this example, the stroke of the circle will be 50% transparent, allowing the fill color to show through the stroke.

Formal Definition

The stroke-opacity property is formally defined as follows:

  • Initial Value: 1
  • Applies To: <circle>, <ellipse>, <line>, <path>, <polygon>, <polyline>, and <rect> elements in an svg
  • Inherited: Yes
  • Computed Value: The specified value, clipped in the range [0,1]
  • Animation Type: By computed value type

Formal Syntax

The formal syntax for the stroke-opacity property is:

stroke-opacity = <opacity>
<opacity> = <opacity-value>
<opacity-value> = <number> | <percentage>

Examples

Basic Usage of stroke-opacity

Here’s how you can use different values for stroke-opacity to control the transparency of strokes in SVG shapes.

Using stroke-opacity with Percentage Values

Let’s see how percentage values can be used to set the stroke-opacity property.

HTML
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="80" height="80" stroke="black" stroke-width="5" fill="dodgerblue" class="rect1" />
<rect x="20" y="20" width="60" height="60" stroke="black" stroke-width="5" fill="dodgerblue" class="rect2" />
<rect x="30" y="30" width="40" height="40" stroke="black" stroke-width="5" fill="dodgerblue" class="rect3" />
</svg>
CSS
.rect1 {
stroke-opacity: 100%; /* Fully opaque stroke */
}
.rect2 {
stroke-opacity: 50%; /* 50% transparent stroke */
}
.rect3 {
stroke-opacity: 20%; /* 20% transparent stroke */
}

In this example, we have three rectangles with different stroke-opacity values set using percentage values. The first rectangle has a fully opaque stroke, the second rectangle has a 50% transparent stroke, and the third rectangle has a 20% transparent stroke.

Applying stroke-opacity to a Group of Shapes

Let’s see how stroke-opacity can be applied to a group of shapes using the <g> element.

HTML
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g class="group">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" />
<rect x="10" y="10" width="80" height="80" stroke="black" stroke-width="5" fill="dodgerblue" />
<line x1="0" y1="0" x2="100" y2="100" stroke="black" stroke-width="5" />
</g>
</svg>
CSS
.group {
stroke-opacity: 0.5; /* 50% transparent stroke for all shapes in the group */
}

In this example, we have a group of shapes (a circle, a rectangle, and a line) enclosed within a <g> element. We apply the stroke-opacity property to the group, setting it to 50% transparent. This affects the transparency of strokes for all shapes within the group.

Compatibility and Support

The stroke-opacity property is widely supported across modern web browsers, making it a reliable tool for web developers and designers. This property has been available since July 2020 and is compatible with the following browsers:

Browser Compatibility for stroke-opacity

The stroke-opacity property is widely supported across modern browsers, making it a reliable choice for web developers. Here’s a quick overview:

  • Google Chrome: Fully supported.
  • Mozilla Firefox: Fully supported.
  • Microsoft Edge: Fully supported.
  • Safari: Fully supported.
  • Opera: Fully supported.

This broad compatibility means you can use stroke-opacity confidently, knowing your designs will look consistent across different devices and platforms.

Browser Compatibility Details

BrowserVersionSupport
Google ChromeAllFull Support
Mozilla FirefoxAllFull Support
Microsoft EdgeAllFull Support
SafariAllFull Support
OperaAllFull Support

Notes on Compatibility

  • Internet Explorer: stroke-opacity is supported starting from Internet Explorer 9. However, using modern browsers is recommended for the best experience.
  • Mobile Browsers: stroke-opacity is also supported in mobile versions of the browsers listed above, ensuring consistent designs on both desktop and mobile devices.

Checking Compatibility

To check if stroke-opacity is supported in your target browsers, you can use tools like Can I Use or refer to the MDN Web Docs.

Polyfills and Fallbacks

For older browsers that don’t support stroke-opacity, consider using polyfills or fallback mechanisms to ensure backward compatibility. JavaScript can detect the browser version and apply alternative styling if needed.

Related CSS Properties

Understanding related CSS properties can help you create more sophisticated designs. Here are some key properties:

  1. opacity: Sets the opacity level for an entire element, including its contents.

    .element {
    opacity: 0.5; /* 50% opacity */
    }
  2. fill-opacity: Sets the opacity of the fill color of an SVG shape.

    .shape {
    fill-opacity: 0.7; /* 70% opacity for the fill */
    }
  3. paint-order: Controls the order in which the fill, stroke, and markers of an SVG shape are rendered.

    .shape {
    paint-order: stroke fill markers; /* Render stroke first, then fill, then markers */
    }
  4. stroke: Sets the color of the stroke applied to an SVG shape.

    .shape {
    stroke: red; /* Red stroke */
    }
  5. stroke-dasharray: Creates dashed or dotted strokes by specifying the lengths of the dashes and gaps.

    .shape {
    stroke-dasharray: 5, 10; /* Dashes of 5 units, gaps of 10 units */
    }
  6. stroke-dashoffset: Specifies the distance into the dash pattern to start the stroke.

    .shape {
    stroke-dashoffset: 5; /* Start the dash pattern 5 units into the stroke */
    }
  7. stroke-linecap: Controls the shape of the end caps of an open stroke.

    .shape {
    stroke-linecap: round; /* Round end caps */
    }
  8. stroke-linejoin: Controls the shape of the corners where two strokes meet.

    .shape {
    stroke-linejoin: round; /* Round corners */
    }
  9. stroke-miterlimit: Sets the maximum length of the miter before it is converted to a bevel.

    .shape {
    stroke-miterlimit: 10; /* Maximum miter length of 10 units */
    }
  10. stroke-width: Sets the width of the stroke applied to an SVG shape.

    .shape {
    stroke-width: 5; /* Stroke width of 5 units */
    }

Usage in SVG Elements

The stroke-opacity property is designed to control the transparency of strokes applied to SVG elements. Here’s how you can use it with different SVG shapes:

Applicable SVG Elements

  1. <circle>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" />
    </svg>
  2. <ellipse>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <ellipse cx="50" cy="50" rx="40" ry="20" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" />
    </svg>
  3. <line>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <line x1="10" y1="10" x2="90" y2="90" stroke="black" stroke-width="5" stroke-opacity="0.5" />
    </svg>
  4. <path>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <path d="M10 10 L90 90" stroke="black" stroke-width="5" fill="none" stroke-opacity="0.5" />
    </svg>
  5. <polygon>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <polygon points="50,10 90,90 10,90" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" />
    </svg>
  6. <polyline>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <polyline points="10,10 40,40 70,10" stroke="black" stroke-width="5" stroke-opacity="0.5" />
    </svg>
  7. <rect>:

    <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="80" height="80" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" />
    </svg>

Inherited Property Usage

The stroke-opacity property is inherited, meaning it can be applied to container elements like <g> and still affect the strokes of descendant elements. This is useful for managing complex SVG graphics efficiently.

Example of Inherited Usage

Applying stroke-opacity to a <g> element affects the strokes of all child elements within the group.

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g stroke-opacity="0.5">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" />
<rect x="10" y="10" width="80" height="80" stroke="black" stroke-width="5" fill="dodgerblue" />
<line x1="0" y1="0" x2="100" y2="100" stroke="black" stroke-width="5" />
</g>
</svg>

In this example, the stroke-opacity property is applied to the <g> element, setting the stroke opacity to 50% for all child elements.

Interaction with Other Properties

Understanding how stroke-opacity interacts with other CSS properties is crucial for creating sophisticated and visually appealing designs. Here are some key interactions:

Interaction with opacity

The opacity property sets the overall opacity of an element, affecting both its contents and strokes. Using opacity in conjunction with stroke-opacity can help achieve different visual effects.

Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" opacity="0.75" />
</svg>

In this example, the circle’s overall opacity is 75%, and the stroke’s opacity is 50%. The stroke will look more transparent than the fill due to both properties.

Interaction with fill-opacity

The fill-opacity property sets the transparency of the fill color of an SVG shape. It works similarly to stroke-opacity but affects the fill instead.

Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" fill-opacity="0.75" />
</svg>

In this example, the fill has 75% opacity, while the stroke has 50% opacity.

Interaction with paint-order

The paint-order property controls the order in which the fill, stroke, and markers of an SVG shape are rendered.

Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="dodgerblue" stroke-opacity="0.5" paint-order="stroke fill" />
</svg>

Here, the stroke is rendered before the fill, which can affect how stroke-opacity looks.

Interaction with stroke-dasharray and stroke-dashoffset

stroke-dasharray creates dashed or dotted strokes, while stroke-dashoffset shifts the dash pattern.

Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="5" fill="none" stroke-opacity="0.5" stroke-dasharray="5, 10" stroke-dashoffset="5" />
</svg>

In this example, the stroke is dashed with a pattern of 5 units of dash followed by 10 units of gap, and the dash pattern is shifted by 5 units.

Interaction with stroke-linecap and stroke-linejoin

stroke-linecap controls the shape of the end caps of an open stroke, while stroke-linejoin controls the shape of the corners where two strokes meet.

Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<line x1="10" y1="10" x2="90" y2="90" stroke="black" stroke-width="5" stroke-opacity="0.5" stroke-linecap="round" />
<polygon points="50,10 90,90 10,90" stroke="black" stroke-width="5" fill="none" stroke-opacity="0.5" stroke-linejoin="round" />
</svg>

In this example, the line has rounded end caps, and the polygon has rounded corners. Both strokes have 50% opacity.

Interaction with stroke-miterlimit

The stroke-miterlimit property sets the maximum length of the miter before it is converted to a bevel.

Example

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<polygon points="50,10 90,90 10,90" stroke="black" stroke-width="5" fill="none" stroke-opacity="0.5" stroke-miterlimit="10" />
</svg>

In this example, the polygon’s stroke corners have a maximum miter length of 10 units, and the stroke has 50% opacity.

Summary

The stroke-opacity property is a powerful tool for controlling the transparency of strokes in SVG elements. Its wide support across modern browsers makes it a reliable choice for web developers and designers. By understanding how to use stroke-opacity in conjunction with other CSS properties and SVG elements, you can create sophisticated and visually appealing designs that enhance the overall aesthetics of your web pages.

Ensuring browser compatibility and using fallbacks for older browsers helps maintain the consistency of your designs across different environments. With the stroke-opacity property at your disposal, you can elevate the visual appeal of your SVG graphics and create engaging web experiences for your users.

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.