Search code examples
cssxhtmlconditional-comments

Can we use like this <body class="all" <!--[if IE 7]>class="ie"<![endif]--> >


Can we use like this <body class="all" <!--[if IE 7]>class="ie"<![endif]--> >?

I want to keep all CSS in one file.


Solution

  • You could use:

    <body class="all">
      <!--[if ie]>
        <div class="ieOnly">
      <![endif]-->
    
    <div id="content">
    
    <p>...</p>
    
    </div>
    
      <!--[if ie]>
        </div>
      <![endif]-->
    </body>
    

    That way the css selector to address IE's flaws/differences is more specific than the normal

    #content {/* for non-IE browsers */}
    
    body.all .ieOnly #content {/* for IE */}
    

    ...and should override the 'normal' #content rules and will never be applied by non-IE browsers, since there's an element .ieOnly in the selector which is otherwise 'invisible' to them.

    Still, strictly speaking, no; you can't do what you propose in your question, though there are alternative approaches.