Wed. July 23, 12:23:35 AM
In browsers that support the data: URI scheme, this link provides a live demo of your CSS demo.
While I strongly advise using hacks at a minimum especially when it comes to CSS there is a time and a place for them, especially when you need a quick way to target a browser other than Internet Explorer.
Most standards favoring browsers (Firefox, Opera & Safari) have no method of targeting CSS to the specific browser while Internet Explorer, the buggiest browser, has a completely safe and easy method of serving CSS/HTML to only IE.
To show that the hacks are working correctly I created 6 paragraphs with their specific browser/version identifier within them. This will let you know that the hack is working correctly
<p id="opera">Opera 7.2 - 9.5</p>
<p id="safari">Safari</p>
<p id="firefox">Firefox</p>
<p id="firefox12">Firefox 1 - 2 </p>
<p id="ie7">IE 7</p>
<p id="ie6">IE 6</p>
Next I automatically hid all P tags:
<style type="text/css">
body p
{
display: none;
}
</style>
The easiest way to target IE is with conditional comments. There is a robust syntax that Microsoft has created to allow authors to do this. I'm not going to go into detail about this since it has been re-hashed a million times by other bloggers but here are two alternatives to parser CSS hacks:
<!--[if IE 7]>
<style type="text/css">
</style>
<![endif]-->
<!--[if IE 6]>
<style type="text/css">
</style>
<![endif]-->
I wouldn't recommend using these hacks since conditional comments are really, really easy to use but if you want to keep all your CSS in one file here is an alternative. Note that the IE7 hack will only apply to IE7 because IE6 does not understand the > selector. It should also be noted that no other browser will recognize this hack.
/* IE 7 */
html > body #ie7
{
*display: block;
}
/* IE 6 */
body #ie6
{
_display: block;
}
The first hack targets only Firefox 1 and 2 by using the body:empty hack. The second hack, which is quite clever target all versions of Firefox by using the proprietary extension -moz. -moz combined with the -document url-prefix() which by the way is used by Firefox Addon creators targets Firefox and Firefox alone. I've found that by using proprietary extensions to CSS, as hacks, usually means the most surefire way to target individual browsers without having to worry about another browser possibly parsing the CSS.
/* Firefox 1 - 2 */
body:empty #firefox12
{
display: block;
}
/* Firefox */
@-moz-document url-prefix()
{
#firefox { display: block; }
}
The Safari CSS hack works similar to the Firefox hack because it uses a Safari proprietary extension to CSS. By using the -webkit prefix we can target Safari and only Safari.
/* Safari */
@media screen and (-webkit-min-device-pixel-ratio:0)
{
#safari { display: block; }
}
The Opera CSS hack works because of negation in CSS. Currently I feel this hack is the weakest of all the hacks I've listed simply because it's targeting ALL browsers that support -min-device-pixel-ratio that aren't -webkit. It will only be a matter of time before Firefox supports this and this hack will then most likely apply to Firefox as well.
/* Opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0)
{
head~body #opera { display: block; }
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CSS Browser Hacks</title>
<style type="text/css">
body p
{
display: none;
}
/* Opera */
html:first-child #opera
{
display: block;
}
/* IE 7 */
html > body #ie7
{
*display: block;
}
/* IE 6 */
body #ie6
{
_display: block;
}
/* Firefox 1 - 2 */
body:empty #firefox12
{
display: block;
}
/* Firefox */
@-moz-document url-prefix()
{
#firefox { display: block; }
}
/* Safari */
@media screen and (-webkit-min-device-pixel-ratio:0)
{
#safari { display: block; }
}
/* Opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0)
{
head~body #opera { display: block; }
}
</style>
</head>
<body>
<p id="opera">Opera 7.2 - 9.5</p>
<p id="safari">Safari</p>
<p id="firefox">Firefox</p>
<p id="firefox12">Firefox 1 - 2 </p>
<p id="ie7">IE 7</p>
<p id="ie6">IE 6</p>
</body>
</html>
Tags: css, css hacks, css3 ( Add Tag )
In browsers that support the data: URI scheme, this link provides a live demo of your CSS demo.
Hmmm... that link got borked because your message board converts the percent sign character to the word "percent".
Lol, good idea though Chris. If you want, email me the data uri and I'll post it via my database so that it doesn't filter out "percent"
I fixed your link Chris. I kind of like your data:uri idea. One thing I hate about blogs/posts is having all these orphaned images/css/files hanging around my web server long after I've posted. With the data:uri's you don't need to worry about all those useless orphaned files.
By the way, is there a website you use to convert HTML to a data:uri?
I prefer to use this one, seems to do a good job. The Wikipedia article has a few code examples too, if you wanted to create your own.
How do use the all firefox versions hack? Do I add my specific selectors to the start or end?
@Daniel, for the all firefox version you simply do this:
@-moz-document url-prefix()
{
/* place your selectors here */
}
One strategy to keep this clean would be to use your scripting language to detect the user agent (obviously a bit weak) and then add a link to these extra rules. Precluding that one day all browsers will really render the same way, it would be nice if each browser had its own conditional comment-type loophole.
Yes, it is best to avoid “hacks”, however Conditional Comments mean the by far worst implications for maintainability …
Hacks are sort of the chicken and the egg scenario. We don't want to use hacks but have to because "some" browsers have poor standards support, but by using hacks, those browsers are fearful of making rapid changes in their support.
I personally don't like hacks that rely on browser compatibility but by using browser prefixes such as -webkit or -moz I feel a bit safer using the hacks.
Great article.
I personally use the 'star html' hack (and it's derivative) for IE6 and 7.
You can view an article outlining the modified star html hack for IE7 here:
http://thingsilearn.wordpress.com/2007/01/31/hello-world/
Hey Neil,
Thanks very much for this really well written article. I have more hair on my head for it.....
The sooner browsers like ie 6 dissappear the better!!!
James
The IE hacks are pretty well known - and the ones you gave are not the only ones in use by webdesigners
However, I'm really not sure about using the hacks for Safari. Opera or Firefox for the sole reason that I feel they could easily become obsolete as you are reallying on coding mistakes within the software itself...
IE is so unresponsive to webdesigners that their coding mistakes will remain features for ever and a day... but the other browsers pride themselves in seeking perfection...
I wonder whether overnight changes via software updates would make these hacks redundant
great!! guys I really appreciate you all's idea.
All the hacks seems to have their values that's why I have kept in my library.
However, I would like to comment seriously regarding safari hack..I used safari hack but It was not working, later I used (head~body) of opera and placed in safari hack, it worked.
Now tell me what was the actual problem ...???
@Manoj: could you give me an example or link me to a page that uses the example?
hit paylaşmak için www.hitsihirbazi.com süper bi site deneyin.
Thanks for this, I will give it a shot! :-)
Those hacks are working but seems doesn't valid css, so how to make it a valid?
@Firman: The hacks that use the vendor specific CSS3 properties will not validate. Examples of this are -webkit or -moz. They will probably become valid CSS someday when the W3C incorporates them in the CSS3 spec fully.
Very Sweet. Best thing I have seen on this subject.
I see, so we just wait the W3C for it then. BTW thanks for these hacks
Hey thanks for the article, very helpful.
thanks for the great article
can u please give me the code to write in aspx page for mozilla check
Thanks You...
bursa evden eve
Very cool writeup, had to use the opera hack for a project, and worked like a charm!
Thanks..
izmir matbaa
Well done! I tested the opera hack to fix kubrick theme for wordpress, it rocks...The theme is OK in opera now..
------------------------------------------------------------------------------
/*Visible to only Opera*/
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
#content {margin: 0 35px 0 0;}
}
Well done! I tested the opera hack to fix kubrick theme for wordpress, it rocks...The theme is OK in opera now..
------------------------------------------------------------------------------
/*Visible to only Opera*/
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
#content {margin: 0 35px 0 0;}
}
Thank you for this Neal! You are a genius and just made my coding life so much easier. <3
Superb article, that`s what i was loogking for, thanx.
Thanks, I used your fix for Opera, fixing a WP theme.
Nice article, good work :)
Thanks a lot for the hack!
What about Chrome? When I do this, Chrome shows "Safari". I know both Chrome and Safari use webkit. Is there any way to know when is Chrome and when Safari.
Thanks again
I've dropped working with IE-hacks since early 2008: to me hacks are counter-productive and maintaining add-on css-files for IE with CC also isn’t very productive. Fed up with hacking IE I rethought the way I used to target different IE-versions.
The way I work (and I'm not the only working this way):
After the body-tag, I include a couple of cc-spans with the respective IE-version as an ID. These spans get closed right before the /body.
{body}
{if IE 6}{span id=’IE6′}{endif}
{if IE 7}{span id=’IE7′}{endif}
{if IE 8}{span id=’IE8′}{endif}
….
{if IE}{/span}{endif}
{/body}
In my CSS-files, I can now keep all rules together, which is a lot more comfortable when developing and testing.
Example:
#header{margin-top: 5px, background-image: bla.png}
#IE6 #header{margin-top: 7px; background-image: bla.gif}
#IE7 #header{margin-top: 6px}
When revising stylesheets months or years later, I can quickly see where and what I fixed for which IE-version.
I would have liked to make the body-tag conditionnal, but it gets messy sometimes working with content management systems, so I never bothered.
I need to align the width of a text field in google chrome!
therefore, I need CSS hacks for google chrome
Regards Me.
Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes