The 7 CSS Hacks that we should use
If you are trying to do pixel-perfect cross-browser CSS layout, then you have probably ran into problems with IE . I am going to highlight the top 7 CSS hacks that we often use to have pixel perfect design.
1)Box Model Hack
The box model hack is used to fix a rendering problem in pre-IE 6 browsers, where the border and padding are included in the width of an element, as opposed to added on
padding: 4em; border: 1em solid red; width: 30em; width/**/:/**/ 25em;
2) Conditional Comments
These conditional comments are for IE-only and they’re not supported by any other browser. For other browsers they are just an ordinary comments and therefor, they are safe to use.
The typical usage is as follows:
<!--[if IE]> Some CssCode<![endif]-->
The above code applies to all versions of Internet Explorer, i.e. 5.01, 5.5 and 6.0, but now we want to apply it to versions of Internet Explorer, i.e. 5.01, 5.5 and 6.0, so we will apply the following condition:
<!--[if lte IE 6]> Some Css Code<![endif]-->
After we finish testing, we remove all hacks to separate file(s), so the main CSS is clean and tidy. This separate file is then called in the header section of a file within conditional comments.
<!--[if lte IE 6]> <link rel="stylesheet" type="text/css" href="ie_hacks.css" /><![endif]-->
Condition is one of the following:
- IE (Any version of IE)
- lt IE version (Versions less than version)
- lte IE version(Versions less than or equal to version)
- IE version (Only version)
- gte IE version (Versions greater than or equal to version)
- gt IE version (Versions greater than version)
Version is the version of Internet Explorer, typically 5, 5.5, 6, or 7, you can read more info about this at Quirksmode.
3) Min-width and Max-width of an element
IE doesn't understand this command, so we'll need a new way of making this work in this browser. Let's take a quick example, we need to apply this to a div with id="wrapper":
<wrapper><div id="nav">
Next we create our CSS commands, so as to create a minimum width of 750px:
#wrapper{min-width: 750px;width:expression(document.body.clientWidth < 750? "750px": "auto" );}
You might also want to combine this minimum width of 750px with a maximum width 1220px:
#wrapper{min-width: 750px;max-width: 1220px;width:expression(document.body.clientWidth < 750? "750px" : document.body.clientWidth > 1220? "1220px" : "auto");}
Another Alternative for for min-height without javascript is to use Dustin Diaz’ nice hack: :
#id{ min-height: 100px; height:auto !important; height:100px; }
4) Easy Selectors
Most in-CSS hacks deal with selector bugs. Below is a list of different IE versions and the beginnings of selectors that are known to select elements in them. All of these selectors use valid CSS.
- IE 6 and below
* html {}
- IE 7 and below
*:first-child+html {} * html {}
- IE 7 only
*:first-child+html {}
- IE 7 and modern browsers only
html>body {}
- Modern browsers only (not IE 7)
html>/**/body {}
- Recent Opera versions 9 and below
html:first-child {}
5)Whatever: hover
The :hover selector enables you to have cool effect for html elements like and in tables.Most browsers have no problem with this, except IE which look at the stylesheets and each individual rule with javascript.
If :hover rules can be tracked, and .htc can be used to change an elements behavior, then it should be possible to create a behavior that enables :hover for any element.
You can read more about this here
6)Transparent PNGs
IE dosn't handle transparent PNG too well. You’ll get an ugly grayish type background wherever it’s supposed to be transparent. And we cann't just use GIFs because aren’t good for higher resolution images. So we need a CSS hack to fix this. Follow the following steps and you will be set:
- A HTC script and a transparent GIF will be used to solve this issue. You can download both files here
- Now just upload these 2 files to wherever you store your IE.css file.
- Add one simple line of CSS code to your ie.css file:
img.pngfix { behavior: url(pngHack.htc); }
Another solution can be found at Komodomedia
7) Stylegala- No More CSS Hacks
Stylegala's method is to detect browser version and serve different CSS rules to different user agents, without using hacks or conditional comments. At the same time the end user or validator will never see the CSS rules specified for other browsers than the one they are using. He used some simple PHP code to detect browser type exactly as any CSS hack.
Thanks Scott, i fixed the typos.
“And we cann’t just use GIFs because aren’t good for higher resolution images.”
This isn’t true, the compression options are better on GIF’s, the superior element in PNG’s is that they support Alpha Transparency.
nice stuff!
About “5) Whatever: hover”. It worked for me but, if what you´re doing, e.g. changing the background-position of a background image (css-sprites) this solution does not work, at least it didn´t for me.
Does anyone know how to achive a css-sprites hover-effekt on an element in IE?
Thank you
Alex: PNG makes GIF look pretty pathetic: it supports gamma correction, (sometimes) smaller file sizes, loss-less compression, up to 48-bit color, and, best of all, true alpha transparency.
GIF image can either use no transparent colors at all or have one color that’s completely transparent – there are no degrees of transparency.
A PNG can be transparent in varying degrees – in other words, it can be of variable opacity. And a transparent PNG is background-independent: it can live on any background color or image.
Great Hacks!! I was looking for PNG transparency code And now I got it.
Using CSS expressions for min-width can *seriously* impact the performance of the page as each expression is re-evaluated every time the user does *anything* in the UI (i.e. every move of the mouse).
Check out Steve Souders (formerly of Yahoo!, now of Goooogle) rule that states this with authority:
http://developer.yahoo.com/performance/rules.html#css_expressions
Good article!!
Oh how I cannot wait for IE6 to fall off the side of the earth!!
On #3: IE6 will often crash when your expression sets the width to the same as the width it is looking for, if that makes sense. I’ve run into this a couple of times.
Cameron Moll discusses the problem here:
http://www.cameronmoll.com/archives/000892.html
Excellent list. Thanks for compiling this. It is nice and sane. I’ve seen people do VERY strange things to keep IE in line and I’m glad to see more reasonable things here. Clients – although they are not usually aware – don’t wish to pay $$$ for a stylesheet full of hacks.
Cheers,
Rob
Hi,
You can always avoid the first hack :)
and the img.pngfix { behavior: url(pngHack.htc); } rule will make your css invalid, so include them in your conditional style for IE.
Since it’s a javascript hack actually, i think it’s better to use a javascript that will add or load from a separate sheet this rule.