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 */
/*\*//*/

@import "ie5mac.css";

/**/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.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln