2014-10-09 56 views
0

我有一个CSS属性:CSS属性条件时,根据浏览器

top:20px; 

它是确定与Internet Explorer,但不与Firefox浏览器。它应该是Firefox的:

top:0px; 

我怎样才能在CSS添加条件的情况?

回答

0

您可以添加以下条件语句到你的CSS

@-moz-document url-prefix() { 
    .className { 
     top:0px; 
    } 
} 

任何CSS在两者之间@-moz-document url-prefix()将只适用于Firefox浏览器。

但也许更好的选择是将条件应用于IE。您可以在当前的CSS中保留top:0px;,并在文档的head中为IE添加以下条件。

<!--[if IE]> 
    <style> 
     .className { 
     top:20px; 
     } 
    </style> 
<![endif]--> 

注:条件注释不会在IE10 +

+0

IE10 +不支持条件注释http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx – xec 2014-10-09 14:59:20

+0

不知道,谢谢!我会更新我的回答:) – jleggio 2014-10-09 15:01:13

1

支持发现了这片脚本here的,可能是很有用处的你。

// This function returns Internet Explorer's major version number, 
    // or 0 for others. It works by finding the "MSIE " string and 
    // extracting the version number following the space, up to the decimal 
    // point, ignoring the minor version number 
    <SCRIPT LANGUAGE="JavaSCRIPT"> 
    function msieversion() 
    { 
     var ua = window.navigator.userAgent 
     var msie = ua.indexOf ("MSIE ") 

     if (msie > 0)  // If Internet Explorer, return version number 
     return parseInt (ua.substring (msie+5, ua.indexOf (".", msie))) 
     else     // If another browser, return 0 
     return 0 

    } 
    </SCRIPT> 

作为一个说明,CSS是造型你的表,所以你需要通过JavaScript或其他脚本语言来添加这个CSS。


这对于你所需要的有点过分,但是剥下它(以免得到版本号)。它为你的测试形成了一个很好的基础。


一个良好的实施例


下面的例子说明了如何从一个客户端脚本检测浏览器的版本。 注意,这个例子没有明确检查平台版本,如Windows 95,视窗> NT,Windows 3.1的,等等,这些都需要一个单独的userAgent子字符串检查时适用:

<SCRIPT LANGUAGE="javascript"> 
    if (msieversion() >= 4) 

     document.write ("This is Internet Explorer 4 or later"); 

    else if (msieversion() >= 3) 

     document.write ("This is Internet Explorer 3"); 

    else 

     document.write ("This is another browser"); 

    </SCRIPT> 
0
<!--[if IE]> 
    <style> 
     #element{top: 20px;} 
    </style> 
<![end if]--> 

适用于IE的条件注释。