Transparency is in some ways another facet of color, and in some ways a distinct and much more complex topic. This is reflected in the different ways you can define transparency, which we’ll compare and contrast in this chapter.

One thing is constant when talking about transparency and web design: you don’t talk about transparency, you talk about opacity. The two concepts are direct opposites: when something is fully opaque, it is not at all transparent, and when something has zero opacity, it is fully transparent and therefore invisible.

Opacity on the Web is always expressed as a decimal number between 0.0 (invisible) and 1.0 (solid, no transparency). These numbers are also known as alpha values, particularly when discussing opacity as an intrinsic part of colors or images. The a in rgba() and hsla() refers to the alpha channel. These functions take four values, instead of the usual three, with the fourth being the alpha value between 0 and 1.

Note

The keyword transparent, which formerly had a special meaning for CSS backgrounds, has been redefined as a named color. Equivalent to rgba(0,0,0,0), it can be used like any other color keyword in browsers that support CSS 3 colors.

The final effect of modifying a graphic’s alpha value depends on the method you use. In particular, the overall opacity property works in a significantly different way compared to the other options.

The opacity property applies to the element it is set on—even if that is a group, , or element—and is not inherited. It takes the final drawn result for that element, including all its child content, and makes it uniformly more transparent.

Tip

The opacity value is applied after determining the final color at every point where two shapes—or fill and stroke of the same shape—overlap.

In contrast, when you set stroke-opacity or fill-opacity, or when you use rgba or hsla color functions, the transparent effect is applied at the time each shape is drawn, to that colored section only. The stroke-opacity and fill-opacity properties are both inherited by default.

Figure 4-1 demonstrates the difference, using a figure-8 path with a thick green stroke and a yellow fill, partially overlapping a blue-and-purple ellipse whose opacity does not change. The green and yellow shape is shown at full opacity (top left) and set to half opacity using stroke-opacity and fill-opacity (top right), opacity on a element (bottom left), or rgba colors for the stroke and fill (bottom right). Example 4-1 gives the code.

Figure 4-1. A green and yellow shape at full opacity and made partially transparent in three different ways

Example 4-1. Using different opacity options to control the transparency of your graphics

The foreground has a specified stroke-width but will otherwise inherit its painting styles.

A CSS block is used to style the text labels and prevent cropping of the nested elements. Note that the 7px font-size will be interpreted within the local coordinate system, and so will not create unusually small type.

Each nested re-creates the same local coordinate system in a different quadrant of the main graphic, so that the reused graphics can be positioned in the same way.

The background element is reused as is within each sample, and then the foreground layered on top. In this first example, the foreground is given solid fill and stroke colors.

The following elements position the content in the other quadrants of the graphic, and use different presentation attributes on the foreground elements.

For the opacity setting, it will apply directly on the element as a combined group, instead of inheriting to the reused graphic.

Figure 4-1 also demonstrates some of the concepts we discussed in Chapter 2. The strokes are centered over the edge of each shape. When the stroke is partially transparent (due to stroke-opacity or an rgba color), this creates a two-toned color effect, with the fill visible under the inside part of the stroke.

Another feature to note is that the overlapping sections of stroke do not have a different color tone, compared to areas where the stroke only passes once. Conceptually, we like to think of strokes as if they were made by a marker or paint brush tracing out the lines. If that were true, these double-stroked regions would have twice as intense color, the way it would if you used translucent watercolor paint and passed your brush over the same region twice. However, the computer works by caculating out the total area of the stroke and then applying the coloring evenly, as if it was cut out of a sheet of semitransparent plastic.

With all these different ways of controlling opacity, what happens when you combine them? The effect is multiplied. For example, the following circle has its fill made partially transparent in two different ways:

The effect would be exactly the same with the following code, because 0.5×0.6=0.3:

r="10"fill="hsl(240,100%,75%)"fill-opacity="0.3">

It gets a little more complicated with opacity, because the transparency is applied after combining the other colors. Nonetheless, an opacity: 0.5 property still causes the alpha value of each pixel in the shape to be cut in half (multiplied by 0.5), after applying the opacity levels from all other properties.

Because the opacity property is applied using a simple mathematical adjustment of each pixel value, it can be efficiently implemented by the graphical processing unit (GPU) of most video cards. Changes in opacity can often be animated quite smoothly as a result, although not all browsers take advantage of this for SVG content.

Partially transparent colors, Boots Boots Boots Khaki Fashion Winter Classic Ankle Toe Autumn Shoes Round Martin Women FALAIDUO Casual fill-opacityHigh Toe Black Spikes Pointed Stilettos Boots Solid Women's Closed Frosted Allhqfashion Top OqZY7I, and stroke-opacity do not create the same optimization, because they only affect parts of an element. However, if you are not animating the opacity, these properties can be more performant because they do not force the creation of a stacking context.

The final color displayed when layers overlap is generated by, first, scaling both colors according to the sRGB model, then taking a weighted average between the background and foreground—where the alpha value is the weight of the foreground—and finally reversing the sRGB scale. This calculation works regardless of whether the background color was created from a single element or from blending multiple partially transparent ones.

This method of blending colors is known as simple alpha compositing. In many graphics programs, it is also known as the “normal” blending mode.

Beyond Simple Alpha Blending

Many graphics programs define alternative blending modes that control how the colors from two different layers or objects are combined together.

In SVG 1.1, many of these blending modes are available, but only through filters. A new Compositing and Blending Specification created by the W3C standardizes the definitions of these blending modes for use by CSS and HTML 5 canvas as well. Two of the new CSS properties it introduces can apply to SVG: mix-blend-mode and isolation.

The mix-blend-mode on an element controls how that element is blended into the colors behind it. There are 16 different modes (including the default, Classic FALAIDUO Fashion Toe Winter Round Boots Khaki Shoes Ankle Boots Martin Women Casual Autumn Boots normal) that define equations for combining the RGBA or HSLA values of the element with the equivalent values for the blended background content.

The isolation property on a grouping element or element limits how far the blending continues. Child content of an isolated element is blended together, but then that element uses its own blending mode to blend the result into the background. You explicitly isolate an element using isolation: isolate, but various other CSS properties will cause isolation, including setting opacity other than 1, using filters or masks, setting a blending mode other than normal on the group itself, or using 3D transformations. An inline element in HTML will by default be isolated.

At the time of writing, blending modes are supported in the latest versions of Firefox, Chrome, Opera, and Safari. They are not supported yet in many mobile browsers or in Internet Explorer, although they are “under consideration” for Microsoft Edge.

Heels Boots Low Zipper Women's Toe AgooLar PU Black Solid Round T6wIxqUThe colors created when using simple alpha blending directly correspond to colors in an sRGB gradient between the background color and the solid color of the object. The alpha value determines the distance along the gradient. If alpha is zero, and the object is completely transparent, the initial value of the gradient is used—in other words, the background color. If alpha is 0.5, the midpoint of the gradient is used. If alpha is 1 (the object is completely opaque), the displayed color is the same as the end point of the gradient—the foreground object’s own color.

Figure 4-2 demonstrates this relationship by comparing a smooth gradient from red (#F00) to blue (#00F) with the result of layering blue squares overtop of a solid red rectangle, where the squares vary in their fill-opacity values.

Note

It may appear to you as if the squares in Figure 4-2 were also filled with gradients, transitioning in the opposite direction. This is a well-recognized optical illusion. Your eyes and brain enhance the contrast between colors on either side of an edge. When a solid-colored element has different contrasting colors on each side—or a gradient on one side—the contrast enhancement is perceived as a gradient.

To confirm that you really are looking at a solid color, use pieces of paper (all the same color) to cover up the adjacent sections of the figure, until you only see a single, solid-color square.

Figure 4-2. Blending colors with gradients and with opacity; the squares consist of a solid red rectangle underneath blue squares with fill-opacity of 0, 0.25, 0.5, 0.75, and 1

Chapter 6 will introduce the and elements used in Example 4-2 and describe how they can be adjusted to create different gradient effects. But first, Chapter 5 will explore what it means in SVG to fill one element with a URL reference to another.

1. Feature: 100% brand new and high quality; Casual Fashion Style ,The latest shoes in autumn and winter, using high-quality materials, soft and comfortable fabrics, give your feet a comfortable warmth, so that you will not be cold this winter.