XML technologies / CSS / CSS Cookbook / Hacks, workarounds and debugging / A smart system for managing CSS hacks

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Develop a system that lets you manage correct CSS rules separately from those used for hacks and workarounds.

Solution

Include a stylesheet in your web page through the element link:

<link rel="stylesheet" type="text/css" media="screen, presentation" href="/_assets/css/screen/screen.css"/>

Within screen.css, import the stylesheet containing the correct values:

@import url(csscookbook.css);

Now use various filters to import the CSS rules needed to correct problems in particular browsers (see the next figure). One such browser is Internet Explorer 5.x for Windows. A Mid-Pass Filter lets you load styles specifically for this browser:

@import url(csscookbook.css);
/* Stildefinitionen speziell für Internet Explorer 5 für Windows */
 @media tty {
  i{content:"\";/*" "*/}} @import 'winie5.css'; /*";}
 } 
/* */

Another browser requiring its own rules is Internet Explorer for Macintosh. The “Mac Bandpass Filter” lets you supply this browser with its own rules too:

@import url(/_assets/css/csscookbook.css);
/* Stildefinitionen speziell für Internet Explorer 5 für Windows */
 @media tty {
  i{content:"\";/*" "*/}} @import '/_assets/css/winie5.css'; /*";}
 }
/* */
/* Stildefinitionen speziell für Internet Explorer für Macintosh */
/*\*//*/&#8232;
@import "ie5mac.css";&#8232;
/**/

Diagram: a smart system for managing CSS hacks

Figure: Diagram of the smart system for managing CSS hacks.

Discussion

Managing browser-specific stylesheets separately offers several advantages. First, the stylesheet containing correct styles remains free of hacks and workarounds.

Second, putting browser-specific hacks in separate files makes these rules easier to manage as well. If you later stop supporting a particular browser, simply delete its rules and the instructions that load the corresponding stylesheet.

The technique discussed here uses CSS hacks to specify browser-specific stylesheets. Another approach is a server-side solution. Web developer Mark Pilgrim devised a method based on the module mod_rewrite for the Apache web server.

The web server identifies the browser and its version, then sends the additional definitions needed by that browser along with the base stylesheet.

See also

Molly E. Holzschlag’s article on managing CSS hacks.

  

 

 

 

back to top