The availability of media features that enable developers to check and respect user preferences is an example of just one of the ways that CSS continues to evolve. The prefers-color-scheme
media feature detects dark and light mode preferences, while prefers-reduced-motion
checks if the user has animations disabled on their operating system. There are many options like this available, although some have more browser support than others. In this article, we’ll focus on a media feature that is gaining support, and that I believe has great potential to customize user experience and improve accessibility: prefers-contrast
. We’ll investigate how this media feature can be used and discuss why it’s important to provide users with options to control website contrast. We’ll also discuss the limitations of prefers-contrast
and the different contrast considerations you’ll need to keep in mind before implementing it in your project. Finally, we’ll integrate everything with JavaScript to create a contrast theme selector that can be used even if an operating system does not support high- or low-contrast settings.
Jump ahead: What is prefers-contrast
?
A media feature is a type of media query that is used to check a device’s specific characteristics and settings. It helps detect the presence or value of many expressions, such as prefers-color-scheme
and prefers-reduced-motion
. There are many media features, including some very obscure ones; for a full list, check out the list of existing media features on MDN.
The prefers-contrast
media feature helps to detect if a user’s operating system has a specific contrast setting that should be reflected on the browser. This media query has four values:
– no-preference
: checks if there is no preference active
– more
: checks if the device has an active option that increases contrast
– less
: checks if the device has an active option that reduces contrast
– custom
: checks if the device has an option that lets the user adjust the colors. This will match with the color pallet the user has chosen for Windows high-contrast mode and the forced-colors: active
media feature
Let’s investigate these media features by looking at an example. We’ll create a button with a focus style, and add different styles if the user has higher or lower contrast options selected on their device:
<style>
:root {
--button-bg: #693FAB;
--button-text: whitesmoke;
--outline-style: solid;
--font-weight: 400;
}
.button {
padding: 0.5em 1.5em;
font-size: 2rem;
border: 2px solid transparent;
background-color: var(--button-bg);
color: var(--button-text);
font-weight: var(--font-weight);
font-family: 'Roboto', sans-serif;
}
.button:focus-visible {
outline: 3px var(--outline-style) var(--button-bg);
outline-offset: 3px;
}
</style>
<button class="button">Click me</button>
<style>
@media screen and (prefers-contrast: more) {
:root {
--button-bg: #472b73;
--font-weight: 500;
}
}
@media screen and (prefers-contrast: less) {
:root {
--button-bg: #835bc2;
--outline-style: dashed;
--font-weight: 300;
}
}
</style>
There are a couple of ways to see the changes we implemented. One option is to go to your operating system settings and change a contrast option. We’ll talk more about this option later in the article. For now, we’ll use Google DevTools to check our changes. Open DevTools and click the Rendering tab. Next, choose one of the following options:
– Press Command+Shift+P (Mac) or Control+Shift+P (Windows, Linux, ChromeOS), select Rendering and then Show Rendering
Or
– Select the Customize and control DevTools menu (the three dots, or ellipses, at the upper right of the window), then select More Options and then Rendering
Either of the above options will bring you to the Emulate CSS media feature prefers-contrast menu. From here, you can select the contrast setting you want to emulate.
By now, you should have a better understanding of how the prefers-contrast
media feature works. Next, let’s discuss why contrast is important and how to choose an appropriate color combination to create an appropriate contrast theme.
Minimum contrast guidelines
Color contrast, or the difference in the perceived brightness of two colors, is a very important part of design and accessibility. If the contrast between the text and background is too low, it can create serious readability issues. WCAG 2.0 introduced a contrast guide to help designers and developers determine if a contrast is adequate and make good choices when picking colors. This contrast algorithm creates a value between 1 and 21; the higher the ratio, the greater the contrast between colors. WCAG also created two success criteria, SC 1.4.3 – Contrast (Minimum) (Level AA) and SC 1.4.6 – Contrast (Enhanced) (Level AAA), to provide designers and developers with a contrast ratio to determine if a color combination is accessible. According to SC 1.4.3 (AA level projects), the minimum contrast for text is 4.5:1 for regular text and 3:1 for larger text (the equivalent of 18px). SC 1.4.6 (AAA level projects) increases those requirements to 7:1 for regular text and 4.5:1 for larger text.
Maximum contrast guidelines
WCAG provides minimum contrast requirements but doesn’t provide guidance about maximum contrast. To determine a maximum value, we first need to understand why different people have different contrast requirements. It’s not hard to understand why a contrast ratio below 4.5:1 is problematic: people with low vision may struggle to read a website’s content. It’s also easy to understand why a higher requirement exists on SC 1.4.6: AA contrast requirements are not sufficient for some visual disabilities, so it makes sense to have a success criterion with a higher threshold for people with more severe visual impairments. You might think that higher contrast is always better, but sometimes too much contrast can lead to poor user experience. Here are a few examples:
– Users with astigmatism may experience a halation effect with high-contrast text, making the text appear blurry and difficult to read
– People with photophobia can have migraines induced by high-contrast colors on a computer or device
– Users with albinism may experience light sensitivity triggered by high-contrast colors
A contrast ratio that is too high can be harmful for some users, but there’s no clear consensus as to the maximum contrast ratio that we should consider. Fortunately, I found a good rule of thumb from the wonderful A11y Slack Community. There’s a GitHub issue in the WCAG’s repository that quotes a Human Factors Design Standard (HFDS) guide that makes the following mention about contrast:
8.2.5.6.12 Character contrast. For optimum legibility, character contrast should be between 6:1 and 10:1. [Source: National Air Traffic Services, 1999]
This maximum contrast ratio of 10:1 is roughly equivalent to a 15:1 ratio for WCAG standards, giving us a good rule of thumb to use for maximum contrast. This guideline is not a definitive answer, but it gives us a good starting point for thinking about a maximum contrast ratio.
To summarize, maintaining a minimum contrast ratio of 4.5:1 and a maximum contrast value of 15:1 is a good starting reference for most projects.
Considerations for using prefers-contrast
The prefers-contrast
media feature is useful for implementing high- and low-contrast options for websites, but as of this writing, it is not fully supported. The implementation of the prefers-contrast
media feature is quite recent, and some implementation details need to be considered before adding it to production.
Browser support
According to caniuse.com, all major browsers accept prefers-contrast
, but as the implementation is quite recent, we can’t rely on every user having their browser updated to the last version. This is where progressive enhancement comes into play. We can create a high- and low-contrast theme with prefers-contrast
. If the user’s browser doesn’t support this media feature, they won’t be able to use it, but they will still see a good base experience.
How operating systems handle contrast settings
Let’s circle back to an issue we touched on previously: understanding how operating systems handle contrast settings. TetraLogical published an article, Meeting WCAG Level AAA, that discusses creating a theme with higher contrast using prefers-contrast
. The article demonstrates how to activate a higher contrast with different operating systems. Here’s a summary of the findings:
– macOS, iOS, and Ubuntu have accessibility options to toggle a high-contrast mode, and that is detected by browsers by using prefers-contrast
– Android has a toggle of a high-contrast mode, but that might not be detected by browsers using prefers-contrast
Full credit to the original article: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Colors/Color_management_in_CSS/The_prefers-contrast_media_feature
Source link