javascriptcssaccessibilityhigh-contrast

How to programmatically enable "-ms-high-contrast" media query?


I'd like to enable high contrast mode on my site and put all accessibility-related rules inside the high contrast media query:

@media screen and (-ms-high-contrast: active) {
  /* All high contrast styling rules */
}

However, how do I enable such mode in JavaScript programmatically? I'd like to have a button for my users to toggle this feature on their will. Is it possible in any way? Am I doing it right? Maybe a better solution exists.

Thank you for your assistance.


Solution

  • Since media queries are automatic (based on browser conditions) you need to emulate that block in your CSS but apply and remove it based on a user interaction.

    It seems to me that you should simply write the styles to emulate IE's high contrast mode and then toggle a class on the document (probably body) when you click a button.

    Put your new class and sub definitions at the bottom of your CSS so you know they'll override previous properties.

    For example:

    h2 {
      font-size: 14px;
      color: #dddddd;
    }
    /* overrides a normal h2 when highContrast class is added to the body */
    /* use a post processor (LESS/SCSS) to easily nest elements */
    body.highContrast h2 {
      font-size: 18px;
      color: #000;
      font-weight: bold;
    }