1

I'm trying to style a high contrast version of a site. The following code works fine for IE and Edge, but I need to detect Firefox.

@media screen and (-ms-high-contrast: active) {}

I could also use JavaScript detection for high contrast and Firefox. Any advice?

Super Jade
  • 5,609
  • 7
  • 39
  • 61
UserEsp
  • 415
  • 1
  • 7
  • 29
  • This question appears to have been answered here: https://stackoverflow.com/questions/55241841/high-contrast-mode-on-mozilla-browser – Matt Wilson Oct 18 '20 at 21:38

1 Answers1

0

I'm trying to style high contrast version of a site... ..need a detection for Firefox


The high contrast media query that you're using,

@media screen and (-ms-high-contrast: active) {}

works only for IE and Edge Legacy browsers. (The -ms prefix stands for Microsoft.)


Solution:

  1. Create a JavaScript test for Windows 10 High Contrast mode. This function adds a div of an unusual color, and tests to see if it the color changes once it's appended to the DOM. If the color changes, you're in high contrast mode! (This will also work for IE and Edge Legacy.)
highContrastMode = () => {
  const testDiv = document.createElement('div');
  testDiv.style.color = 'rgb(200, 100, 50)';
  document.body.appendChild(testDiv);
  const color = document.defaultView!.getComputedStyle(testDiv, null).color;
  document.body.removeChild(testDiv);

  return color !== 'rgb(200, 100, 50)' ? true : false;
}
  1. Create a user agent test for Firefox:
const isFirefox = window.navigator.userAgent.indexOf('Firefox') > -1;
  1. Then apply your CSS!
Super Jade
  • 5,609
  • 7
  • 39
  • 61