Conditional Statements for IE (Internet Explorer)

AID: 2233
  • Status: Published

5200 points

  • ByFDMilwaukee
  • TypeTips/Tricks
  • Posted on2010-01-07 at 14:00:17
Awards
  • Community Pick
I often run into questions on Experts Exchange asking "Why does my site look different in FireFox than it does in IE"  and "My site looks great in Firefox/Safari, but looks messed up in IE".  I often suggest a quick fix for people, and that is to use conditional statements for Internet Explorer.

I am not going to focus so much on why the problem happens (that's another several articles in and of itself), but how to implement IE conditional statements, and when to use them.  The very basic explanation of why web pages display differently is because each browser interprets HTML, CSS, and other programming languages in its own way.  W3C (World Wide Web consortium [learn more at w3c.com]) sets "standards" for web development, however it is up to the developer of each browser how closely they want to comply with those "standards".  Firefox and Safari comply fairly closely, where as Internet Explorer does it more their own way (it should be noted that IE standards compliance has gotten better with each new version - 8 being the most recent).

Some styles that commonly need attention are position, paddings and margins, float elements, and in the dreaded IE6, transparent images.

So now that we have a basic idea of why my site looks "weird" in some browsers.  How do we fix it?  Here's how I design a site.  I layout my site, test/preview in Firefox, than add a conditional statement, and "fix" it for IE.  And here is how you apply the "fix" for IE:

to the head of your document add the following
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="ie_style.css" />
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window


The above statement say "if the browser is Internet Explorer than apply style sheet ie_style.css"

You can now use your new style sheet to adjust your CSS so your site looks just as good in IE as it does in Firefox.  

So now what do you do if your site looks fine in IE8, but looks "weird" in IE6, and IE7?  Now you need to add an operator to your conditional statement as follows:
<!--[if lt IE8]>
<link rel="stylesheet" type="text/css" href="ie-8_style.css" />
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window


The above statement will apply style sheet ie-8_style.css to all versions of IE below 8.

...And the best part about the IE conditional statement?  You can use it for just about anything

  • To apply styles as we discussed above

  • To apply JavaScript, or other script to a specific browser version

  • To prompt the user to upgrade or switch browsers (PLEASE EVERYONE UPGRADE FROM IE6 :))

  • Or to display another page or site all together


The following is a list of operators, their application, and syntax


As shown above the conditional statement requires
<!-- [if xxx]> HTML <![endif]--> 
                                    
1:

Select allOpen in new window


This is standard HTML comment with the IE conditional comment.  So lets look at the operators:
+-----+--------------------------------------+
|!    |NOT operator                          |
|lt   |Less than operator                    |
|lte  |Less than or equal to operator        |
|gt   |Greater than operator                 |
|gte  |Greater than or equal to operator     |
+-----+--------------------------------------+
                                    
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

+------------------+----------------------------------------------------+
|[if !IE]          |if not Internet Explorer                            |
|[if lt IE 7]      |if less than Internet Explorer 7                    |
|[if lte IE 7]     |if less than or equal to Internet Explorer 7        |
|[if gt IE 7]      |if greater than Internet Explorer 7                 |
|[if gte IE 7]     |if greater than or equal to Internet Explorer 7     |
+------------------+----------------------------------------------------+
                                    
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window




Examples

The following expression says if browser is Internet Explorer version 7 or below, execute script ExpertExchange.js:
<!--[if lte IE7]> 
<script language="javascript" src="ExpertsExchange.js"><script>
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window



This expression is similar to examples I gave above, but instead of linking to an external style sheet all the styles are within the conditional comment.  This expression says if browser is Internet Explorer version 8 make the body text red, and size:120%.
<!--[if IE8]>
<style>/*this is regular old CSS within the style tag*/ body {font-size:120%; font-color:red}</style>
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window



This expression congratulates the user on using a version of Internet Explorer higher than version 6, in huge red letters:
<!--[if gt IE6]>
<p style="font-size:400%; font-color:red">Congratulations, you are using Internet Explorer version 7 or higher!</p>
<![endif]-->
                                    
1:
2:
3:

Select allOpen in new window




Please visit Microsoft's site  http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
to learn much more about IE conditional statements, and to see a complete list of operators, and how they can be used.

I hope you enjoyed my first EE article, and hope it saved you some anguish in your site development endeavours.  

If you found this article useful please click the yes link at the bottom right of this article to let me know!

FDM
Asked On
2010-01-07 at 14:00:17ID2233
Tags

web browser

,

Internet Explorer

,

Firefox

,

web authoring

Topic

Hypertext Markup Language (HTML)

Views
2708

Comments

Expert Comment

by: nikkyron on 2010-02-21 at 23:35:31ID: 9934

I am using the "lt IE8" version of this conditional comment system to try and fix a float issue in ie7. When I  put the conditional comment structure in the head then pull it up in ie I actually see the comment tags themselves.

Any idea why?

Author Comment

by: FDMilwaukee on 2010-02-22 at 17:32:53ID: 9969

crap.....sorry nikkyron I responded to this this morning, however I had my comment set to private.

These were my two previous posts.

is it an .aspx based site?

One other question...is this happening across all browsers, only in one browser, only in IE8?

have you tried (see code view below)-----will accomplish the same thing.

<!--[if lte IE7]>
                                        
1:

Select allOpen in new window

Expert Comment

by: manal87 on 2011-05-09 at 01:19:41ID: 26386

Hi there and thanx for your post it is helpful,

my problem is that I don't know how to use conditional statements for something very specific.

I have designed my perfonal portfolio website www.manalelias.com and I'm encountering one specific problem both in Internet Explorer and Firefox. I am an amateur and would really appreciate an expert's help.

My navigation bar and the div below it (solid color) are absolute but I have applied the following javascript to keep them fixed (meaning they remain apparent/fixed when scrolling sideways):

<script type="text/javascript">
<!--
window.pos = function() {
  if (window.scrollX != null && window.scrollY != null) return { x: window.scrollX, y : window.scrollY };
  else return { x: document.body.parentNode.scrollLeft, y : document.body.parentNode.scrollTop };
};

window.onscroll = function(e) {
  document.getElementById('navigation').style.left = window.pos().x + 'px';
  document.getElementById('under_nav').style.left = window.pos().x + 'px';
};
-->
</script>


The probelm in IE and Firefox is major jerking of the navigation bar when scrolling sideways. I do not have this problem is Safari. And I really don't want to change the design. IF there is no solution to stop the jerking, how can I cancel the javascript only on IE and firefox?

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top HTML Experts

  1. COBOLdinosaur

    296,938

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  2. DaveBaldwin

    165,369

    Guru

    2,100 points yesterday

    Profile
    Rank: Genius
  3. Ray_Paseur

    121,795

    Master

    0 points yesterday

    Profile
    Rank: Savant
  4. nap0leon

    107,610

    Master

    0 points yesterday

    Profile
    Rank: Sage
  5. leakim971

    99,938

    Master

    0 points yesterday

    Profile
    Rank: Genius
  6. tommyBoy

    88,716

    Master

    0 points yesterday

    Profile
    Rank: Genius
  7. LZ1

    81,545

    Master

    0 points yesterday

    Profile
    Rank: Genius
  8. mplungjan

    81,271

    Master

    855 points yesterday

    Profile
    Rank: Savant
  9. ChrisStanyon

    71,364

    Master

    0 points yesterday

    Profile
    Rank: Sage
  10. xmediaman

    43,950

    1,500 points yesterday

    Profile
    Rank: Guru
  11. jason1178

    42,650

    0 points yesterday

    Profile
    Rank: Genius
  12. Proculopsis

    39,955

    0 points yesterday

    Profile
    Rank: Sage
  13. StingRaY

    36,218

    0 points yesterday

    Profile
    Rank: Wizard
  14. webmatrixpune

    34,810

    0 points yesterday

    Profile
    Rank: Guru
  15. kozaiwaniec

    31,116

    0 points yesterday

    Profile
    Rank: Guru
  16. sammySeltzer

    27,868

    0 points yesterday

    Profile
    Rank: Genius
  17. HainKurt

    27,264

    0 points yesterday

    Profile
    Rank: Genius
  18. SSupreme

    26,354

    0 points yesterday

    Profile
    Rank: Wizard
  19. chaituu

    23,450

    0 points yesterday

    Profile
    Rank: Sage
  20. s8web

    22,532

    0 points yesterday

    Profile
    Rank: Sage
  21. gurvinder372

    22,077

    0 points yesterday

    Profile
    Rank: Genius
  22. therealteune

    20,912

    0 points yesterday

    Profile
    Rank: Wizard
  23. ahoffmann

    20,240

    0 points yesterday

    Profile
    Rank: Genius
  24. HagayMandel

    20,176

    0 points yesterday

    Profile
    Rank: Guru
  25. anuradhay

    19,974

    0 points yesterday

    Profile
    Rank: Guru

Hall Of Fame