如何为mozilla,chrome和IE编写特定的CSS

Sus*_*usa 20 html css php browser-detection

您可以使用什么CSS条件语句来包含IE,Mozilla,Chrome的特定CSS.

If IE  
#container { top: 5px; }  

If Mozilla 
#container { top: 7px; }    

If Chrome  
#container { top: 9px; }
Run Code Online (Sandbox Code Playgroud)

相应的'If'是什么?

Sta*_*arx 35

为了那个原因

  • 您可以扫描用户代理并找出其版本的浏览器.包括操作系统特定样式的操作系统
  • 您可以为特定浏览器使用各种CSS Hacks
  • 或者脚本或插件来识别浏览器并将各种类应用于元素

使用PHP

看到

然后根据检测到的浏览器创建动态CSS文件

这是一个CSS Hacks列表

/***** Selector 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 */
Run Code Online (Sandbox Code Playgroud)

资料来源:http://paulirish.com/2009/browser-specific-css-hacks/

如果你想使用插件,那么这里就是一个

http://rafael.adm.br/css_browser_selector/


Emm*_*ett 8

您可以使用php将浏览器名称作为body类回显,例如

<body class="mozilla">
Run Code Online (Sandbox Code Playgroud)

然后,你的条件CSS看起来像

.ie #container { top: 5px;}
.mozilla #container { top: 5px;}
.chrome #container { top: 5px;}
Run Code Online (Sandbox Code Playgroud)

  • 我知道,但是如何从中提取浏览器的名称.我这样说,所以你会完成你的答案. (2认同)