-
Notifications
You must be signed in to change notification settings - Fork 23k
css(add): contrast-color() function page #39847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+193
−2
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a203547
css(add): contrast-color() function page
OnkarRuikar 74c1a02
Apply suggestions from code review
Josh-Cena 4d57b3b
remove livesample markers
OnkarRuikar 54b8ec2
add color picker to the example
OnkarRuikar 0bc9bc6
add prefers-color-scheme example
OnkarRuikar e730c6e
Apply suggestions from code review
OnkarRuikar f7873d1
move extra content to the description section
OnkarRuikar 63a9430
Apply suggestions from code review
OnkarRuikar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
files/en-us/web/css/color_value/contrast-color/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| --- | ||
| title: contrast-color() | ||
| slug: Web/CSS/color_value/contrast-color | ||
| page-type: css-function | ||
| status: | ||
| - experimental | ||
| browser-compat: css.types.color.contrast-color | ||
| --- | ||
|
|
||
| {{CSSRef}}{{SeeCompatTable}} | ||
|
|
||
| The **`contrast-color()`** functional notation takes a {{cssxref("color_value","color")}} value and returns a [guaranteed](https://www.w3.org/TR/WCAG21/#contrast-minimum) contrasting color. The function makes it easy to specify text color when background colors are created dynamically. It saves developers from maintaining background-text color pairs. It is not limited to deriving text colors; you can use it for deriving colors of anything, such as border, background, etc. | ||
|
|
||
| The function produces only `white` or `black` value, depending on which value produces maximum contrast with the input color. If both white and black colors produce the same contrast, then the `white` value is returned. | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| > [!WARNING] | ||
| > There is no guarantee that the resulting colors obtained using the `contrast-color()` function will always be accessible. For example, generally, mid-tone background colors don't result in enough contrast. It is recommended to use clearly-light or clearly-dark colors with the `contrast-color()` function. | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ## Syntax | ||
|
|
||
| ```css | ||
| contrast-color(red) | ||
| contrast-color(var(--backgroundColor)) | ||
| ``` | ||
|
|
||
| ### Values | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - `color` | ||
| - : Any valid {{cssxref("<color>")}}. | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
OnkarRuikar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### Examples | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #### Contrasting text for a button | ||
|
|
||
| In the following example when you change background color of the button, the webbrowser automatically applies a contrasting color to the `Submit` text. | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```html hidden | ||
| <label> | ||
| Select background color of the button: | ||
| <input type="color" id="colorPicker" value="#660066" /> | ||
| </label> | ||
| <br /> | ||
| <button>Submit</button> | ||
| ``` | ||
|
|
||
| ```css | ||
| :root { | ||
| --button-color: lightblue; | ||
| } | ||
|
|
||
| button { | ||
| background-color: var(--button-color); | ||
|
|
||
| /* set contrasting text color automatically */ | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| color: contrast-color(var(--button-color)); | ||
| } | ||
| ``` | ||
|
|
||
| ```css hidden | ||
| body { | ||
| padding: 1rem; | ||
| } | ||
|
|
||
| button { | ||
| margin: 3rem; | ||
| padding: 1rem; | ||
| width: 150px; | ||
| font-size: 2rem; | ||
| border-radius: 1rem; | ||
| } | ||
|
|
||
| @supports not (color: contrast-color(red)) { | ||
| body::before { | ||
| content: "Your browser doesn't support the contrast-color() function."; | ||
| background-color: wheat; | ||
| display: block; | ||
| width: 100%; | ||
| text-align: center; | ||
| } | ||
|
|
||
| body > * { | ||
| display: none; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```js hidden | ||
| const colorPicker = document.getElementById("colorPicker"); | ||
| const root = document.documentElement; | ||
|
|
||
| function updateColor(color) { | ||
| root.style.setProperty("--button-color", colorPicker.value); | ||
| } | ||
|
|
||
| colorPicker.addEventListener("change", updateColor); | ||
| updateColor(); | ||
| ``` | ||
|
|
||
| {{EmbedLiveSample("Contrasting text for a button", "", 250)}} | ||
|
|
||
| #### Light and dark mode usage | ||
|
|
||
| In the following example [`prefers-color-scheme`](/en-US/docs/Web/CSS/@media/prefers-color-scheme) [media query](/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries) has been used to set background color based on operating system or browser color scheme/theme setting. The `contrast-color()` function sets text color automatically. | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In the browser setting try changing the dark mode setting to see the effect. | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```html hidden | ||
| <pre> | ||
| Q: How does CSS transform light into energy? | ||
| Ans: Using <a href="/en-US/docs/Web/CSS/font-synthesis">font-synthesis</a>. | ||
| </pre> | ||
| ``` | ||
|
|
||
| ```css | ||
| :root { | ||
| --background-color: navy; | ||
| } | ||
|
|
||
| @media (prefers-color-scheme: light) { | ||
| :root { | ||
| --background-color: wheat; | ||
| } | ||
| } | ||
|
|
||
| body, | ||
| a { | ||
| background-color: var(--background-color); | ||
| color: contrast-color(var(--background-color)); | ||
| } | ||
| ``` | ||
|
|
||
| ```css hidden | ||
| body { | ||
| padding: 2rem; | ||
| font-size: 1.2rem; | ||
| } | ||
|
|
||
| pre { | ||
| margin-top: 3rem; | ||
| } | ||
|
|
||
| @supports not (color: contrast-color(red)) { | ||
| body::before { | ||
| content: "Your browser doesn't support the contrast-color() function."; | ||
| background-color: wheat; | ||
| display: block; | ||
| width: 100%; | ||
| text-align: center; | ||
| } | ||
|
|
||
| body { | ||
| background-color: white; | ||
| } | ||
|
|
||
| body > * { | ||
| display: none; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| {{EmbedLiveSample("Light and dark mode usage", "", 250)}} | ||
|
|
||
| ## Specifications | ||
OnkarRuikar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| {{Specifications}} | ||
|
|
||
| ## Browser compatibility | ||
|
|
||
| {{Compat}} | ||
|
|
||
| ## See also | ||
|
|
||
| - [How to have the browser pick a contrasting color in CSS](https://webkit.org/blog/16929/contrast-color/) | ||
OnkarRuikar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - [WebAIM Contrast Checker](https://webaim.org/resources/contrastchecker/) | ||
| - {{cssxref("color_value", "<color>")}} data type | ||
| - [WCAG: color contrast](/en-US/docs/Web/Accessibility/Guides/Understanding_WCAG/Perceivable/Color_contrast) | ||
| - [CSS colors](/en-US/docs/Web/CSS/CSS_colors) module | ||
| - [`prefers-contrast`](/en-US/docs/Web/CSS/@media/prefers-contrast) and [`prefers-color-scheme`](/en-US/docs/Web/CSS/@media/prefers-color-scheme) {{cssxref("@media")}} features | ||
| - [`contrast()`](/en-US/docs/Web/CSS/filter-function/contrast) | ||
| - [CSS custom properties](/en-US/docs/Web/CSS/--*) and {{cssxref("var")}} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.