Hello everyone,
Inspired by an article at .net magazine about clean browser targeting (article by Jonathan Smiley, just to give the right credits :)) I altered the template.php of zen theme to add a class according to the browser the visitor is viewing the website from. So what I did:
I added the following classes (I kept also the one that zen offers) to the body element:
For Firefox 2 -> ff ff2 (so able to target firefox in general and a specific version also)
Firefox 3 -> ff ff3
Firefox 3.0 -> ff ff30
IE 8 -> ie ie8
IE 7 -> ie ie7
IE 6 -> ie ie6
chrome -> chrome
safari -> safari
This is a great way to fix cross browser compatibility problems without having to call another css file. I do not see any drawback but would be nice to hear the opinion of others.
So in order to implement this just paste the following code at template.php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/Firefox.2/i',$user_agent))
{$classes[]= "ff ff2";}
else if(preg_match('/Firefox.3/i',$user_agent))
{$classes[]= "ff ff3";}
else if(preg_match('/Firefox.3/i',$user_agent))
{$classes[]= "ff ff3";}
else if(preg_match('/Firefox.3.0/i',$user_agent))
{$classes[]= "ff ff3";}
else if(preg_match('/MSIE.8/i',$user_agent))
{$classes[]= "ie ie8";}
else if(preg_match('/MSIE.7/i',$user_agent))
{$classes[]= "ie ie7";}
else if(preg_match('/MSIE.6/i',$user_agent))
{$classes[]= "ie ie6";}
else if(preg_match('/Chrome/i',$user_agent))
{$classes[]= "chrome";}
else if(preg_match('/Safari/i',$user_agent))
{$classes[]= "safari";}
else
{$classes[]= "browser-unknown";}
just before the $vars['body_classes_array'] = $classes;
Thanks,
Dimitris
Comments
Comment #1
jix_ commentedWhile this can be very practical, I'm not sure if it's really the way to go. Targeting certain designs at specific devices (media queries) is fine, but encouraging themers to target at specific browsers (by adding these classes to Zen) really conflicts with the whole idea behind progressive enhancement.
Like I said, it can be very practical when having to fix a bug that appears only in Chrome, for example. But I don't think it should be there by default. Maybe a theme setting so one can enable it as soon as their design becomes a little experimental/buggy … but how may times does one actually need or want that? I know I personally never did.
Comment #2
dtsio commentedSure could be optional. Personally i like not having to load extra .css files for 2-3 IE fixes so this way is better. If of course you use media queries it gets more complicated...
Comment #3
johnalbin