[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.



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

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 10th, 2007, 19:49
SuperMember

SuperMember
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...
Reply With Quote

  #2 (permalink)  
Old Oct 10th, 2007, 20:17
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #3 (permalink)  
Old Oct 10th, 2007, 20:19
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #4 (permalink)  
Old Oct 10th, 2007, 20:24
SuperMember

SuperMember
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.
Reply With Quote
  #5 (permalink)  
Old Oct 10th, 2007, 20:26
SuperMember

SuperMember
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".
Reply With Quote
  #6 (permalink)  
Old Oct 10th, 2007, 20:27
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #7 (permalink)  
Old Oct 10th, 2007, 20:44
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,637
Thanks: 0
Thanked 6 Times in 6 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
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!
Reply With Quote
  #8 (permalink)  
Old Oct 10th, 2007, 20:49
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #9 (permalink)  
Old Oct 10th, 2007, 20:52
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,637
Thanks: 0
Thanked 6 Times in 6 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
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!
Reply With Quote
  #10 (permalink)  
Old Oct 10th, 2007, 21:03
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #11 (permalink)  
Old Oct 10th, 2007, 21:49
SuperMember

SuperMember
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.
Reply With Quote
  #12 (permalink)  
Old Oct 10th, 2007, 21:51
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: How do I write IE conditional comment

i'll shutup then
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #13 (permalink)  
Old Oct 10th, 2007, 21:57
SuperMember

SuperMember
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!
Reply With Quote
  #14 (permalink)  
Old Oct 10th, 2007, 22:06
SuperMember

SuperMember
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.
Reply With Quote
  #15 (permalink)  
Old Oct 10th, 2007, 22:19
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
  #16 (permalink)  
Old Oct 10th, 2007, 23:14
SuperMember

SuperMember
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.
Reply With Quote
  #17 (permalink)  
Old Oct 10th, 2007, 23:18
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: How do I write IE conditional comment

Is it possible to put the the code before the conditional comments? that should work.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #18 (permalink)  
Old Oct 11th, 2007, 00:46
SuperMember

SuperMember
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

Ok that worked!
I did misunderstand the directions..
Now the conditional comments sit in the html code and it works fine!

Thanks again Alex!
Reply With Quote
  #19 (permalink)  
Old Oct 11th, 2007, 07:17
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,771
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: [SOLVED] How do I write IE conditional comment

You're welcome
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #20 (permalink)  
Old Oct 16th, 2007, 07:06
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,603
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
Send a message via Yahoo to Monie
Re: [SOLVED] How do I write IE conditional comment

Can we do this for Firefox, Opera, Safari or any other browser?
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] do you write quicker Hosting & Domains 8 Dec 12th, 2007 05:13
[SOLVED] conditional statement firefox saltedm8 Web Page Design 9 Nov 25th, 2007 18:13
[SOLVED] popup and document.write problem in IE deluxmilkman JavaScript Forum 3 Oct 3rd, 2007 11:26
Conditional IF in external stylesheet AdRock Web Page Design 6 Sep 23rd, 2007 22:45
Functions and conditional IFs AdRock PHP Forum 4 Sep 17th, 2007 08:18


All times are GMT. The time now is 01:27.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved