How can i have a browser specific css in Drupal 7 . I would like to load chrome.css if browser is chrome . Please help
-
Other than IE<9, what do you need browser-specific stylesheets for?Spudley– Spudley2011-05-04 12:26:18 +00:00Commented May 4, 2011 at 12:26
-
@Spudley :I need browser-specific css only for chromeJAB– JAB2011-05-04 12:29:09 +00:00Commented May 4, 2011 at 12:29
-
really? what particular feature?Spudley– Spudley2011-05-04 12:35:17 +00:00Commented May 4, 2011 at 12:35
-
@Spudley : i have set left:100px to a css element. The alignment is perfect in IE 8 and firefox 4 but in chrome i have to set left:350pxJAB– JAB2011-05-04 12:40:01 +00:00Commented May 4, 2011 at 12:40
-
That sounds very odd. Chrome has excellent standards compliance, so I'm very surprised to hear something as fundamental as basic positioning is so badly out. I'd suspect there's something more to this; some other error in your stylesheet which needs to be fixed. I would advise you to look for the cause of the problem rather than trying to fix the symptoms with a hack. If you really can't find it, show us the code here (probably best in a new question), and I'm sure someone will point it out very quickly.Spudley– Spudley2011-05-04 12:47:07 +00:00Commented May 4, 2011 at 12:47
3 Answers
You can do this in your template.php file with a preprocess_html theme hook and drupal_add_css function.
You will find example in drupal 7 theme, for example in bartik :
// Add conditional stylesheets for IE
drupal_add_css(path_to_theme() . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
drupal_add_css(path_to_theme() . '/css/ie6.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 6', '!IE' => FALSE), 'preprocess' => FALSE));
Edit : since there are no conditional comments for chrome, you can check $_SERVER['HTTP_USER_AGENT'] :
if (isset($_SERVER['HTTP_USER_AGENT'])
&& stripos($_SERVER['HTTP_USER_AGENT'], 'chrome')!==false) {
drupal_add_css( ... );
}
But before doing this, try to fix your css rules !
I recommend using the Conditional Stylesheets module
Internet Explorer implements a proprietary technology called Conditional Comments. While web developers frown upon technologies that aren't cross-browser supported, many CSS developers have found Conditional Comments very useful. They can have cleaner CSS in their normal stylesheets and can fix the broken rendering in IE by placing IE-only CSS inside conditional comments; this technique is even recommended by Microsoft. Without this module, the only way to have IE conditional stylesheets was to add 37 lines of code (more if you want to add more than one stylesheet) in four horribly-difficult-to-remember function calls to your theme's template.php. Blech. Who wants that?
Comments
I agree with previous speakers, you should absolutely try to change your css so that does not need browser specific code, but if you really need to target the Webkit browsers (Chrome and Safari) you can have a look at this solution:
http://www.evotech.net/blog/2010/04/hack-for-webkit-filter-for-chrome-and-safari/
It is not specific to Drupal, but a so-so accepted way of targeting webkit, in case of emergency.