views:

294

answers:

6

I want to have a particular CSS style different for IE6. Basically I am using CSS sprites with a PNG file. But for IE6 I want to use the .gif version.

I dont want to use the <!-- if lte IE6 statement. I want to include it within my CSS file itself.

How do I do that?


Edit:

I need this because I want my users to include a single CSS file and not 4 lines of code. My users are absolute newbies. I don't want to confuse them. Plus the only change I want is to use .gif instead of the current .png.


Edit:

How does _background-image: sound? Is there anything wrong with that?
Alternatively, can I use a conditional statement inside a CSS file?

A: 

Use a conditional comment.

<!--[if IE 6]>
<link rel="stylesheet" href="/ie6.css">
<![endif]-->

edit: Now that Wellbog has fixed your question, no, there's no way to do that with pure valid CSS.

You could conceivably use PHP or another server-side language to detect IE6 from the user agent string and serve a different CSS file, but it's much better to just use the conditional commenting technique.

What's your reason for refusing to use the existing, working, non-hacky solution Microsoft provides?

ceejayoz
He specifically said he doesn't want to.
Welbog
That part wasn't visible until you edited the question.
ceejayoz
You had as much opportunity to edit the question to see the rest of the obvious cut-off sentence as I had.
Welbog
I've seen enough obvious cut-off sentences that *didn't* have hidden HTML in them to not waste my time trying to read minds.
ceejayoz
+4  A: 

Apparently you can put IE6 specific statements into a CSS by prefixing them with an underscore.

See http://vexxhost.com/blog/2007/03/01/only-css-hack-you%E2%80%99ll-ever-need-seriously/

Robert Harvey
+1  A: 

Here's a pretty comprehensive list of unrecommended hacks: http://www.javascriptkit.com/dhtmltutors/csshacks3.shtml

Max Schmeling
+8  A: 

If you don't want to use conditional comments, then you can use the * html hack:

h1 {
    color: green;
}

* html h1 {
    color: red; /* this will only be applied by IE 6, 5.5, 5, and 4 */
}
NickFitz
+1 for the answer Alec is looking for ... but certainly not for encouraging css hacks when IE conditional statements are far better.
rpflo
reducing downloads is a valid reason, but one that should apply to fewer and fewer sites these days
annakata
Obviously conditional comments (not statements) are better, but they are specifically excluded in the original question. I reckon this is the least-harmful alternative.
NickFitz
I Agree completely :)
rpflo
+2  A: 

As you obviously will have noticed from the answers you're getting, using conditional comments for this is so standard that people tell you to do that even when you've specifically said you don't want to.

But if you absolutely have to have the user agent determination made at the CSS file level, what I would do is write a PHP script that outputs the CSS (rather than HTML) and analyze the user agent in PHP. If the file has to be referred to as stylesheet.css or whatever, Apache rewrites or MultiViews can be used to make a PHP script available under that name.

chaos
+1  A: 

You said you don't want to use conditional statements, but they are very much the recommended and best way to go. The main reason is maintinability, CSS browser hacks are often hard for the next person, or you several months down the line, to understand. Having non-hacky CSS in a completely separate file makes it far easier to manage.

I would very much recommend you don't do user agent sniffing, it is open to lots of problems, for instance many browsers report themselves as IE even when they are not (default in Opera 7 I think). The User-Agent string is not to be trusted and should only be used as a last resort.

roryf
If you don't recognise a CSS hack I don't think you've any business changing the CSS file, but regardless the maintenance issue is more concerned with getting crossfire of what CSS will and will not be applied by future browsers and exotic unintended browsers.
annakata
It's not about recognising a CSS hack it's about knowing WTF it's supposed to do. Agree about the future-proofing, hence why conditionals are recommended. There's times when I've wished Safari/Firefox had something similar...
roryf