Looking to customize a high contrast version of a website, I am in need of a way to detect this for Firefox.
The media query you are currently using for high contrast,
@media screen and (-ms-high-contrast: active) {}
is only effective for IE and Edge Legacy browsers. (The -ms
prefix is specific to Microsoft.)
Here's the Solution:
- Implement a JavaScript test specifically for Windows 10 High Contrast mode. This function involves creating a div with an unusual color, checking if the color changes after it is added to the DOM. If the color changes, then the user is in high contrast mode! (This method also works 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;
}
- Set up a user agent test for Firefox:
const isFirefox = window.navigator.userAgent.indexOf('Firefox') > -1;
- Now you can proceed with applying your CSS!