Display an Unsupported Browser Warning with CSS

Using CSS3 can be lots of fun. But, making your websites compatible with older browsers that don't support it can be a pain. You can easily spend as much time trying to make your site compatible with older browsers as you did designing it in the first place. No thanks. There are a few services out there—browser-update.org, for example— that allow you to display a nice subtle message to users on older browsers. That's one option, but I wanted something that really got the point across…

The project I'm working on relies on so much modern CSS that it would appear completely broken on older browsers, so I decided to block out the screen for people using older browsers. After browsing around a bit, I found a few posted JavaScript solutions to this problem, but I didn't want to have to use JavaScript. So, I figured out a way to do it just using CSS.

First, add a div on the page, just after the <body> tag, with your warning inside. For styling purposes, I nested another div inside of it:

echo '<div id="browser_warning">';
	echo '<div id="browser_warning_inner">This website includes CSS elements that your browser does not support. Please upgrade your browser to a current version, then come back and try again.</div>';
echo '</div>';

After you have your div on the page, you'll want to style it, then hide it. Add this to your stylesheet:

#browser_warning {
#browser_warning_inner {
@media only all {
	#browser_warning { display:none; }

Everything before @media there is setting up the div to fill the entire screen and have very prominent text centered on it. The part after the media query hides it all. This works because all browsers understand and can process the first parts, but only browsers that are CSS3 compliant will process the last part (the part that hides the warning).

There are other ways of going about this, I'm sure, but I like this one because the browsers that support CSS3 media queries happen to be the same browsers that can handle all of the CSS3 elements my project included.



This post was published on February 20th, 2015 by Robert James Reese in the following categories: CSS and HTML Before using any of the code or other content in this post, you must read and agree to our terms of use.