views:

5891

answers:

8

Is it possible to create, for instance, a box model hack while using in-line CSS?

For example:

<div id="blah" style="padding: 5px; margin: 5px; width: 30px; /*IE5-6 Equivalent here*/">

Thanks!

A: 

The most appropriate answer is don't. (Edit: to be clear, I mean don't do it inline, I don't mean don't use CSS hacks.)

Edit: This doesn't work, IE ignores the conditional comment. Leaving the answer here to not be a bastard.

The next most appropriate answer is conditional comments:

<div id="blah" style="padding: 5px; margin: 5px; width: 30px; <!--[if lte IE 6]> ... <![endif]-->">
eyelidlessness
This isn't valid HTML though.
Konrad Rudolph
Can such conditional comments be written in-tag?
@Konrad: Why not? It's a valid HTML comment. IE interprets it (which is not valid), but it is still a valid comment.
eyelidlessness
@Daimonan: They can be written anywhere a comment can be written.
eyelidlessness
+2  A: 

Without arguing for or against CSS hacks, personally if I needed to do something like that, I would prefer to use a conditional comment:

<!--[if lt IE 7]>
<style>
#blah {
padding: 5px;
margin: 5px;
width: 30px;
}
</style>
<![endif]-->
alexp206
+6  A: 

I'd go outside - slap a class on that element, or use the ID you have, and handle the styling externally.

I'd also concur with the conditional comments answers preceding mine.

That said: As an absolute last resort, you can use the following style hacks to target <= IE6, and even IE7. The trouble comes when/if they fix IE8 to defeat your hack.

.foo {
padding: 5px;
^padding: 4px; /* this targets all IE, including 7. It must go first, or it overrides the following hack */
_padding: 3px; /* this targets >= IE6 */
width: 30px;
}

Good luck.

John Dunagan
For completeness sake, * evidently works the same as ^.
eyelidlessness
Certainly. In fact, any character BUT _ (underscore) can be used to foil IE7.
John Dunagan
+6  A: 

You can use the "prefixing" hack in inline styles as well:

<div style="*background:red"></div>

Just make sure you put the IE hacks at the end of the style attribute. However I second the opinion that inline styles should be avoided when possible. Conditional comments and a separate CSS file for Internet Explorer seem to be the best way to handle such issues.

korchev
A: 

Keep in mind that IE 6 needs the box model hack in quirks mode but not in standards mode. IE 5 and IE 5.5 need the BMH all the time.

So if you're in standards mode, you'll need to use something like the original voice-family hack (which targets IE 5.X and not IE 6). If you're in quirks mode, any old IE <= 6 hack will do.

(The content of your question suggests to me that your page renders in quirks mode.)

savetheclocktower
A: 

Yeah like everyone above, if you can avoid doing it inline do!! But if you really need to go inline then parser filters are probably your best bet, these are certain characters you can use on properties such as the underscore hack in ie6

print("code sample");

 style="position:relative;padding:5px; _position:absolute; _padding:10px;"

ie6 will still get the underscored styles, everyone else will just ignore them!

There is also using !important instead of underscore.

have a play around and see what happens, but again like above, try and avoid like the plague :)

Paul M
A: 

Read this and check that solves your problem. http://praveenbattula.blogspot.com/2009/05/ie-hacking-apply-styles-only-to-ie.html

Rare Solutions
A: 

The best solution is to work in Standards Mode rather than Quirks Mode.... that'll eliminate the need for the majority of your box model hacks right away.

Beyond that, conditional comments with an IE-specific stylesheet are far cleaner and more maintainable. That method has been good enough for most every top-notch designer for the last several years... unless there's something specific about your site that requires it all to be inline I suggest taking a step back and looking at the root problems instead of how you can patch these little symptoms as they appear.alt text

Gabriel Hurley