确定正在使用的浏览器,然后根据该类设置类的样式

时间:2013-06-11 18:04:11

标签: javascript html css

有没有办法检查网站正在浏览哪个浏览器,然后根据这个更改CSS类中的某些元素,使其在所有浏览器中看起来都一样?

有没有办法在CSS中执行此操作?

编辑:

没错,我现在已经让它在IE和Firefox中工作了,但它在chrome中看起来有些偏差....是否有一些Javascript来检测我是否使用chrome,然后基于此更改margin-bottom类#titleAreaBox?

8 个答案:

答案 0 :(得分:3)

您可以使用Modernizr

逐步增强CSS
  

Modernizr是一个检测HTML5和CSS3功能的JavaScript库   在用户的浏览器中。

答案 1 :(得分:1)

如何使用Conditional CSS

它运行服务器端并允许您在CSS代码中添加条件 这是该项目网站的一个例子:

body {
    font: 90%/1.45em "Lucida Grande", Verdana, Arial, Helvetica, sans-serif;
    margin: 0;
    padding: 0;
    color: #333;
    background-color: #fff;
}

body, html {
    height: 100%;
    width: 100%;
}

.container {
    text-align: center;
    font-size: 3em;
}

[if IE].container {
    height: 100%;
    width: 100%;
    text-align: center;
    line-height: 2em;

    [if gte IE 7]background: url('ie7.png') no-repeat center center;
    [if lte IE 6]background: url('ie.jpg') no-repeat center center;
}

[if Webkit].container {
    position: absolute;
    top: 50%;
    left: 50%;
    height: 70px;
    width: 400px;
    margin-top: -125px;
    margin-left: -200px;
    padding-top: 180px;

    -webkit-border-radius: 30px;
    -webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5);

    background: url('webkit.png') no-repeat center 30px;
    background-color: #eee;

    -webkit-text-stroke: 1px #555;
    text-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5);
}

[if Opera].container {
    position: absolute;
    top: 50%;
    height: 60px;
    width: 100%;
    margin-top: -100px;
    padding-top: 140px;

    background: url('opera.png') no-repeat center 10px;
    background-color: #ccc;
}

[if Gecko].container {
    position: absolute;
    top: 0;
    left: 50%;
    height: 100%;
    width: 300px;
    margin-left: -150px;

    font-size: 32px;
    line-height: 2em;

    background: url('moz.png') no-repeat center center;
    background-color: #ddddff;
}

答案 2 :(得分:0)

您可以使用条件注释为Internet Explorer执行此操作,例如:

<!--[if IE 6]>
    // IE6
<![endif]-->
<!--[if lte IE 8]>
    // IE8 or below
<![endif]-->

对于其他浏览器,您必须使用JavaScript。 (提示:尝试使用navigator.userAgent,在我的计算机上"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36"。)有关详细信息,请see this link

答案 3 :(得分:0)

您需要navigator对象

答案 4 :(得分:0)

对于Internet Explorer,您可以在head html标记内添加:

<!--[if IE 6]>
...include IE6-specific stylesheet here...
<![endif]-->
<!--[if IE 7]>
...include IE7-specific stylesheet here...
<![endif]-->
<!--[if lte IE 8]>
    // IE8 or below
<![endif]-->

对于mozilla,您可以将其添加到样式表,以仅为mozilla浏览器指定css代码

<style type="text/css">
@-moz-document url-prefix() {
    h1 {
        color: red;
    }
}

/***** Selecor Hacks ******/

/* IE6 and below */
* html #uno  { color: red }

/* IE7 */
*:first-child+html #dos { color: red } 

/* IE7, FF, Saf, Opera  */
html>body #tres { color: red }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }

/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }

/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho {  color: red }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 #diez  { color: red  }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
 #veintiseis { color: red  }
}

/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece  { color: red  }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red  }

/* Everything but IE6-8 */
:root *> #quince { color: red  }

/* IE7 */
*+html #dieciocho {  color: red }

/* Firefox only. 1+ */
#veinticuatro,  x:-moz-any-link  { color: red }

/* Firefox 3.0+ */
#veinticinco,  x:-moz-any-link, x:default  { color: red  }



/***** Attribute Hacks ******/

/* IE6 */
#once { _color: blue }

/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }

/* Everything but IE6 */
#diecisiete { color/**/: blue }

/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }

/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }

/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */
</style>

答案 5 :(得分:0)

您可以使用$ _SESSION ['HTTP_USER_AGENT']

通过PHP完成此操作

也可以通过HTML定位IE浏览器: 尝试:

<!--[if IE 6]><link rel="stylesheet" href="path_to_style_ie6.css" type="text/css" media="screen, projection"><![endif]-->
<!--[if IE 7]><link rel="stylesheet" href="path_to_style_ie7.css" type="text/css" media="screen, projection"><![endif]-->

或者在CSS文件中看到溢出问题: Apply CSS rules if browser is IE

答案 6 :(得分:0)

在jQuery中这样做:

$(document).ready(function(){

    if(navigator.userAgent.indexOf("Mozilla") > 0 )
        $(".titleAreaBox").css("margin-top", changed_value);

    if (navigator.userAgent.indexOf("MSIE") > 0 )
        $(".titleAreaBox").css("margin-top", changed_value);

    if (navigator.userAgent.indexOf("Chrome") > 0)
        $(".titleAreaBox").css("margin-top", changed_value);

    if(navigator.userAgent.indexOf("Opera") > 0 ) 
        $(".titleAreaBox").css("margin-top", changed_value);

});

答案 7 :(得分:0)

正如其他人所说,Modernizr是理想的。建议不再检查浏览器版本。相反,您应该测试所需的功能。您可以使用像Modernizer这样的库来帮助您。

EG:要检查地理位置功能,您可以使用此检查:

if (Modernizr.geolocation) { 
    //your code 
}

如果您正在检查浏览器版本,您可能希望获得某些功能,例如地理定位或AJAX(IE出于某种原因使用不同的库在其生命周期内完成此操作)。