[SOLVED] How do I write IE conditional comment

This is a discussion on "[SOLVED] How do I write IE conditional comment" within the Web Page Design section. This forum, and the thread "[SOLVED] How do I write IE conditional comment are both part of the Design Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Oct 10th, 2007, 19:49
Elite Veteran
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] How do I write IE conditional comment

I have a site I'm working on temporarily located here http://geocities.com/lchadwind/

It looks fine in IE7 and FF2 but the .menu classes are too wide for IE6.
So my third div floats down underneath the first two.

Since I've been playing around with the margins and padding and widths and not come up with anything that will work as a compromise across the browsers, I've decided to use a Conditional Comment.

Can someone tell me how it's done? I've looked at several tutorials and am completely confused. None of the tutorials worked. I'm sure I did them wrong but hey.. I tried...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 10th, 2007, 20:17
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

Code: Select all
<!--[if IE 6]>
<style type="text/css">
.menu {
width: 100px /*change this*/
}
</style>
<![endif]-->
Try that
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 10th, 2007, 20:19
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

If this is a problem in IE6 and below use this:

Code: Select all
<!--[if lte IE 6]>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 10th, 2007, 20:24
Elite Veteran
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I write IE conditional comment

Beautiful Alex .. thank you...
I had tried your example but did not have this
Quote:
<style type="text/css">
I just used <style>.menu code here </style>
Is that where I went wrong?


I did find another solution but I'm not sure it's as good as the one Alex gave.

Code: Select all
.menu {
    width: 180px;  /*style for all */
    width: 150px; /*style for IE*/
    float: left;
    margin: 10px 0px 4px 11px;
    text-align: center;
    }
But it also worked.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 10th, 2007, 20:26
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I write IE conditional comment

Don't worry; it's really easy once you get used to it, and it's extremely useful.

First, let's look at an ordinary HTML comment:
Code: Select all
  <!-- No-one will ever see this text, except in the source code. -->
Browsers will completely ignore everything inside those comment symbols.

IE, however, looks slightly deeper, because it's watching out for conditional comments (CC's):
Code: Select all
 <!--[if IE]>
       <p>You are using Internet Explorer. You poor, misguided child.</p>
 <![endif]-->
IE will treat this exactly as though the blue bits did not exist. In this example, only IE will display the paragraph; all other browsers ignore it completely.

CC's are great for serving an extra stylesheet to IE. For example:

Code: Select all
        <link href="style.css" rel="stylesheet" type="text/css">
<!--[if IE]>
        <link href="IE.css" rel="stylesheet" type="text/css">
<![endif]-->
In this example, all browsers (including IE) will get "style.css", but IE will also get "IE.css".

So you can use IE.css to override the existing styles. Let's say style.css has p { color: blue }, and IE.css has p {color: red }. Then IE will get red paragraphs, while all other browsers get blue paragraphs.

You can also use CC's to isolate different versions of IE:

Code: Select all
<!--[if IE 7]>
        <link href="ie7.css" rel="stylesheet" type="text/css">
<![endif]-->
<!--[if lt IE 7]>
        <link href="ie6.css" rel="stylesheet" type="text/css">
<![endif]-->
Here, only IE7 will get IE7.css, and all earlier versions (IE6, IE5,...) will get IE6.css instead. The "[if lt IE 7]" stands for "[if less than IE 7]. Similarly:
  • [if gt IE 6] = "if greater than IE 6"
  • [if gte IE 6] = "if greater than or equal to IE 6"
  • [if lte IE 7] = "if less than or equal to IE7"
This syntax must be copied exactly, or it will fail. Note the space between "IE" and "7".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 10th, 2007, 20:27
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

I don't think the other will validate, without the use of "!important;".
And yes I think the style tag with not attributes was where you went wrong
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 10th, 2007, 20:44
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,762
Thanks: 0
Thanked 14 Times in 14 Posts
Re: How do I write IE conditional comment

Quote:
Originally Posted by alexgeek View Post
I don't think the other will validate, without the use of "!important;".
And yes I think the style tag with not attributes was where you went wrong
What?? What needs the '!important'.. Please explain - i seem to have lost you!
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 10th, 2007, 20:49
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

Quote:
Originally Posted by Marc View Post
What?? What needs the '!important'.. Please explain - i seem to have lost you!
When you "override" a property, you should specify it as !important;
This means, that "this property is not important, if another script tells it to be something else, let it."

Practical example:

HTML: Select all
<style type="text/css">
.boat {
color: red !imporant;
}
</style>
<!--[if IE 7]>
<style type="text/css">
.boat {
color: blue;
}
</style>
<![endif]-->


This would mean that in IE7 the boat class would be blue.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 10th, 2007, 20:52
Marc's Avatar
Staff Manager

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Posts: 1,762
Thanks: 0
Thanked 14 Times in 14 Posts
Re: How do I write IE conditional comment

Would it :s That looks like to me they would both be red. Mabey i need to get off this computer! Im fighting shadows!
__________________
Marc
Staff Manager - Webforumz.com


Want to be a moderator? PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 10th, 2007, 21:03
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

It is using some programming techniques known as inheritance and overriding.
Because the boat's red property is not important, the IE7 conditional comment overrides the existing one.
Do you follow?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 10th, 2007, 21:49
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I write IE conditional comment

Quote:
Originally Posted by alexgeek View Post
When you "override" a property, you should specify it as !important;
Whoa there! That's like using a bazooka to kill a mosquito.

You need to understand the rules about CSS conflicts before you start fire-bombing your code with !important everywhere.
  1. The rule with the greatest specificity wins (so body p { ... } beats p {... }, and p#intro { ... } beats them both).
  2. When specificity is equal, the last rule takes precedence.
  3. These rules apply with multiple stylesheets: it would be the same as combining them into one giant stylesheet.
In my example, the latter rule takes precedence, so IE gets red paragraphs.
Quote:
Practical example:

HTML: Select all
<style type="text/css">
.boat {
color: red !imporant;
}
</style>
<!--[if IE 7]>
<style type="text/css">
.boat {
color: blue;
}
</style>
<![endif]-->
This would mean that in IE7 the boat class would be blue.
No, that won't work! !important overrides everything (except another !important). In your example, all browsers will get red.

!important is extremely useful for debugging CSS conflicts, but you must remove it after debugging -- or else your debugging process will be compromised next time.

I have never found a need for !important, except for in debugging. Indiscriminate use of !important is sloppy CSS, and it will come back to haunt you one day -- when you run out of overrides, and realise you need to learn about specificity.

Last edited by MikeHopley; Oct 10th, 2007 at 21:54.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Oct 10th, 2007, 21:51
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

i'll shutup then
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Oct 10th, 2007, 21:57
Elite Veteran
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I write IE conditional comment

No Alex.. you don't need to shut up!

You answered my question perfectly. 99 out of 100 times I use external style sheets but this ONE time, I have to put the css inline because this is a shopping cart homepage. No ability to externally attach a stylesheet.
Well maybe there is but I don't want to figure it out..

Mike.. you should be a teacher! (Maybe you are?)

Excellent explanation. I will stick this in my subscriptions.

Thanks a million!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Oct 10th, 2007, 22:06
Most Reputable Member
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I write IE conditional comment

Quote:
Originally Posted by alexgeek View Post
i'll shutup then
No, don't do that! You gave exactly the right answer for Linda's problem.

And if you had kept quiet about !important, you would still be unaware how it really works.

Quote:
Mike.. you should be a teacher! (Maybe you are?)
Thanks. I'm a part-time badminton coach. I definitely get a kick out of teaching; if the writing fails dismally, teaching could be my bail-out option.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Oct 10th, 2007, 22:19
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: How do I write IE conditional comment

Okay, I'll keep blabbering

And a "mentor" is a teacher anyway, so you are ha.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Oct 10th, 2007, 23:14
Elite Veteran
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How do I write IE conditional comment

I appears that everything above is not working.

I have added this to between the <head></head>
Code: Select all
 <!--[if IE 6]>
    <style type="text/css">
    .menu {
    width: 180px; 
    }
    </style>
    <![endif]-->
      <!--[if IE 7]>
    <style type="text/css">
    .menu {
    width: 219px; 
    
    }
    </style>
    <![endif]-->
This is the code further down for the same div.
Code: Select all
.menu {
    width: 205px; 
    float: left;
    margin: 10px 1px 4px 0px;
    text-align: center;
    }
Did I miss something? None of the browsers are responding to their own code.
I have it set for ff at the moment but the IE7 and IE6 are not following their if's.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #17