What are CSS Conditional Comments and Conditional Rules?

Using CSS Conditional Comments to detect browser versions and avoid those nasty CSS hacks for Internet Explorer or Firefox.

By Tim TrottHTML & CSS Tutorials • May 19, 2009
What are CSS Conditional Comments and Conditional Rules?

CSS Conditional Comments allow certain code elements to be processed by a given internet browser without JavaScript, hacks, or other methods. This tutorial will show how to serve cross-browser CSS content with 100% valid CSS and NO hacks.

Conditional comments are nothing new; they have been around since Internet Explorer 5, but I haven't seen them used much, and to be honest, I haven't used them much until now.

Standard HTML comments look like this:

xml
<!-- Comment content  -->

A conditional comment uses an expression that evaluates it as true or false. If the expression is true, the HTML within is rendered; if the expression is false, then the HTML is ignored.

xml
<!--[if expression]> HTML <![endif]-->

So, how do we use this to detect browsers? Well, we can use the IF IE feature to detect Internet Explorer-based browsers like this:

xml
<!--[if IE]> Hello Internet Explorer User! <![endif]-->

If you use this code, any visitor to the page using Internet Explorer will see this hello message, but anybody using Firefox, Opera, or Chrome will not.

Let's suppose you wish to target a specific version of IE; insert the version after IE:

xml
<!--[if IE 6]> Hello Internet Explorer 6 User! <![endif]-->

And if you want to target any version above or below, you can use a mathematical operator for comparisons:

Operator Description
lt Less Than
gt Greater Than
lte Less Than or Equal to
gte Greater Than or Equal to

Example usage:

xml
<!--[if lte IE 6]> Target IE6 and below <![endif]-->
xml
<!--[if lt IE 7]> Target anything below IE7 but not including <![endif]-->
xml
<!--[if gte IE 6]> Target IE6 and above <![endif]-->

How do we use this to serve CSS content? The message between the comment tags can be anything you want, including Javascript and HTML tags. We can use this to serve Internet Explorer or Firefox user's specific content.

xml
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head>
  <link rel="stylesheet" href="global-styles.css" type="text/css" />
  <!--[if lte IE 6]><link rel="stylesheet" href="ie6-and-below.css" type="text/css" media="all" /><![endif]-->
  <!--[if IE 7]><link rel="stylesheet" href="ie7.css" type="text/css" media="all" /><![endif]-->

In this example, I have created a global CSS document which contains all the styles needed for the website to work. Internet Explorer 6 had some problems with margins and padding, so I created a CSS document which contains margin and padding reset for IE6. I had a problem with a border in IE7, so I only created another CSS file for IE7.

This method means that anybody using Firefox will only use one CSS document, while Internet Explorer users will download the main CSS and a "fix" document specific to IE. Using conditional comments also means that every CSS document is valid and that no hacks must be used to get the correct page rendering to work.

You can also use conditional comments to target non-IE browsers using !IE will specify all browsers matching "not IE". Very handy for including CSS3 and other features supported by all browsers apart from Internet Explorer.

xml
<!--[if !IE]><link rel="stylesheet" href="css3goodies.css" type="text/css" media="all" /><![endif]-->

Remember: This tip can be used for ANY HTML content, not just CSS. You can send Javascript or any HTML code to specific browsers.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.