问题
How can i have a browser specific css in Drupal 7 . I would like to load chrome.css if browser is chrome . Please help
回答1:
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 !
回答2:
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?
回答3:
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.
来源:https://stackoverflow.com/questions/5883140/drupal-7-browser-specific-css