• About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us
AimactGrow
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
AimactGrow
No Result
View All Result

contrast-color() | CSS-Methods

Admin by Admin
May 3, 2026
Home Coding
Share on FacebookShare on Twitter


The CSS contrast-color() perform takes a  worth (in addition to a variable) and returns both black or white, whichever is probably the most contrasting colour for that worth.

In different phrases, contrast-color() is form of an accessibility device for conforming to WCAG distinction necessities.

.card {
  background-color: var(--swatch);
  colour: contrast-color(var(--swatch));
}

For instance, on the following demo replace the background colour to see the textual content colour change routinely.

The contrast-color() perform is outlined within the CSS Shade Module Degree 5 specification.

Syntax

The CSS contrast-color() perform syntax is is formatted like this:

contrast-color() = contrast-color(  )

Let’s break that down with examples.

Arguments

/* Utilizing a customized variable */
contrast-color(var(--base-background));

/* Passing a colour instantly */
contrast-color(#34cdf2);
contrast-color(inexperienced);

contrast-color() takes a  as its solely argument and resolves to white or black, relying on which has the very best distinction. If each white and black have the identical distinction stage, the perform defaults to white.

Fundamental utilization

The contrast-color() give us a easy different to defining a number of background and textual content colours, whereas additionally making certain they’re contrasting sufficient. Think about we had the next state of affairs:

:root {
  --primary-text: #f1f8e9;
  --primary-bg: #2d5a27;
  --secondary-text: #311b92;
  --secondary-bg: #d1c4e9;
  --tertiary-text: #002b36;
  --tertiary-bg: #ff5722;
}

.major {
  colour: var(--primary-text);
  background-color: var(--primary-bg);
}

.secondary {
  colour: var(--secondary-text);
  background-color: var(--secondary-bg);
}

.tertiary {
  colour: var(--tertiary-text);
  background-color: var(--tertiary-bg);
}

We outlined a textual content colour for every background colour in our variables, and if we had greater than three potential backgrounds, we’d have needed to outline all of them. As an alternative, utilizing contrast-color(), we might outline solely the background colour for every theme and let the perform return the suitable contrasting colour for the texts.

:root {
  --primary: #2d5a27;
  --secondary: #d1c4e9;
  --tertiary: #ff5722;
}

.major {
  colour: contrast-color(var(--primary));
  background-color: var(--primary);
}

.secondary {
  colour: contrast-color(var(--secondary));
  background-color: var(--secondary);
}

.tertiary {
  colour: contrast-color(var(--tertiary-bg));
  background-color: var(--tertiary-bg);
}

It is very important notice that contrast-color() continues to be a piece in progress (on the time of this writing), and in some circumstances may not be acceptable from a design standpoint because it solely returns black or white. Subsequently, I like to recommend utilizing it solely in easy eventualities the place both black or white make sense.

The truth is, it has some shortcomings which might be price noting.

contrast-color() shortcomings

Whereas contrast-color() seems to enhance net accessibility, it has buts we should always concentrate on earlier than utilizing it.

  • It resolves to solely black or white texts. Though the draft guarantees extra management sooner or later, we now have to stay to these two colours for now.
  • We’re caught with white when utilizing colours the place neither black nor white is a ample distinction, or they each have the identical distinction.
  • contrast-color() solely works with colours for now. So, in circumstances the place you’re working with textual content on background photographs or utilizing font weights to extend distinction, you’ll should discover a completely different strategy to meet distinction necessities. And even when it may be technically used with gradients, these can also solely go between black to white which could not present sufficient distinction between the gradient colours.
  • contrast-color() doesn’t account for the font-size, which is a defining criterion, in selecting a distinction colour. Hopefully, this shall be accounted for sooner or later.

So, on the time of writing, it appears it’s higher to manually outline colours which might be contrasting sufficient in our themes as contrast-color() isn’t actually possible proper now.

Older syntax

Based mostly on earlier articles, the contrast-color() perform used to take a number of colour arguments–the bottom colour versus a number of contrasting colour choices to select from:

contrast-color(var(--bg) vs crimson, lightgreen, blue)

This syntax now not exists within the draft. It’s one colour and one colour solely.

Specification

The contrast-color() perform is outlined within the CSS Shade Module Degree 5 specification.

Browser help

Whereas browser help is restricted on the time of this writing, it’s a good suggestion to incorporate a fallback in case you’re planning to apply it to a mission. We will use the @helps at-rule to detect if the browser understands the perform:

.card {
  --bg-color: #2d5a27;
  background-color: var(--bg-color);

  /* Default Fallback */
  colour: ghostwhite;
}

/* Use the perform if supported */
@helps (colour: contrast-color(crimson)) {
  .card {
    colour: contrast-color(var(--bg-color));
  }
}

Additional studying:

Associated

Tags: contrastcolorCSSTricks
Admin

Admin

Next Post
AT&T Simply Reshuffled Its Limitless Lineup. Here is What You are Getting (and Paying)

AT&T Simply Reshuffled Its Limitless Lineup. Here is What You are Getting (and Paying)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

The Finest Printers for House and Workplace: Brother, HP, and Extra

The Finest Printers for House and Workplace: Brother, HP, and Extra

December 8, 2025
Pokémon Go Grookey Neighborhood Day occasion information

Pokémon Go Grookey Neighborhood Day occasion information

January 18, 2026

Trending.

Backrooms director Kane Parsons explains the birds, the portals, and his sensible results

Backrooms director Kane Parsons explains the birds, the portals, and his sensible results

May 31, 2026
100 Most Costly Key phrases for Google Advertisements in 2026

100 Most Costly Key phrases for Google Advertisements in 2026

January 13, 2026
Resident Evil followers have adopted a Love & Deepspace character because the son of Leon S. Kennedy and one in every of his potential spouses

Resident Evil followers have adopted a Love & Deepspace character because the son of Leon S. Kennedy and one in every of his potential spouses

April 4, 2026
Random Forest Algorithm in Machine Studying With Instance

Random Forest Algorithm in Machine Studying With Instance

May 4, 2025
The Full Information to EcoGPT

The Full Information to EcoGPT

June 6, 2026

AimactGrow

Welcome to AimactGrow, your ultimate source for all things technology! Our mission is to provide insightful, up-to-date content on the latest advancements in technology, coding, gaming, digital marketing, SEO, cybersecurity, and artificial intelligence (AI).

Categories

  • AI
  • Coding
  • Cybersecurity
  • Digital marketing
  • Gaming
  • SEO
  • Technology

Recent News

Undertaking possession (fairness and fairness)

The Lego downside, revisited | Seth’s Weblog

August 1, 2026
Stuxnet-linked Fast16 malware, designed to subvert nuclear weapons testing simulations, was seemingly a part of a marketing campaign to sluggish Iran’s nuclear ambitions (Kim Zetter/ZERO DAY)

This yr’s Defcon badges embody Baochip-1x, an open supply chip whose safety is verifiable and that will also be used as a {hardware} safety token (Kim Zetter/Wired)

August 1, 2026
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved

No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved