> codebench_TOOL: HomeLOCAL-ONLY⌘K Switch

CSS Opacity vs. Background Alpha: Tint a Label Without Fading Its Text

Two stock labels have the same blue tint, but only one is easy to read. Compare element opacity with background alpha, then decode the last two hex digits.

·2 min readAbout the maintainer
The last pair in the CSS color #33669966 is singled out: hex 66 makes the background paint 40 percent opaque.

A shop wants a quieter blue background behind its “In stock” label. You add opacity: 0.4 to the badge. The blue looks right, but now the perfectly ordinary stock label looks disabled. Making the letters bolder doesn't fix it. The problem is which part of the badge you asked the browser to fade.

Compare two labels on the same surface

Place these elements next to each other on a white page. They contain exactly the same text. The first uses the opaque blue #336699 and fades the whole element. The second uses the same blue with an alpha channel in the background color.

<span class="stock whole">In stock</span>
<span class="stock paint">In stock</span>
.stock {
  display: inline-block;
  padding: 0.4em 0.8em;
  color: #142033;
  font: bold 1rem system-ui, sans-serif;
}
.whole { background-color: #336699; opacity: 0.4; }
.paint { background-color: #33669966; }

Compare an empty corner of each badge first. The tints look alike. Now compare the lettering. With opacity, the browser composites the badge as a group: its background, its text, and any child icon all fade together. Giving a child opacity: 1 does not undo that last step. In the second badge, only the background paint is translucent. The text still uses the dark ink you specified.

Whole-element opacity fades the ink and the blue rectangle together. A translucent background leaves the ink at full strength.
Judge the empty corners and the letters separately. A matching background pixel doesn't prove that the two badges are equivalent.

Read the alpha byte, not a percentage

Open the Color Converter and paste #3696 into its text field. Four-digit CSS hex is shorthand: each digit doubles, so the HEX output is #33669966. In CSS the order is red, green, blue, then alpha. The last pair, hexadecimal 66, is 102 in decimal. Divide by 255 and you get exactly 0.4. The RGB row therefore reads rgba(51, 102, 153, 0.4).

Try the reverse direction: enter rgba(51, 102, 153, 0.8). The hex result is #336699cc, not #33669980. Hexadecimal 80 means 128 out of 255, just over half opacity. At the endpoints, 00 means that none of this paint shows through and ff means it is fully opaque. Neither one specifies the color of the surface underneath.

Keep the backdrop in the test

Move the two badges over a striped background. With the second badge, the stripes show through its blue fill but not through the solid interiors of its letters. That's useful for a label on a photograph, and it also means the sample on white wasn't a contrast test for that photograph. Check the label over the lightest and darkest patches where it will actually sit.

Use element opacity when the whole badge is supposed to disappear, such as during a fade-out. For a persistent label whose words should stay readable, keep the element opaque and put alpha on the particular paint you mean to change. The decision isn't really between two ways to write a color. It's between fading a layer of paint and fading everything that has already been drawn.

#css#colors#opacity
> codebench $Ctrl+Enter Run