Skip to Main Content
Perplexed Owl Random Ponderings

Benjamin ‘Benilda’ Key:

October 02, 2019; March 21, 2023

How to Selectively Hide or Display Elements Based on the Web Browser Using CSS


I recently encountered a situation in which a feature I wanted to add to my website was not supported in Internet Explorer because it relied on third-party JavaScript code that is not supported in Internet Explorer. The feature was trigged by pressing a button. The page was displayed properly but when the button was pressed nothing happened. The console revealed the reason why nothing happened when the button was pressed. The third-party JavaScript code uses a language feature not supported by Internet Explorer.

I decided that the simplest solution was to hide the button for Internet Explorer and display it for all other web browsers.

A quick Google search suggested that this was easy. The article How To Create an IE-Only Stylesheet suggested that all I need to do is use conditional comments as follows.


<!--[if !IE]><!-->
<p>This paragraph should be hidden in Internet Explorer and visible in all other web browsers.</p.
<!--<![endif]-->

While this does work for older versions of Internet Explorer, it does not work for Internet Explorer 10 and Internet Explorer 11 as discussed in Conditional comments are no longer supported.

After giving this some thought, I decided that it might be possible to do this using CSS, as follows.


if (IsInternerExplorer()) {
.hidden-for-ie-visible-for-others {
    display:none !important;
}
else {
.hidden-for-ie-visible-for-others {
    display:block;
}
}

The problem is that CSS does not support if/else statements.

The Quest for a Solution

I found several good resources such as How to Target Internet Explorer 10 and 11 in CSS that showed how to do the if part. It can be done as follows.


@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.hidden-for-ie-visible-for-others {
    display:none !important;
}
}

Unfortunately, the How to Target Internet Explorer 10 and 11 in CSS does not provide any information on how to do the else part. I was at a loss for how to do the else part until I thought to do a Google search for css not Internet Explorer. This led me to the StackOverflow question Apply CSS to all browsers except IE using media query. The accepted answer suggests that the @supports rule can be used to detect all browsers except Internet Explorer as follows.


@supports not (-ms-high-contrast: none) {
   /* Non-IE styles here */
}

Unfortunately, -ms-high-contrast is not supported for older versions of Internet Explorer; it was added in Internet Explorer 10. Therefore, if we were to make use of both the @media technique and the @supports technique the hidden-for-ie-visible-for-others class would be defined for Interner Explorer 10 and Internet Explorer 11 and for most other browsers but it would not be defined for older versions of Internet Explorer.

The Solution

Fortunately, the CSS Specificity feature provides a solution. CSS properties are calculated using Precedence Rules. A simple description of these Precedence Rules is as follows.

  1. Inline css (html style attribute) overrides css rules in style tag and css file.
  2. A more specific selector takes precedence over a less specific one.
  3. Rules that appear later in the code override earlier rules if both have the same specificity.
  4. A css rule with !important always takes precedence.

Source: What is the order of precedence for CSS?

Therefore, the hidden-for-ie-visible-for-others class can be defined as follows.


/* Define the hidden-for-ie-visible-for-others class for old
versions of Interner Explorer. */
.hidden-for-ie-visible-for-others {
    display:none;
}
/* Define the hidden-for-ie-visible-for-others class for
Interner Explorer 10 and Interner Explorer 11. */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.hidden-for-ie-visible-for-others {
    display:none !important;
}
}
/* Define the hidden-for-ie-visible-for-others class for
other modern browsers. */
@supports not (-ms-high-contrast: none) {
.hidden-for-ie-visible-for-others {
    display:block !important;
}
}

Then you can use the hidden-for-ie-visible-for-others class as follows.


<p class="hidden-for-ie-visible-for-others">This paragraph should be hidden in Internet Explorer and visible in all other web browsers.</p.
Back to top