Noupe Editorial Team November 13th, 2007

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.

Further Readings
Credits

Thanks to our friends "Karim" and "pacotole" for pointing out Dustin Diaz’ nice hack for min-height.

Thanks to our friend "Narga" for creating a vietnamese translation page on http://www.narga.net/348

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

37 comments

  1. Unfortunately, Stylegala’s PHP destroys the browser’s capability to cache the CSS regardless of any meta settings and the styles will be downloaded with _every_ request. Not good.

    So, from a professional e-commerce developer’s point of view, you’ve got a list of 6 good hacks.

    Still, its a good effort. Thanks. I’ll probably use a few of these.

  2. Hack #3 alternative without javascript:

    #id {
    min-height: 100px;
    height:auto !important;
    height:100px;
    }

  3. Many thanks to Karim, pacotole for pointing out Dustin Diaz’s hack for min-height, i added it to hack#3 today and credits were given to them.
    Narga: Thanks for translating this post to vietnamese, credits is given to you too.

  4. David: To allow browser caching insert this code at the top of the php file.

    
    < ?php
    
    $headers = apache_request_headers();
    
    // If there is a 'If-Modified-Since' in the request header and this file has NOT been modified since the 'If-Modified-Since'
    
    if (isset($headers['If-Modified-Since']) &#038;&#038; strtotime($headers['If-Modified-Since']) >= filemtime(__FILE__))
    
    {
    
    header(&#8217;HTTP/1.1 304 Not Modified&#8217;);
    
    die();
    
    }
    
    // Set the cache to expire in a week
    
    $nextWeek = gmdate(&#8217;D, d M Y H:i:s&#8217;, mktime(date(&#8217;H'), date(&#8217;i'), date(&#8217;s&#8217;), date(&#8217;m'), date(&#8217;d')   7, date(&#8217;Y')));
    
    header(&#8217;Expires: &#8216; . $nextWeek . &#8216; GMT&#8217;);
    
    // Set when the file was last modified
    
    header(&#8217;Last-Modified: &#8216; . gmdate(&#8217;D, d M Y H:i:s&#8217;, filemtime(__FILE__)) . &#8216; GMT&#8217;);
    
    // Do browser tests and output here or if you want to save cpu too you can do this,
    
    // but make sure to check the modified time of &#8216;actual_css.php&#8217;
    
    include(&#8217;actual_css.php&#8217;);
    
    ?>
    
    

    There’s also this method, it uses javascript:

    http://rafael.adm.br/css_browser_selector/

  5. Online Television:
    GIF files are only capable of displaying a pixel as either completely transparent or completely opaque. When an image contains alpha layers, however, parts of an image can be partially transparent. PNGs thus have the potential for creating some interesting effects on a web page, like translucent background images and drop-shadows.
    It realy depend on quality of image that you want and if you want to show some effects like drop shadows, transparency, gradient effects, etc…

  6. Great article, nice summary of approaches that save you from pulling out your hair.

    BTW small typos in point 6 “teh folloing” should be “the following” and in point 7, “methos” should be “method”.

Sorry, Comments are closed...