What are some ways to offer a visually striking high contrast option for a pastel color palette that is also easily

Is there a way to balance a website's color theme with high contrast options while still leaning towards a pastel, low-contrast design unless specifically requested by the user? How can we ensure compliance with minimum contrast requirements outlined in WCAG 2 guidelines?

I attempted to establish a fallback theme with higher contrast and provide a lower contrast version for users who do not require enhanced contrast, utilizing the prefers-contrast media query. However, my example (also available on codepen here) failed an accessibility audit due to a foreground color of #eeeeee against a background color of #f26600 resulting in a low contrast ratio of 2.71.

What CSS modifications are necessary to establish a suitable fallback option? How can users communicate their contrast preferences, and is it possible for browsers or operating systems to adjust contrast settings based on various factors such as daylight, dark/light themes, or ambient light sensors?

p {
  background-color: #f26600;
  color: #eeeeee;
}

@media (prefers-contrast: more) {
  p {
    background-color: #aa3300;
    color: #ffffff;
  }
}

I also endeavored to invert the logic in the code snippet to prioritize high contrast as the default instead of vice versa: see codepen.io/openmindculture/pen/eYVPgoo. While this version passed without contrast warnings, will it ever display the pastel version? How can I indicate a preference for less contrast, and how can this be reflected to the user?

Answer №1

That’s a truly thought-provoking question.

Introducing an Alternative Contrast Variation

Including a control with adequate contrast ratio for users to switch to a display with ample contrast is actually considered a valid technique to fulfill WCAG's contrast standards.

However, there are certain requirements that must be met:

  1. The link or control on the original page must also adhere to the required contrast levels of the designated success criterion. (If the user cannot perceive the control, they may not be able to utilize it to navigate to the new page.)
  2. The new page must encompass all the same content and functionality as the original page.
  3. The new page should comply with all the success criteria for the targeted level of conformity. (In other words, the new page cannot only focus on achieving the desired contrast level without adherence to other guidelines).

If your goal is simply to switch styles, it is probable that conditions 2 and 3 would be fulfilled. You would just need to incorporate a button for switching between styles.

You could create a second stylesheet (.css file) explicitly defining the contrast version. Here's how you can implement this:

<link href="contrast.css" rel="alternate stylesheet" type="text/css" title="Contrast Styles">

This approach allows browsers supporting this feature (such as Firefox) to offer users the option to switch styles. Refer to Alternative style sheets

Subsequently, you can include a button in your webpage that activates this stylesheet via JavaScript, along with a media query importing it if @media (prefers-contrast: more).

It remains unclear whether your audit tools will grant approval while the alternate stylesheet is active. Ultimately, a manual review is necessary for legal compliance, ensuring conformance with 1.4.3.

How Can Users Specify Their Contrast Preference?

In the past, there was a debate about whether a font-size control should be embedded into websites, given its long-standing availability within browsers. While WCAG did not mandate it (to my knowledge), proponents argued that many users were unfamiliar with browser settings.

A similar argument can be made for contrast modes (as many users might not be well-versed in OS contrast settings or lack access to them), which is why WCAG now requires a control regardless.

Is There a Method for Browsers or Operating Systems to Adjust Contrast Preferences Based on Daylight Conditions, Themes, or Ambient Light Sensors?

In Android, there are options to utilize the dark theme setting based on daylight circumstances, and possibly some applications permitting adjustment according to ambient light levels.

To my knowledge, there is no mechanism enabling contrast mode adjustments based on these factors, not even in Windows 11, which has significantly enhanced its accessibility contrast themes.

It could be argued that such adaptations signify the user's need for heightened contrast, irrespective of day or night, light or dark themes.

Answer №2

To work around this issue, one approach is to set the default color scheme to high contrast in CSS and then use JavaScript to introduce alternative pastel colors.

By utilizing a timeout, we can avoid false positive color contrast warnings from validation tools that do not account for the fact that we are accommodating the user's contrast preferences. This method ensures that the audit tool completes its assessment before the alternate colors are applied. Implementing CSS transitions creates a smooth transition effect, enhancing the overall design aesthetic of the website.

The solution involves avoiding the prefers-contrast media queries in CSS, which addresses two key challenges:

  • Browsers often ignore @media (prefers-contrast: less), and
  • Validation tools such as the axe browser plugin require sufficient high contrast even when default style rules do not rely on (prefers-contrast: more) media queries.

We have several options for switching to pastel colors:

  • Loading a different CSS file,
  • Adjusting CSS custom properties, or
  • Removing specific class names.

In the example code snippet below, which will pass the axe color contrast audit, I have incorporated two special CSS class names:

  • contrast--more to apply the initial high contrast colors, and
  • contrast--varies to highlight elements with customizable color options and trigger a CSS transition for those elements.
.contrast--varies {
  transition: background-color 2s ease-out, color 2s ease-out;
}

document.addEventListener('DOMContentLoaded', () => {
      const prefersMoreContrastQuery = window.matchMedia("(prefers-contrast: more)");
  if (prefersMoreContrastQuery && !prefersMoreContrastQuery.matches) {
    window.setTimeout(() => {
      const moreContrastElements = document.getElementsByClassName("contrast--varies");
      for (let i = 0; i < moreContrastElements.length; i++) {
         moreContrastElements[i].classList.remove("contrast--more");
      }
    }, 5000);
  }
});
.contrast--varies {
  transition: background-color 2s ease-out, color 2s ease-out;
}

.pastelboxes div:first-child {
  color: #fff;
  background: #dbcc7c;
}

.pastelboxes div:nth-child(2) {
  color: #fff;
  background: #e2d7c1;
}

.pastelboxes div.contrast--more:first-child {
  background-color: #9a8800;
}

.pastelboxes div.contrast--more:nth-child(2) {
  background-color: #966e37;
}

.pastelboxes div {
  padding: 1em;
  font-size: 24px;
  font-weight: bold;
}
<div class="pastelboxes"gt;
  <div class="contrast--varies contrast--more">
    Some like pastel colors...
  </div>

  <div class="contrast--varies contrast--more">
    ...but illegible text for many people?!
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Apply the CSS rule specifically for desktop screens, excluding mobile and tablet devices

My website has a logo in the header that appears too small on computer screens. I came across a code snippet that allows me to increase the size of the logo, but I only want this change to be applied on computers and not on mobile or tablet devices. The co ...

"Create a notification pop-up in CSS that appears when a link is clicked, similar to

I am looking to create a model page that functions similarly to the inbox popup on Stack Overflow. When we click on the inbox icon, a small box appears with a tiny loader indicating messages or comments. The box then expands depending on the content inside ...

Avoid passing down line-height settings to elements within nested elements

Is there a way to prevent the span styled with font-size: 12px from inheriting the line-height of the span styled with font-size:18px? I need the line-height value to be 1.5 for both 18px and 1.5px font sizes, but I am unable to separate the two spans. ht ...

Is it possible to use an SVG image as a border with a variable height in CSS

Check out this jsfiddle for my current project's nav setup. I want to center the logo in the middle of the nav with three links centered around it. However, I need the design to be responsive across various screen widths. Any advice on how to achieve ...

Aligning SVG clipping path using CSS

This problem has me stumped! I need to center the SVG clip within the surrounding div, just like in this example: http://jsfiddle.net/ztfdv9qh/ - Make sure to check the link because the SVG is quite long! <div class="svg-wrapper"> <div class ...

What is the best way to access the width of the top-level parent div and adjust the widths of its child and parent divs according

I'm currently working on creating a custom directive in Angular 4 to enable resizing of a div. Here's the HTML code snippet: <div class="super-parent"> <div class="parent"> My Div content <div class="child" re ...

Using regular expressions to eliminate the width attribute from tables

Currently, I am carrying out some HTML processing before storing the data in the database. If a user pastes content containing HTML tables, I need to eliminate certain tags and attributes. To extract the table content, I am using content.match('<t ...

Puppeteer challenge with breaking images

When generating a PDF from HTML, I am facing an issue where the signature image breaks on another page. Is there a way to ensure that if content or images break, they will move to another PDF page? Puppeteer version - 3.3.0 Node version - 12.16.1 Check t ...

My website has a navbar button that is not directing to the intended section of the screen

I have created this HTML code for my Navbar buttons: <button class="navbar-toggle" data-toggle = "collapse" data-target=".navHeaderCollapse"> <span class="icon-bar"></span> <span class="icon-bar">< ...

I am attempting to create a presentation featuring the identical title

How can I enhance this code by turning it into a slideshow with additional images while maintaining the h1 and h2 tags within it? Here's the existing code displaying an image with titles overlayed on top: <header class="header-image"> <d ...

I am facing issues with the CSS in my EJS file when it is being rendered within an if statement

Having trouble applying CSS to this EJS file, no matter what styling I use. Here's the code snippet: itemEdit.ejs <%- include("partials/header.ejs", {title: ` - Items`, body_id: `items`} )%> <%- include("partials/navbar.ejs&qu ...

Div Tag Failing to Render Image

It seems like the path for the background image is correct, as it worked in the HTML file. However, the image is not being displayed. Can anyone help troubleshoot this issue? HTML <head> <title>2013 YourFantasyFootball</title> <lin ...

Arrange the position of a div based on the location of another div

I'm working on a webpage that has 3 divs. The first one, labeled as "A," is positioned at the top of the screen, while the second one, labeled as "B," is centered on the screen. My goal is to always position the third div, labeled as "C," according t ...

How can I adjust the transparency in a JavaScript popup modal window for an ASP.Net GridView?

Recently, I added an 'onclick' event to every row of an asp gridview and the popup window that appears is functioning perfectly. Now, I'm interested in adding a transparency level to the body of the popup window for a translucent effect. Can ...

Placing a div with position:absolute inside another div with position:absolute and turning them into position:fixed

HTML <div class="box1"></div> <div class="box2"> <div class="box3"></div> </div> CSS Properties of box1, box2 and box3 are: box1 { position: absolute; z-index: 100; background-color: rgba(39, 39, 39, 0.3) ...

Changing the Image Path According to the Device's Retina Ratio

I'm currently setting up a website for a photographer and working on creating a gallery. To streamline the process, I have automated many aspects such as file names, paths, widths, lightbox classes, etc. All I have to do in the HTML is write an <a ...

Creating CSS layouts without the need for using the ctrl+space function

Currently, when I am working in a CSS file and begin typing, if I press ctrl + space, I receive autocomplete suggestions. Is there a way to enable this feature without needing to press ctrl + space each time? ...

Adjusting the width of titles in a CSS menu upon hover

I am currently creating a basic CSS menu. However, I am facing an issue where selecting a menu title in the menu bar causes the width of the title to extend to match the width of the associated menu, which is not my desired outcome (the title's width ...

Contrasting behavior shifts between in-line style declaration and class usage

My goal is to create a grid of boxes, each containing information about different dog breeds. However, I've encountered an issue with styling that has left me puzzled. When I apply a style in-line versus using the same style within a class, the behavi ...

Font-face src with local() is incompatible with Android devices

(I apologize for not discovering this earlier, but it seems that Chrome-Android also does not support the font I intended to use. The fallback-to-sys-default-font-trick used by Chrome-Android did fool me.) I had planned to incorporate unicode-range to add ...