如何为 mozilla、chrome 和 IE 编写特定的 CSS
您可以使用什么 CSS 条件语句来包含 IE、Mozilla、Chrome 的特定 CSS。
If IE
#container { top: 5px; }
If Mozilla
#container { top: 7px; }
If Chrome
#container { top: 9px; }
各自的“如果”是什么?
What would be the CSS conditional statement you can use to include specific CSS for IE, Mozilla, Chrome.
If IE
#container { top: 5px; }
If Mozilla
#container { top: 7px; }
If Chrome
#container { top: 9px; }
What would be the respective 'If's' ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为此,
使用 PHP
请参阅
然后根据检测到的创建动态CSS文件浏览器
这是 CSS Hacks 列表
来源: http://paulirish.com/2009/browser -specic-css-hacks/
如果您想使用插件,那么这里有一个
http://rafael .adm.br/css_browser_selector/
For that
Using PHP
See
Then then create the dynamic CSS file as per the detected browser
Here is a CSS Hacks list
Source: http://paulirish.com/2009/browser-specific-css-hacks/
If you want to use Plugin then here is one
http://rafael.adm.br/css_browser_selector/
您可以使用 php 将浏览器名称回显为
body
类,例如然后,您的条件 CSS 看起来像
You could use php to echo the browser name as a
body
class, e.g.Then, your conditional CSS would look like
对于干净的代码,您可以使用此处的 javascript 文件: http://rafael.adm.br/css_browser_selector/
通过包含以下行:
您可以使用以下简单模式编写后续 css:
For clean code, you might make use of the javascript file here: http://rafael.adm.br/css_browser_selector/
By including the line:
You can write subsequent css with the following simple pattern:
由于标签中还包含 PHP,因此我将建议一些服务器端选项。
最简单的解决方案是大多数人在这里建议的解决方案。我通常遇到的问题是,它可能会导致您的 CSS 文件或
第二个最佳解决方案(我发现)是让 php 输出实际的 css 文件,即
其中
这允许您创建更小、更易于浏览器处理的文件。
我发现的最好的方法是特定于 Apache 的。方法是使用mod_rewrite或mod_perl的PerlMapToStorageHandler根据渲染引擎将URL重新映射到系统上的文件。
假设您的网站是
http://www.myexample.com/
并且它指向/srv/www/html
。对于 chrome,如果您请求 main.css,则不会加载/srv/www/html/main.css
它会检查是否存在/srv/www/html/main .webkit.css
如果它存在,它将转储它,否则它将输出 main.css。对于 IE,它会尝试main.trident.css
,对于 Firefox,它会尝试main.gecko.css
。与上面一样,它允许我创建更小、更有针对性的 css 文件,但它也允许我更好地使用缓存,因为浏览器将尝试重新下载文件,并且 Web 服务器将向浏览器提供适当的 304 来告诉它,您无需重新下载。它还为我的 Web 开发人员提供了更多的自由,而无需他们为目标平台编写后端代码。我也有 .js 文件被重定向到 javascript 引擎,对于main.js
,在 chrome 中它尝试main.v8.js
,在 safari 中,main .ninto.js
,在 Firefox 中,main.gecko.js
。这允许更快地输出特定的 javascript(更少的浏览器测试代码/功能测试)。当然,开发人员不必针对特定目标,可以编写main.js
而不是制作main..js
并且它会正常加载。即有一个main.js
和一个main.jscript.js
文件意味着 IE 获取 jscript 文件,其他人获取默认 js,与 css 文件相同。Since you also have PHP in the tag, I'm going to suggest some server side options.
The easiest solution is the one most people suggest here. The problem I generally have with this, is that it can causes your CSS files or <style> tags to be up to 20 times bigger than your html documents and can cause browser slowdowns for parsing and processing tags that it can't understand
-moz-border-radius
vs-webkit-border-radius
The second best solution(i've found) is to have php output your actual css file i.e.
<link rel="stylesheet" type="text/css" href="mycss.php">
where
This allows you to create smaller easier to process files for the browser.
The best method I've found, is specific to Apache though. The method is to use mod_rewrite or mod_perl's PerlMapToStorageHandler to remap the URL to a file on the system based on the rendering engine.
say your website is
http://www.myexample.com/
and it points to/srv/www/html
. For chrome, if you ask for main.css, instead of loading/srv/www/html/main.css
it checks to see if there is a/srv/www/html/main.webkit.css
and if it exists, it dump that, else it'll output the main.css. For IE, it triesmain.trident.css
, for firefox it triesmain.gecko.css
. Like above, it allows me to create smaller, more targeted, css files, but it also allows me to use caching better, as the browser will attempt to redownload the file, and the web server will present the browser with proper 304's to tell it, you don't need to redownload it. It also allows my web developers a bit more freedom without for them having to write backend code to target platforms. I also have .js files being redirected to javascript engines as well, formain.js
, in chrome it triesmain.v8.js
, in safari,main.nitro.js
, in firefox,main.gecko.js
. This allows for outputting of specific javascript that will be faster(less browser testing code/feature testing). Granted the developers don't have to target specific and can write amain.js
and not makemain.<js engine>.js
and it'll load that normally. i.e. having amain.js
and amain.jscript.js
file means that IE gets the jscript one, and everyone else gets the default js, same with the css files.Paul Irish 对 IE 特定 CSS 的方法是我见过的最优雅的方法。它使用条件语句将类添加到 HTML 元素,然后可以使用这些类来应用适当的 IE 版本特定 CSS,而无需求助于 hack。 CSS 已通过验证,并将继续在未来的浏览器版本中发挥作用。
该方法的完整细节可以在他的网站。
这不包括 Mozilla 和 Chrome 浏览器特定的黑客攻击……但我确实发现我并不需要这些。
Paul Irish's approach to IE specific CSS is the most elegant I've seen. It uses conditional statements to add classes to the HTML element, which can then be used to apply appropriate IE version specific CSS without resorting to hacks. The CSS validates, and it will continue to work down the line for future browser versions.
The full details of the approach can be seen on his site.
This doesn't cover browser specific hacks for Mozilla and Chrome... but I don't really find I need those anyway.
您可以在 css 文件中使用此代码:
代码 -webkit-top:9px;适用于 chrome,-moz-top:7px 适用于 mozilla,最后一个适用于 IE。
玩得开心!!!
you can use this code in your css file:
the code -webkit-top:9px; is for chrome, -moz-top:7px is for mozilla and the last one is for IE.
Have Fun!!!
使用代理检测器,然后使用您的网络语言创建程序来创建 css,
例如在 python 中
use agent detector and then with your web language create program to create css
for example in python
查看此链接:http://webdesignerwall.com/tutorials/css-specific -for-internet-explorer
2 特定于 Explorer 的 CSS 规则(IE CSS hacks)
另一种选择是声明只能由 Explorer 读取的 CSS 规则。例如,在 CSS 属性前添加星号 (*) 将针对 IE7,或在属性前添加下划线将针对 IE6。但是,不建议使用此方法,因为它们不是有效的 CSS 语法。
IE8 或更低版本:要编写专门针对 IE8 或更低版本的 CSS 规则,请在分号之前添加反斜杠和 9 (\9)。
IE7 或更低版本:在 CSS 属性前添加星号 (*)。
IE6:在属性前添加下划线(_)。
。盒子 {
}
Check out this link : http://webdesignerwall.com/tutorials/css-specific-for-internet-explorer
2 CSS Rules Specific to Explorer (IE CSS hacks)
Another option is to declare CSS rules that can only be read by Explorer. For example, add an asterisk (*) before the CSS property will target IE7 or add an underscore before the property will target IE6. However, this method is not recommended because they are not valid CSS syntax.
IE8 or below: to write CSS rules specificially to IE8 or below, add a backslash and 9 (\9) at the end before the semicolon.
IE7 or below: add an asterisk (*) before the CSS property.
IE6: add an underscore (_) before the property.
.box {
}
将您的 css 放入以下脚本中并将其粘贴到您的 CSS 文件中。
@media screen 和 (-webkit-min-device-pixel-ratio:0) { 你完整的 css 样式 }
例如:
@media screen and (-webkit-min-device-pixel-ratio:0) { container { margin-top: 120px;} }
就像一个魅力。
Place your css in the following script and paste it into your CSS file.
@media screen and (-webkit-min-device-pixel-ratio:0) { your complete css style }
For example:
@media screen and (-webkit-min-device-pixel-ratio:0) { container { margin-top: 120px;} }
Works like a charm.