CSS contrast-color()関数ジェネレーターとは
The CSS contrast-color() function (Baseline 2026) selects the color with the highest contrast ratio from a list of candidates against a specified background. With contrast-color(background vs color1, color2, ...), you provide the background color and a list of candidate colors. The function evaluates the WCAG contrast ratio of each candidate against the background and returns the candidate with the best ratio. The optional to max keyword tells the function to always return the maximum-contrast candidate. This is a native CSS alternative to JavaScript-based text color switching for readable overlays, buttons, and badges.
クイックアンサー
Use CSS contrast-color(background vs candidates...) to automatically select the most readable color from a list. Works in Chrome 132+, Edge 132+, Safari 19+, and Firefox 136+. Supports to max mode for maximum contrast or priority-list order for branded color preference. Use with CSS custom properties for design system integration.
制限事項
- contrast-color() is not supported in browsers before Chrome 132, Safari 19, or Firefox 136. Always provide a fallback color before the contrast-color() declaration, and use @supports to conditionally apply the function where available.
- The function evaluates contrast based on relative luminance, which is a mathematical approximation of human color perception. Two colors with identical contrast ratios may subjectively read differently depending on hue, saturation, and surrounding context.
- contrast-color() selects a single color from the candidate list. It cannot generate intermediate colors, blend candidates, or adjust opacity. For fine-grained color adjustment beyond picking from a list, use CSS color-mix() or manual color tokens.
使い方
- Enter the background color against which contrast will be measured. This can be a hex, rgb, hsl, or named color value.
- Add candidate colors — the function evaluates each one against the background and picks the best match. You can reorder candidates for the to max mode priority fallback.
- Choose the contrast mode: to max always picks the highest-contrast candidate, or omit it to use ordered priority (first candidate above the minimum ratio wins).
- Copy the generated contrast-color() expression into your stylesheet and replace the candidate colors with your design token references.
主な用途
- Style text overlays on hero images and banners where the text must remain readable regardless of the background color or image.
- Build badge and tag components that automatically adjust label text color based on the badge background color.
- Create a dynamic button system where button text color adapts to the primary button background without writing multiple color variants.