Since you also have PHP in the tag, I'm going to suggest some server side options.
The easiest solution is the one most people suggest here. The problem I generally have with this, is that it can causes your CSS files or <style> tags to be up to 20 times bigger than your html documents and can cause browser slowdowns for parsing and processing tags that it can't understand -moz-border-radius
vs -webkit-border-radius
The second best solution(i've found) is to have php output your actual css file i.e.
<link rel="stylesheet" type="text/css" href="mycss.php">
where
<?php
header("Content-Type: text/css");
if( preg_match("/chrome/", $_SERVER['HTTP_USER_AGENT']) ) {
// output chrome specific css style
} else {
// output default css style
}
?>
This allows you to create smaller easier to process files for the browser.
The best method I've found, is specific to Apache though. The method is to use mod_rewrite or mod_perl's PerlMapToStorageHandler to remap the URL to a file on the system based on the rendering engine.
say your website is http://www.myexample.com/
and it points to /srv/www/html
. For chrome, if you ask for main.css, instead of loading /srv/www/html/main.css
it checks to see if there is a /srv/www/html/main.webkit.css
and if it exists, it dump that, else it'll output the main.css. For IE, it tries main.trident.css
, for firefox it tries main.gecko.css
. Like above, it allows me to create smaller, more targeted, css files, but it also allows me to use caching better, as the browser will attempt to redownload the file, and the web server will present the browser with proper 304's to tell it, you don't need to redownload it. It also allows my web developers a bit more freedom without for them having to write backend code to target platforms. I also have .js files being redirected to javascript engines as well, for main.js
, in chrome it tries main.v8.js
, in safari, main.nitro.js
, in firefox, main.gecko.js
. This allows for outputting of specific javascript that will be faster(less browser testing code/feature testing). Granted the developers don't have to target specific and can write a main.js
and not make main.<js engine>.js
and it'll load that normally. i.e. having a main.js
and a main.jscript.js
file means that IE gets the jscript one, and everyone else gets the default js, same with the css files.