accessibilitymozillahigh-contrast

High contrast mode on Mozilla browser


I am not able to use any media query for high contrast mode in the Mozilla (Firefox) browser. The media queries given are working fine on IE and edge, but not working on Mozilla. The images are not coming on Mozilla in high contrast mode. Can someone suggest any media query which will target Mozilla in high contrast mode?

I have used following media queries:

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

}
@media screen and (-ms-high-contrast: black-on-white) {

}
@media screen and (-ms-high-contrast: white-on-black) {

}

Solution

  • For Mozilla I use JS to detected high contrast mode or media query

    function HCTest(idval) {
        var objDiv, objImage, strColor, strWidth, strReady;
        var strImageID = idval; // ID of image on the page
    
        // Create a test div
        objDiv = document.createElement('div');
    
        //Set its color style to something unusual
        objDiv.style.color = 'rgb(31, 41, 59)';
    
        // Attach to body so we can inspect it
        document.body.appendChild(objDiv);
    
        // Read computed color value
        strColor = document.defaultView ? document.defaultView.getComputedStyle(objDiv, null).color : objDiv.currentStyle.color;
        strColor = strColor.replace(/ /g, '');
    
        // Delete the test DIV
        document.body.removeChild(objDiv);
    
        // Check if we get the color back that we set. If not, we're in 
        // high contrast mode. 
        if (strColor !== 'rgb(31,41,59)') {
            return true;
        } else {
            return false;
        }
    }
    jQuery(function () {
        var HCM = HCTest();
        if (HCM === true) {
            jQuery('Body').addClass("moz-contrast");
        } else {
            jQuery('Body').removeClass('moz-contrast');
        }
    });
    

    CSS For mozila

    @-moz-document url-prefix() {

    .moz-contrast{
       /**styling**/
    }
    

    }