Opacity vs fill-opacity in svg

What is the difference in opacity vs fill-opacity in SVG?

I called docs fill-opacity and opacity , but I'm confused by what is meant by fill-opacity: opacity of the content of the current object vs opacity: transparency of an object

+5
source share
4 answers

The difference is exactly what the name indicates :). fill-opacity applies only to the fill element (or, in other words, only its background), stroke-opacity applies only to stroke , while opacity applies to both.

opacity is a post-processing operation. That is, the element (or group) as a whole (fill + stroke) is displayed, and then the transparency is adjusted based on the opacity parameter, while fill-opacity and stroke-opacity are intermediate steps, so transparency is individually added to the stroke and fill. Therefore, when stroke-opacity and fill-opacity are used together, the result will still not be the same as using opacity (since transparency on the fill will allow the fill color to show wherever they overlap). Visual visualization can be seen in the first two elements below.

Also, as pointed out by Robert (in the comments), fill-opacity does not apply to image , while opacity works.

 svg { width: 100vw; height: 100vh; } body { background: url(http://lorempixel.com/600/600/nature/1); height: 100vh; } polygon { stroke-width: 3; } 
 <svg viewBox='0 0 40 190'> <polygon points='10,10 30,10 30,30 10,30' fill='steelblue' stroke='crimson' opacity='0.5' /> <polygon points='10,40 30,40 30,60 10,60' fill='steelblue' stroke='crimson' fill-opacity='0.5' stroke-opacity='0.5' /> <polygon points='10,70 30,70 30,90 10,90' fill='steelblue' stroke='crimson' fill-opacity='0.5' /> <polygon points='10,100 30,100 30,120 10,120' fill='steelblue' stroke='crimson' stroke-opacity='0.5' /> <image xlink:href='http://lorempixel.com/100/100/nature/3' x='8.5' y='130' width='23' height='23' stroke='crimson' opacity='0.5' /> <image xlink:href='http://lorempixel.com/100/100/nature/3' x='8.5' y='160' width='23' height='23' stroke='crimson' fill-opacity='0.5' /> </svg> 

In the CSS world, you can think of it as something like the snippet below. (Note that I'm not saying they are equal, there are subtle differences between SVG and CSS . This is just an attempt to explain what these SVG attributes translate into CSS.)

 div { height: 20vh; width: 20vh; margin: 30px auto; font-family: Verdana; font-size: 2vw; } div:nth-of-type(1) { opacity: 0.5; background: rgba(70, 130, 180, 1); border: .35vw solid rgba(220, 20, 60, 1); } div:nth-of-type(2) { background: rgba(70, 130, 180, 0.5); border: .35vw solid rgba(220, 20, 60, 1); } div:nth-of-type(3) { background: rgba(70, 130, 180, 1); border: .35vw solid rgba(220, 20, 60, 0.5); } body { background: url(http://lorempixel.com/600/600/nature/1); height: 100vh; } 
 <div></div> <div></div> <div></div> 
+13
source

In addition to influencing which parts of each individual element are affected (for example, padding versus stroke), another difference is what happens when elements overlap within a group. opacity affects the transparency of the group as a whole, and fill-opacity affects the transparency of individual elements within the group. One consequence of this is that when the elements within such a group overlap, there is a compounding effect in the overlap area when fill-opacity , but not when opacity used. This is shown in the image below when transparency (fill-) of 0.5 is applied to each element within the group or to the group itself.

enter image description here

 <svg height="200"> <g transform="translate(0, 0)"> <rect x="10" y="10" width="40" height="40" fill-opacity="0.5"/> <rect x="30" y="30" width="40" height="40" fill-opacity="0.5"/> </g> <g transform="translate(80, 0)" fill-opacity="0.5"> <rect x="10" y="10" width="40" height="40"/> <rect x="30" y="30" width="40" height="40"/> </g> <g transform="translate(0, 80)"> <rect x="10" y="10" width="40" height="40" opacity="0.5"/> <rect x="30" y="30" width="40" height="40" opacity="0.5"/> </g> <g transform="translate(80, 80)" opacity="0.5"> <rect x="10" y="10" width="40" height="40"/> <rect x="30" y="30" width="40" height="40"/> </g> <text transform="translate(170,45)">fill-opacity</text> <text transform="translate(170,125)">opacity</text> <text transform="translate(10,175)">applied to</text> <text transform="translate(0,190)">each element</text> <text transform="translate(90,175)">applied to</text> <text transform="translate(103,190)">group</text> </svg> 
+3
source

fill-opacity controls the opacity of the fill color. opacity controls the opacity of the entire object.

Take a look at this demo: https://jsfiddle.net/DerekL/9mvrL66g/

+2
source

I found this table useful when considering which taste opacity use with SVG . Do not forget about stroke-opacity and stop-opacity .

 | Attribute | Explanation | Applies to: | |:--------------:|:----------------------------------:|:------------------------------------:| | opacity | The overall opacity of the element.| Everything but gradient stops | | fill-opacity | The opacity of the fill paint. | Elements with the attribute 'fill' | | stroke-opacity | The opacity of the stroke paint. | Elements with the attribute 'stroke' | | stop-opacity | The opacity of the gradient stop. | Gradient stops | 
+2
source

All Articles