views:

390

answers:

4

IE handles one of my div's classes totally wrong. It works in every other browser I tried. I want to simply not apply the class to the div in IE. Is there a trick to make IE not apply it?

More info: The div has multiple classes. I want to still load one but not the other in IE. The classes are defined in a Javascript file.

Thanks. Mike

+6  A: 

You can use conditional comments for IE to override the style on the class - that might be the easiest thing. Have a look here: MSDN link Essentially, you'd assign a more specific style to the class in the comment, overriding the standard - so, you could take most of the styling off. Intelligent browsers won't see the comment.

Fake51
http://www.quirksmode.org/css/condcom.html has some good examples of conditional comments.
adrianbanks
Thanks for the suggestion. Another problem I have is that the class is not defined in a stylesheet but in a Javascript file. It took a lot of code to do what it does. I need to just tell IE NOT to apply the class, rather than modify it. Can that be done? Thanks again.
Michael Swarts
If you are emitting the style in JavaScript, you can detect what the current browser is using the user-agent string and only emit the style if it is not IE. Have a look at http://www.w3schools.com/js/js_browser.asp for a simple tutorial.
adrianbanks
I'm not very good at Javascript, but I gave your suggestion a try:<script type="text/javascript">function detectBrowser(){var browser=navigator.appName;var b_version=navigator.appVersion;var version=parseFloat(b_version);if ((browser=="Microsoft Internet Explorer") {}else { <script type="text/javascript" src="Scripts/scriptIEmessesUp.js"></script> }}</script>But it didn't work. I probably made a mistake. Could you tell me how to make this work?Thanks.
Michael Swarts
Conditional compilation (see http://www.javascriptkit.com/javatutors/conditionalcompile.shtml) is a cleaner way to address javascript that you only want to run in IE.
Alohci
Instead of trying to detect the browser using javascript, stick the javascript in a conditional comment, like outlined in in answer above. Only IE browsers will run that script, and you can choose exactly which of the IE browsers run it, as well.
Fake51
+5  A: 

You can reset that specific style in a IE only stylesheet using something like this in the head section:

<!--[if lte IE 7]>
<link rel="stylesheet" type="text/css" media="screen"  href="/styles/ie.css" />
<![endif]-->

(for IE 7 and below...)

Update to reflect some of the comments: You can target all non-IE browsers as a whole using this in your document (in the head if you go for styles):

<!--[if !IE]><!-->
/* some styles AND / OR javascript */
<!--<![endif]-->
jeroen
See my comment above. This would work if I could change it to recognize if it is NOT IE. That way I could use the condition to prevent IE from loading the javascript file and thus not load the class. Possible?
Michael Swarts
@Michael Swarts: `<!--[if !IE]>` should do the trick. You should even be able to use an else statement.
Noldorin
Only IE reads the comment tags, so when I tried that to prevent IE from loading the JS file with the class instructions, all browsers stopped applying the class.
Michael Swarts
@Michael Swarts: Do you use any javascript library's like jquery? If so, you could try if that library has a way to detect the browser type and not apply the style in case of IE. If you don't use a library, you will have to write something yourself.By the way, if it is always the same property that is in the javascript-generated style, you can still reset it using the above method.
jeroen
I also tried this:<div class="<!--[if!IE]>classIEcantunderstand<![endif]-->">...</div>Again, all browsers stopped applying the class.
Michael Swarts
You can only target the different versions of IE this way, that's why I was talking about resetting styles for IE. You cannot target the other browsers.
jeroen
Make sure you put the IE only CSS after the main CSS file otherwise it won't be overwritten.
Matthew James Taylor
@Matthew James Taylor: good point.
jeroen
You can target other browsers than IE using the conditional comments - that's why there's the [if !IE] comment that Noldorin points out. Check this article by Stu Nicholls to see how: http://www.cssplay.co.uk/menu/conditional.html
Fake51
@Fake51: Yes and no, the conditional comments are only seen by IE, the trick used by cssplay is simply closing the comment after the [if] statement. That way you target all other browsers (all browsers that don´t support the conditional comments) so yes you can target all other browsers as a whole but not as individual browsers.
jeroen
Which is by the way very appropriate for the original question :-)
jeroen
A: 

I hate to do browser detection via javascript, but if none of the other solutions work you might try something like the following:

function removeClassForIE() {

    // Look at userAgent to test for IE
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {

        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {

             if (divs[i].className == "myClassName") {
                 divs[i].className = "";
            }
        }
    }
}

That should remove the class from those divs, and only if its IE. You could call the function like <body onload="removeClassForIE();">

Matt Bridges
First of all, the div has 2 classes. I gave this a try like so:<script type=text/javascript>function removeClassForIE() { // Look at userAgent to test for IE if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { var div = document.getElementsByTagName("div"); for (var i = 0; i < div.length; i++) { if (div[i].className == "okClass notOKclass") { div[i].className = "okClass"; } } }}</script>And then I called it as you said: <body onload="removeClassForIE();">However, IE still loaded the class. Any ideas?
Michael Swarts
I would try sticking an alert(divs[i].className); statement into the for loop, just to see what IE thinks the className variable holds and make sure you are making the correct comparison.
Matt Bridges
A: 

You can use star(*) Hack for ie. This will apply only for ie browsers. For Example:

*html .test{margin:0px;}. Similarly you can specify the hack separately for ie6 and ie7.

for more details of CSS hacks, their is a good tutorial, please check it.

Webdevelopertut

Webdevelopertut