收录日期:2019/02/17 00:17:54 时间:2010-06-16 02:00:57 标签:css,xhtml,conditional-comments

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

I want to keep all CSS in one file.

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.

Short answer: No (at least, not in-line), but why do you need to? :)

Just defined a body { } style in an IE conditional stylesheet, like this:

<!--[if IE 7]>
  <link rel="stylesheet" type="text/css" href="ie7.css">
<![endif]-->

And inside there:

body { /* or body.all { */
  background: pink; /* Pink is pretty!, or green, whatever */
}

No, you cant specifically comment out an attribute even with IE's conditional comments. But there could be other ways of expressing it.

If you want to add a class to body based on the browser without hacks, you're gonna need to use server-side code and UA sniffing.