Revised answer after question edit
Here is a CSS only way to achieve that.
As the CSS @supports
won’t work on your targeted (unwanted) browsers: Safari 7-8, IE <= 10, Android Browser < 4.4, UC Browser for Android and Opera Mini < 8, your “browserupgrade” message will be visible on those using this rule.
@supports (display: flex) { .browserupgrade { display: none; }}
There is a few browsers that still does support the non-prefixed flex
but doesn’t support @supports
, IE 11(1) and Opera Mini 8, but luckily we can address them with a couple of CSS specific rules.
/* IE 11 */
_:-ms-fullscreen, :root .browserupgrade { display: none; }
/* Opera Mini 8 */
:-o-prefocus, .browserupgrade { display: none; }
Here is the complete code to show an upgrade message for your targeted browsers.
CSS
.browserupgrade { display: block; }
/* IE 11 */
_:-ms-fullscreen, :root .browserupgrade { display: none; }
/* Opera Mini 8 */
:-o-prefocus, .browserupgrade { display: none; }
/* all modern browsers */
@supports (display: flex) { .browserupgrade { display: none; }}
HTML
<div class="browserupgrade">
<p>You are using an outdated browser. Please <a href="http://browsehappy.com/">
upgrade your browser</a> to improve your experience.</p>
</div>
(1): The IE 11 CSS rule should work on IE Mobile 11 too, though haven’t one to test it on.
The CSS @supports
is also available as an API, CSS.supports(). Here is a very well written article by David Walsh.
Additionally, if one would like to automatically redirect those browser, here is a small script that does that, after a delay of 10 sec.
var el = document.querySelector('.browserupgrade');
if (window.getComputedStyle(el,null).getPropertyValue("display") != 'none') {
setTimeout(function(){
window.location = 'http://browsehappy.com/';
}, 10000);
}