Drupal 7 Browser specific css

瘦欲@ 提交于 2019-11-30 16:11:44

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!