Detecting high contrast mode in Firefox using CSS or JavaScript

I am currently in the process of designing a high contrast version of a website. The code below is effective for Internet Explorer and Edge, but I am looking to specifically detect Firefox.

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

Does anyone have recommendations on how to detect both high contrast mode and specifically identify Firefox using JavaScript?

Answer №1

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:

  1. 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;
}
  1. Set up a user agent test for Firefox:
const isFirefox = window.navigator.userAgent.indexOf('Firefox') > -1;
  1. Now you can proceed with applying your CSS!

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

Guide to displaying options in the Vue material select box even when the default value is blank

I have implemented the material design framework vuematerial with vue.js. In traditional HTML, a selection box looks like this: <select> <option value="">You should initially see this</option> <option value="1">One</o ...

Firefox doesn't support CSS bubble functionality

Here is the CSS code I am using: .bubbledLeft, .bubbledRight{ position: relative; margin-top: 3px; max-width: 99%; clear: both; min-width: 99%; } .bubbledLeft{ float: left; margin-right: auto; padding: 14px 10px 4px 15 ...

Issue with page break functionality during print preview

div.pagebreak { page-break-after: always; page-break-inside: avoid; } HTML <!-- Page separator --> <div class="pagebreak" style="float: none;"><hr class="hidden-print" /></div> <app-mud-chec ...

Incorporating CSS styling to specific elements

I am facing an issue with a CSS rule that looks like this: div#tabstrip.tabstrip-vertical.k-widget.k-header.k-tabstrip div#tabstrip-4.k-content.k-state-active{ background-image: url("http://wwww.example.com/logo2.png") !important; background-posit ...

What is the method to ensure an element is displayed in Selenium WebDriver?

Looking for assistance on how to choose options from a dropdown when the element is not visible and has a boolean attribute? Here's the HTML code snippet: <select id="visualizationId" style="width: 120px; display: none;" name="visualization"> & ...

Using Bootstrap to embed YouTube videos results in a sleek, modern design with distinctive black

When my screen is maximized on Chrome, I notice irritating black lines appearing on my responsive Youtube embeds. Unfortunately, I am unable to post an image due to lack of reputation. Specifically, when the video is paused, there is a slim black line at ...

How can I adjust height without affecting weight in CSS using curly brackets?

Can CSS be used to specifically adjust the height of a curly bracket without changing its thickness? I have been attempting to modify the height of a curly bracket dynamically by adjusting the font-size, but this also alters the weight. Is there a workar ...

<td> issue with IE not rendering overflow:hidden properly

In an effort to display text in a TD, I've implemented the overflow property set to hidden for the td element. Surprisingly, this code works flawlessly in Chrome, Safari, and Mozilla browsers, but unfortunately falls short in IE. Despite attempting to ...

What is the best way to combine two sections in html/css/bootstrap?

I've been trying to create a simple webpage with a navigation bar and a section below it, but I keep running into an issue where there's unwanted white space between the nav bar and the next section in blue. Is there a way to eliminate this gap a ...

Loading an external script is causing a total style overhaul on my website

Every time I load my page to check the remaining balance of a gift card (using a third-party script), it seems to mess up the styles that are already set on my website. Here is a snippet of the code causing the issue: <div id="main" class=&quo ...

insert a div element at a static position with a height of 100%

Adding two divs on top and bottom of a fixed div is what I need to do. To achieve this, I created a panel with a fixed position on the left side. I added the first div with the h-100 class which sets its height to 100%. However, when I try to add the secon ...

Alter the background shade of an item as it is added or removed from a multi-select list and transferred to another list

Is there a way to visually represent the movement of items between two multi-select lists? I have one list on the right and one on the left, and when objects move from right to left, I want them to have a red background (indicating removal) and if they mov ...

Content does not display within a list element (ul)

I am attempting to display captions beneath the thumbnail slider in list format. However, the text is not appearing unless I switch the <ul> tag to <div>. You can locate the thumbnail here. It is the first thumbnail. Much appreciated. ...

The HTML button triggers a function to execute on a different webpage when clicked

I'm facing a straightforward issue that I can't seem to figure out due to my limited experience with Angular and web development. The problem revolves around two components, namely home and dashboard. In the home.component.html file, there's ...

A linear gradient effect originating from the center/middle of the background

Is it possible to create a linear-gradient background that starts from the center or middle of the screen? This is the CSS I am currently using: body { display: table; width: 100%; height: 100%; margin: 0 auto; background-repeat: no-r ...

Experiencing distinct webpage designs on 127.0.0.1 versus localhost

As I work on creating a responsive website using my laptop, I have encountered an unexpected issue. Using live reload at 0.0.0.0 port 3000 allows me to preview my project on my mobile device. However, the layout appears differently when viewed at 127.0.0.1 ...

Highcharts' labels on the x-axis do not automatically rotate

One issue I am facing is that my custom x-axis labels on the chart do not rotate upon window resize. I would like to have them rotated when the screen size is less than 399px. Check out the code here $(function () { $('#container').highchart ...

The content in the html document is not flowing outside a div unless I eliminate the floating property from the div(s)

My goal is to create a page layout with three sections, each floated left based on the device width using media queries in my CSS. I have successfully achieved this, but when I inspect the page using Chrome Developer Tools, I notice that the body is not sp ...

Customizing the Mui Theme in a Unique Way

After generating an MUI component from Mui DatePicker, I encountered the following code: <span class="my-theme-MuiTypography-root my-theme-MuiTypography-caption my-theme-MuiDayCalendar-weekdayLabel css-1mln09i-MuiTypography-root-MuiDayCalendar-week ...

Can someone provide assistance on how to add an icon to a button?

Help needed with adding icons to buttons! I am new to coding and struggling with adding icons to each button. If any classes need to be changed, please let me know. I am currently learning HTML and CSS and could use some guidance. Thank you! Example: ...