1

hi a i am triyng to using conditional css like this in my style sheet

 div.box {  
     width: 400px;  
     [if IE 6] width: 600px;  
     padding: 0 100px;  
 }

i want to set a different width to my div in ie6 , so i am trying this code and not working , how can i set a different width in ie6

also i tried this in my style sheet

[if IE]
div.box {
         width: 600px;  
         padding: 0 100px; 
}
[endif]
0

5 Answers 5

2

Conditional comments are for use inside HTML documents, not inside CSS files. IE's HTML parser will interpret the rule inside the comment - as far as I know, there's no equivalent logic in its CSS parser.

The following code will work:

<!--[if IE 6]>
<style type="text/css">
div.box {
         width: 600px;  
         padding: 0 100px; 
}
</style>
<![endif]-->

In most cases, it's better to use the conditional comment to include an external stylesheet, rather than just wrapping a block of inline styles, like so:

<!--[if IE 6]>
<link rel="stylesheet" href="/css/IEpatches.css" type="text/css" media="screen" charset="utf-8"/>
<![endif]-->

Edited to specify IE6 only as requested in the question.

Sign up to request clarification or add additional context in comments.

Comments

2

The best way to do conditionals for ie6 is to add a conditional include in the head of the html page like this:

<!--[if IE 6]>
  <link rel="stylesheet" href="[something like style/ie6.css goes here]" type="text/css">
<![endif]-->

and then just add your new widths as exceptions to elements in the ie6.css style sheet.

Comments

1

Have you tried?

<!--[if IE]>
// Your conditional CSS
<![endif]-->

I think that your formatting is wrong for your conditional statement.

Conditional Comments

2 Comments

Conditional comments don't work inside CSS documents. They work by virtue of being interpreted as regular HTML comments by all browsers except IE, which parses them to decide whether to ignore them or not.
actually in my code i use like this . is this correct .termschecklabel { position:absolute; margin-left: 30px; bottom: 70px; } <!--[if IE]> .termschecklabel { position:absolute; margin-left: 30px; bottom: 120px; } <![endif]-->
0

Have you read the Conditional-CSS documentation? Specifically, do you run your CSS through the Conditional-CSS compiler? Because if you don't, you'll just send the CSS to the browser unchanged - there is no way Conditional-CSS can do its magic then.

1 Comment

To prevent any more downvotes under the wrong pretense, this is a simple misunderstanding: conditional-css.com
0

As you probably have learned by now, conditional comments are parsed in the HTML output, not within the CSS file itself, unless you are using third party software.

Another way you can target IE within your CSS files without hacks:

<!--[if lt IE 7]> <html class="ie6"> <![endif]-->
<!--[if IE 7]>    <html class="ie7"> <![endif]-->
<!--[if IE 8]>    <html class="ie8"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->

It's become common to use variations of this technique for IE, I believe it was made popular by HTML5 Boilerplate. You are basically using conditional comments to add a class to the <html> tag (or body tag if you wish) so you can target elements in your CSS files like so:

.ie6 #header {
    /* some ie6 only styles here */
}

To me, this is more maintainable than using separate stylesheets, but suffers the very mild setback of other browsers reading (but not applying) the IE styles.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.