views:

82

answers:

4

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

I want to keep all CSS in one file.

+1  A: 

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

Daniel A. White
instead of using conditional commnet and different css and css hacks i wanted to add a class to body then i can use that class for all IE specfic CSS needs.
metal-gear-solid
+2  A: 

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 */
}
Nick Craver
you mean 2 different `body`
metal-gear-solid
I want to keep all style in one css.
metal-gear-solid
@metal-gear-solid - You place this after your main CSS, whatever styles are in it's `body` declaration will override previously defined properties (barring `!important`), just like any other CSS would. For this part "I want to keep all style in one css"...well, you can't have *valid* CSS and do IE specific styling then, you'll have to use hacky CSS, which I advise against.
Nick Craver
Darn, Nick... give the rest of us a chance... ;) Why don't you get married or something?! (went to your site the other day... All the best to you two!)
Gert G
@Gert - You'll see a flat-line in my activity graph around mid-September ;)
Nick Craver
@metal-gear-solid: You ought to separate the IE specific styles or you'll get a maintenance nightmare. :)
Gert G
Agree with Gert G, but if there's only one or two and you don't want to server-side merge your CSS file you can probably get away with a heavily notated "THIS IS FOR IE" section in your CSS file.
Graphain
+1  A: 

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.

MiffTheFox
+1  A: 

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.

David Thomas
Clever idea to use IE tags in body, not just on the style declarations.
Graphain
@ricebowl - is it semantically correct to use use 2 `body`
metal-gear-solid
It allows for perfectly valid css, in a single stylesheet, the html is also valid, depending on your views on IE conditional comments. But yeah, it should validate fine.
David Thomas
@metal-gear-solid: what? No. It's **never** valid to have more than one `body` tag inside a (x)html document*. Have I led you to think it might be? (* Unless the subsequent `body` tags are within frames/iframes and the parent document has an appropriate doctype.)
David Thomas