简体   繁体   English

链接各种浏览器的单独/条件/唯一样式表

[英]link separate/conditional/unique stylesheets for various browsers

What is the current best practice for pointing gecko, opera, msie, webkit, etc., to their own additional stylesheets? 将壁虎,歌剧,msie,webkit等指向自己的其他样式表的当前最佳做法是什么?

Browser sniffing is out? 浏览器嗅探出来了吗? Most of what I'm finding online are old posts from 2008, or theoretical arguments about right vs. wrong, or how to use or whatever. 我在网上找到的大部分内容都是2008年以来的旧帖子,或者是关于对与错,如何使用等的理论论证。

There must be something more sophisticated that we can use for this. 我们必须为此使用一些更复杂的东西。 Something jquery or php that says, (pseudo-code), "IF moz, @import url(moz.css)" or whatever. 诸如jquery或php这样的东西,(伪代码),“ IF moz,@ import url(moz.css)”或其他内容。

Detect browser from the useragent and include appropriate css for that browser. 从用户代理检测浏览器,并包括适用于该浏览器的CSS。

$browser = get_browser($_SERVER['HTTP_USER_AGENT'], true);

switch ($browser['browser']) {
    case "IE": //load ie specific css
                break;
    case "Firefox":
    case "Mozilla":   //load gecko specific css
        break;

    case "Safari":  //load safari css
           break;
    case "Opera": //load opera css
          break;

    default:
        echo '<link href="default.css" rel="stylesheet" type="text/css" />';
}

Here is a detailed article on get_browser() function. 这是有关get_browser()函数的详细文章

Detecting specific browsers is a bad idea, as different versions can have different features which you may not be aware of. 检测特定的浏览器不是一个好主意,因为不同的版本可能具有您可能不知道的不同功能。 A better solution is to use something like Modernizr to detect features , and style accordingly. 更好的解决方案是使用Modernizr之类的 功能来检测features并相应地设置样式。

If you must detect different rendering engines for whatever reason though, in PHP it's relatively straightforward using the browser_info() method: 如果无论出于何种原因都必须检测不同的渲染引擎,则在PHP中使用browser_info()方法相对简单:

<head>
    ...
    <!-- Stylesheets for all browsers -->
    <link rel="stylesheet" href="style.css" />

    <?php
    $browser    = get_browser(null, true);

    switch($browser['browser']){
        case 'Firefox':
            // User is using Firefox
            $style  = 'firefox.css';
            break;

        case 'Safari':
            // User is using Safari
            $style  = 'safari.css';
            break;
    }

    if(isset($style)){
        // Specific browser stylesheet set - add to page
        ?>
        <link rel="stylesheet" href="<?php echo $style;?>" />
    <?php } ?>
</head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM