• 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.

Video games folks — and machines — play: Untangling strategic reasoning to advance AI | MIT Information

Video games folks — and machines — play: Untangling strategic reasoning to advance AI | MIT Information

May 6, 2026
Is Your Gross sales Workforce Responsible of AI-Washing? A CRO’s Information to AI Brokers, Assistants, and Precise ROI

Is Your Gross sales Workforce Responsible of AI-Washing? A CRO’s Information to AI Brokers, Assistants, and Precise ROI

October 30, 2025

Trending.

Nsfw Chatgpt Options – Examples I’ve Used

Nsfw Chatgpt Options – Examples I’ve Used

October 13, 2025
Digital Detox & Display Time Statistics 2025

Digital Detox & Display Time Statistics 2025

March 28, 2026
How creators and entrepreneurs are utilizing AI to hurry up & succeed [data]

How creators and entrepreneurs are utilizing AI to hurry up & succeed [data]

June 17, 2025
All Overwatch 2 Dokiwatch Skins, Title Playing cards, And Cosmetics

All Overwatch 2 Dokiwatch Skins, Title Playing cards, And Cosmetics

April 24, 2025
What’s a Ahead Deployed Engineer: The AI Position OpenAI, Anthropic, and Google Are Hiring in 2026

What’s a Ahead Deployed Engineer: The AI Position OpenAI, Anthropic, and Google Are Hiring in 2026

May 21, 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

33 Immortals: Finest Perks

33 Immortals: Finest Perks

June 17, 2026
Modular Phishing Equipment Makes use of GitHub Pages to Steal Fee Card Particulars and Passwords

Modular Phishing Equipment Makes use of GitHub Pages to Steal Fee Card Particulars and Passwords

June 17, 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