Serving IE users other content
Say you want to serve Internet Explorer users something different than users using another browser, such as a "Get Firefox" banner, or just do not want IE users to view your site because you know IE will screw it up anyway. You could then use user agent detection, either clientside (e.g. JavaScript) or serverside (e.g. PHP). But then some Opera user or someone using the Firefox UA Switcher Extension comes to your site - and will get the IE content, will think "Huh! I don't use IE. Weird site." and he'll close your site.
Fortunately, there is an alternative to UA detection, called conditional comments. It are comments, but only when the condition does or doesn't match. For example, IE might parse the content in the comment if the condition matches. They are only supported by IE, because they are a non-standard thing (why would you need them for other browsers anyway). I personally use them to serve IE users an extra CSS sheet containing bugfixes, and I also once made a "Get Firefox" banner for IE users.
Let's look at some code:
<![if !IE]> You at least use a non-IE browser, and maybe even Firefox! If you place things here, IE users won't see them. <![endif]> <!--[if IE]> You should use <a href="http://www.getfirefox.com/">Firefox</a>! IE users will see this instead. <![endif]-->And the code in action:
You at least use a non-IE browser, and maybe even Firefox! If you place things here, IE users won't see them.
As you can see, the first conditional comment will work for browsers that are not IE, which works because it isn't actually a comment, but IE will see it as being one and ignore what is between the two tags. The second block is actually a comment, although it doesn not look like it. If IE sees the condition matches, it will parse the content in the comment. You can also use version checks in the conditions.
- praseodymium's blog
- Login or register to post comments



Most important use of <!--[if IE]>
By far one of the most annoying things that I as a webmaster have to deal with is Internet Explorer's utterly abhorent support for font sizes. it is literally the only browser on Earth that still limits the scalability of fonts and won't scale "fixed-sized" fonts.
To complicate matters further, FireFox treats dynamic font sizes differently...making them 20% smaller than Internet Explorer.
Thus, you can use the if IE to correct this difference...for instance:
<!--[if IE]>
<style type="text/css">
body { font-size: 0.7em; }
</style>
<![endif]-->
This will effectively bring Internet Explorer in line with FireFox's representation of the non-fixed fontsizes, while at the same time, allow the broken IE to resize all your fonts :-)
Enjoy,
-hope
30 or 20%?
Then you make IE fonts 30% smaller than in Firefox don't you?
LOL! Not really!
typo! it *should* be 30%...standards-wise, you basically force IE to treat font-size: medium as font-size: small.
Only works IE 5.5+
Unfortunately IE 5.0 -- the one that needs it the most -- doens't support this. The history behind this was that at 5.5 MS realized a) they are totally whacked out and b) they couldn't solve any of the problems (due to fundamental design flaws related to the "security" zones).
IE 7 has been completely rewritten...yet it *still* isn't compliant!! It has a WHOLE *SLEW* of *new* CSS quirks...mostly in CSS2 and its CSS3 support is *SO* abnormal it seems they tried to implement a non-existant CSS4 or something.
BTW, did you know that IE 7 was written in C#? ::shudder::
You're right
Yes, you're right. IE 5.x is supposed to support it but it screws up - although sometimes it might work anyway.
There's also a solution to that problem. Maybe there is some quirk in IE <5.5, and you could also use javascript for that - knowing that most users using the UA switcher won't switch their UA string to a very old IE version (considering that UA switchers are mostly used to work around buggy browser detection scripts).
C# isn't all that bad though!