CSS / Reference / color: Foreground Colors, CurrentColor, and Contrast
CSS color sets the foreground color used for text. A child with no color declaration normally inherits it, and other properties such as a border color can reuse the element’s own foreground color through currentColor. Set background-color to paint the background directly.
Set the text color and its alpha
You can write a color as a name, a hexadecimal value, or an rgb() function. #006c9c and rgb(0 108 156) specify the same blue. A value after the slash sets the color’s alpha, or opacity. The initial color is the system color CanvasText, while the color on a real page depends on the rules that apply to the element. See the CSS foreground color specification for the formal definition.
| Value | Meaning | Swatch |
|---|---|---|
rebeccapurple |
A named purple | |
#006c9c |
Blue expressed as hexadecimal red, green, and blue components | |
rgb(0 108 156) |
The same blue with components on a scale from 0 to 255 | |
rgb(0 108 156 / 50%) |
The same blue at 50% alpha |
A color at 50% alpha blends with the background behind it. The alpha in color: rgb(0 108 156 / 50%) applies wherever that foreground color is painted. Applying opacity: 0.5 to the box fades the whole box, including its background, borders, and descendants. For more ways to write colors, see the guide to CSS color values and units.
Share a color through inheritance and currentColor
A child without its own color takes its parent’s color. Setting it on a notice therefore colors ordinary text inside the notice. A border and an empty decorative chip can use currentColor to share that color.
.notice {
color: #006c9c;
background-color: #fff;
border: 2px solid currentColor;
}
.notice .ink-chip { background-color: currentColor; }
A border set to currentColor follows the foreground color of the element that owns the border. The chip follows its own color, so a separate color declaration on the chip can make it differ from the box. When currentColor is itself the value of color, it resolves to the inherited color. The box keeps the white background specified in the snippet. See the CSS definition of currentColor and the related background-color property.
Run the example and change one declaration
Save the whole document as a UTF-8 file named color-example.html and open it in a browser. It needs no external stylesheet.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare CSS colors and contrast</title>
<style>
body { margin: 1rem; color: #20242b; background-color: #fff;
font: 1rem/1.5 system-ui, sans-serif; }
main { max-width: 44rem; margin-inline: auto; }
.notice { border: 2px solid currentColor; padding: .75rem;
background-color: #fff; margin-block: .75rem; }
.cool { color: #006c9c; }
.warm { color: #a03c00; }
.ink-chip, .color-chip { display: inline-block; width: 1em;
height: 1em; background-color: currentColor; }
.notice a { color: rebeccapurple; text-decoration: underline; }
.opaque { color: rgb(0 108 156); }
.faded { color: rgb(0 108 156 / 50%); }
</style>
</head>
<body>
<main>
<h1>Compare CSS colors and contrast</h1>
<h2>Inheritance and currentColor</h2>
<div class="notice cool">
<span class="ink-chip" aria-hidden="true"></span>
<strong>Blue text and border</strong>
<a href="#contrast">Go to the contrast samples</a>
</div>
<div class="notice warm">
<span class="ink-chip" aria-hidden="true"></span>
<strong>Orange text and border</strong>
<a href="#contrast">Go to the contrast samples</a>
</div>
<h2 id="contrast">Alpha on a white background</h2>
<p class="opaque"><span class="color-chip" aria-hidden="true"></span> Opaque blue</p>
<p class="faded"><span class="color-chip" aria-hidden="true"></span> The same blue at 50% alpha</p>
</main>
</body>
</html>
The first notice has blue text, a blue border, and a blue chip. The second uses orange for all three. Both backgrounds are white. The links stay purple because they have a color rule of their own. In the last two lines, the one with 50% alpha looks lighter.
Change only the value in .cool to #a03c00 and reload. The first box’s text, border, and chip turn orange. Its background and link color stay the same. Restore #006c9c after comparing the two boxes. Next, change only the background in .notice to #edf4f8, check what changed, and then restore white.
Check text contrast against the actual background
On white, opaque #006c9c has about 5.79:1 contrast. With 50% alpha, the same blue blends into white and drops to about 2.2:1. The Web Content Accessibility Guidelines (WCAG) 2.2 Level AA minimum is 4.5:1 for regular text and 3:1 for large text. Large text is generally at least 18pt or 14pt bold, or the equivalent size for Chinese, Japanese, and Korean fonts. Check the final background and the exceptions in the WCAG minimum contrast criterion.
When text sits over a translucent layer or a photo, check the color directly behind the text after compositing. Check light and dark themes and verify that important text stays readable in forced colors. For errors and selected states, pair color with an error message, an underline, or a visible selection indicator. The WCAG use-of-color criterion gives examples.
Find why some text has a different color
Inspect the element that has a different color and check its computed color. A declaration on a child or link can override the value you expected it to inherit. To let links inside the notices follow their own box, replace the example’s link rule with this one.
.notice a {
color: inherit;
text-decoration: underline;
}
Each link now matches its box and keeps its underline. If changing text color also recolors a background or decoration, inspect that property for a connection through currentColor.




