AD JB Toolbox 01 EN

CSS / Reference / color: Foreground Colors, CurrentColor, and Contrast

color sets a foreground color used for text and related painting. Background color is separate; properties using currentColor can follow the element’s foreground color.

Syntax and value choices

Property Initial value Inherited Main application conditions
color canvastext Yes All elements; visible effects depend on the text, image, or box conditions below

Initial values are not necessarily the values assigned to HTML elements by the browser’s stylesheet. Inspect the actual result in developer tools’ Computed panel.

.notice { color: #006c9c; }
.notice { border: 2px solid currentColor; }
Value or category Behavior and choice
Named / hexadecimal Use a named color or a hexadecimal value.
rgb(0 108 156) Specify red, green, and blue components.
rgb(0 108 156 / 50%) Include an alpha component.
currentColor Reuse the element’s foreground color in another color-valued property.

For supported values and conditions, see MDN color.

Run the example and compare results

Save this complete code as css-reference-10663-example.html in UTF-8 and open it in a browser. Keep its demonstration styles in that file rather than pasting them into a site-wide stylesheet.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>color comparison example</title>
<style>
body { margin: 0; color: #20242b; background: #fff; font: 16px/1.7 "Malgun Gothic", sans-serif; }
#demo { padding: 24px; max-width: 820px; margin: auto; }
h1 { font-size: 22px; margin: 0 0 20px; }
.samples { display: flex; flex-wrap: wrap; gap: 24px; }
.label { margin: 0 0 8px; font-weight: 700; }
.target, .reference { width: 220px; border: 2px solid currentColor; padding: 12px; }
.target { color: #006c9c; }
.reference { color: #a03c00; }
</style>
</head>
<body>
<main id="demo">
<h1>color comparison</h1>
<section class="samples"><div class="a"><p class="label">Blue foreground</p><div class="target">Text and border share a color</div></div><div class="b"><p class="label">Orange foreground</p><div class="reference">Text and border share a color</div></div></section>
</main>
</body>
</html>

The text and currentColor border change together. Changing color alone does not fill the background.

Inheritance and currentColor

Color is normally inherited by children. Explicit inherit requests the parent’s computed color. Omitting a border color or using currentColor can connect that border to each element’s text color.

Communicate with more than color

Combine an error or selected state with useful text or another perceivable cue. Check light, dark, and forced-color environments. Alpha compositing changes the final displayed color and therefore the contrast with its background.

Change one setting at a time

Use the complete example file above. Change only the specified selector or value rather than several comparison conditions at once.

  1. Change only .target’s color and observe its currentColor border. Set a background separately, then restore the original colors.
  2. Combine error colors with useful text or another cue. Measure contrast against the final background and check light, dark, and forced-color environments.

For color notation and practical choices of relative units, see CSS Colors and Units: px, em, rem, and Percentages.

Compare the minimum 4.5:1 threshold for ordinary text with 3:1 for large text. Check the official size conditions and exceptions such as inactive elements and logos. Over a photo or alpha background, inspect the composited background instead of entering only the original colors.

Check the relevant conditions in WCAG text contrast criteria.

For coordinated font sizing, line heights, and typeface choices, see CSS Fonts, Font Sizes, and Line Height. Related properties to compare alongside these conditions are background-color.

When the result is missing or unexpected

  • Inspect a child or link rule when only some text has a different color.
  • Check for background properties using currentColor when a foreground change also recolors a background.
More in This Category
CSS / Reference / box-shadow: Offsets, Blur, Spread, and Inset Shadows

CSS / Reference / box-shadow: Offsets, Blur, Spread, and Inset Shadows

Build box shadows from offsets, blur, and spread, then inspect clipping and layering. Distinguish box geometry from glyph or transparent-image shadows.

CSS / Reference / list-style-position: Inside and Outside Markers on Wrapped Items

CSS / Reference / list-style-position: Inside and Outside Markers on Wrapped Items

Choose marker placement using wrapped text, list spacing, and actual child structure. Preserve list meaning and diagnose clipped outside markers.

CSS / Reference / empty-cells: Show or Hide Empty Table Cells

CSS / Reference / empty-cells: Show or Hide Empty Table Cells

Control empty-cell borders and backgrounds in a separated table while preserving its structure. Compare show, hide, whitespace, and the collapsed-border exception.

CSS / Reference / text-shadow: Glyph Shadows, Layers, and Contrast

CSS / Reference / text-shadow: Glyph Shadows, Layers, and Contrast

Add text shadows without box-shadow-only syntax. Compare blur and layer order, preserve legibility, and avoid treating decoration as a contrast guarantee.

CSS / Reference / align-content: Flex Lines, Grid Tracks, and Available Space

CSS / Reference / align-content: Flex Lines, Grid Tracks, and Available Space

Distribute available space in flex, grid, and supported block layouts. Compare wrap with nowrap, fixed with auto tracks, and content-group alignment with item alignment.

CSS / Reference / box-sizing: Content and Border Boxes, Including Small-Size Limits

CSS / Reference / box-sizing: Content and Border Boxes, Including Small-Size Limits

Calculate content-box and border-box sizes, including margins and the minimum content-size limit. Apply resets deliberately and check external components.

CSS / Reference / line-height: Unitless Ratios, Inherited Lengths, and Line Boxes

CSS / Reference / line-height: Unitless Ratios, Inherited Lengths, and Line Boxes

Choose scalable line-height values and compare ratio inheritance with fixed lengths. Separate line boxes from paragraph margins and test larger child text.

CSS / Reference / background-origin: Positioning References Versus Painting Boundaries

CSS / Reference / background-origin: Positioning References Versus Painting Boundaries

Choose a background image’s positioning box and distinguish it from background-clip. Compare how padding, borders, percentage sizes, and fixed attachment affect the reference.

CSS / Reference / text-decoration: Decoration Propagation, Underlines, and Link Meaning

CSS / Reference / text-decoration: Decoration Propagation, Underlines, and Link Meaning

Style text decoration and compare parent-originated lines with child rules. Keep links identifiable and separate meaningful deletions from visual strike-through.

CSS / Reference / background-clip: Background Painting Areas and Text Clipping

CSS / Reference / background-clip: Background Painting Areas and Text Clipping

Choose border, padding, or content painting areas independently of image positioning. Compare visible boundaries and plan readable fallbacks for text-clipped backgrounds.