Drupal 7 浏览器特定 css

发布于 2024-11-04 17:14:39 字数 72 浏览 3 评论 0原文

我怎样才能在 Drupal 7 中拥有浏览器特定的 css 。如果浏览器是 chrome 我想加载 chrome.css 。请帮忙

How can i have a browser specific css in Drupal 7 . I would like to load chrome.css if browser is chrome . Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-11-11 17:14:39

您可以在 template.php 文件中使用 preprocess_html 执行此操作主题挂钩和 drupal_add_css 函数。

您将在 drupal 7 主题中找到示例,例如在 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));

编辑:由于 chrome 没有条件注释,您可以检查 $_SERVER['HTTP_USER_AGENT'] :

if (isset($_SERVER['HTTP_USER_AGENT'])
    && stripos($_SERVER['HTTP_USER_AGENT'], 'chrome')!==false) {
    drupal_add_css( ... );
}

但在执行此操作之前,尝试修复你的CSS规则

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 !

缺⑴份安定 2024-11-11 17:14:39

我建议使用条件样式表模块

Internet Explorer 实现了一项名为
有条件的评论。虽然网络开发人员对技术不屑一顾
许多 CSS 开发人员发现,不支持跨浏览器
条件注释非常有用。他们可以在他们的应用程序中使用更干净的 CSS
正常样式表,并且可以通过放置来修复 IE 中损坏的渲染
条件注释内仅限 IE 的 CSS;这个技术是均匀的
微软推荐。
如果没有此模块,则拥有 IE 条件样式表的唯一方法
是添加 37 行代码(如果您想添加多于一行,则需要更多行代码)
样式表)在四个非常难记住的函数调用中
你的主题的template.php。布莱赫。谁想要那个?

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?

○闲身 2024-11-11 17:14:39

我同意之前发言者的观点,你绝对应该尝试更改你的 css,这样就不需要特定于浏览器的代码,但如果你确实需要针对 Webkit 浏览器(Chrome 和 Safari),你可以看看这个解决方案:

http://www.evotech.net /blog/2010/04/hack-for-webkit-filter-for-chrome-and-safari/

它并不是 Drupal 特有的,而是一种在紧急情况下针对 webkit 的普遍接受的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文