如何检测用户的浏览器并应用特定的 CSS 文件?
我需要检测浏览器并应用匹配的 CSS 文件。
我创建了 3 个 css 文件:__ie.css、ff.css、opera.css。现在我需要检测浏览器以便包含好的浏览器。
我知道这一点
<!--[if IE]>
<link rel="stylesheet" href="css/ie.css" type="text/css"/>
<![endif]-->
,但是我该如何对 Firefox 和 Opera/Chrome 执行同样的操作呢?
I need to detect the browser and apply the matched CSS file.
I have created 3 css files: __ie.css, ff.css, opera.css. Now I need to detect the browser in order to include the good one.
I know this
<!--[if IE]>
<link rel="stylesheet" href="css/ie.css" type="text/css"/>
<![endif]-->
But how do I do the same with Firefox and Opera/Chrome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最接近纯 CSS 的是使用功能查询。它允许您检查浏览器是否支持特定的属性/值组合,而不是检测浏览器类型/版本。
下面是使用transform属性进行垂直居中的示例。如果浏览器不支持transform,那么我们不想调整其定位:
Browser support for Feature查询
The closest you can come with pure CSS is with feature queries. Instead of detecting the browser type/version, it allows you to check if a specific property/value combinations are supported by the browser.
The following is an example of using the transform property for vertical centering. If the browser doesn't support transform, then we don't want to adjust its positioning:
Browser support for Feature Queries
如果您必须检测浏览器才能应用 CSS,那么您可能需要在使用特定于浏览器的样式表之前重新考虑您的 CSS。所需要的只是一个浏览器模仿另一个浏览器的用户代理字符串,或者发布一个新版本,然后一切都会崩溃。使用当前标准并验证您的代码 (http://validator.w3.org/),您将不得不担心跨浏览器问题少得多。即使仅使用不带版本号的
也可能会破坏更高版本中的布局。
话虽这么说,如果您想根据可用的 CSS 功能来设置不同的页面样式,请查看 Modernizr。这样,您只需检查功能,即使新版本的浏览器发布,这些功能也不会被破坏。
如果所有其他方法都失败并且您确实需要检测访问者的浏览器,请尝试
jquery.browser
。它内置于 jQuery 中,并且使用起来很简单。 http://api.jquery.com/jQuery.browser/。If you have to detect browsers just to apply CSS, then you might want to rethink your CSS before going to browser-specific stylesheets. All it takes is for one browser to mimic another's user agent string, or a new version to be released, and everything breaks. Use the current standards and validate your code (http://validator.w3.org/), and you'll have to worry about far fewer cross-browser issues. Even just using
<!--[if IE]><![endif]-->
without a version number could break the layout in later versions.That being said, if you want to style the page differently based on what CSS features are available, take a look at Modernizr. This way, you're only checking features, which won't be broken if a new version of the browser is released.
If all else fails and you really need to detect the visitor's browser, try
jquery.browser
. It's built into jQuery, and is simple to use. http://api.jquery.com/jQuery.browser/.我需要添加纯 CSS 来在我的应用程序中显示细滚动条,这应该适用于每个浏览器,所以我用这种方式做到了。此代码将检测浏览器并将样式设置为整个应用程序中的滚动条。
I got a requirement of adding pure CSS to show thin scrollbars in my application which should apply for every browser, so I did it in this way. This code will detect the browser and sets the style to the scrollbars in entire application.
如果您需要检测 Firefox、Opera 和 Chrome 的浏览器,那么您就做错了。相同的 CSS 应该适用于所有这些。
当然也有例外——所有浏览器都缺少其他浏览器中不会出现的功能和错误。例如
text-overflow:ellipsis
不支持火狐。但是,如果您坚持使用常用功能,那么这些情况很少见,而且一般来说,除了各种版本的 IE 之外,您现在确实不需要进行浏览器检测。
如果您不确定大多数浏览器是否支持您要使用的功能,您可以访问 CanIUse.com 或 Quirksmode。
如果您使用的是非常前沿的功能,那么是的,您将遇到跨浏览器支持的问题。但在这种情况下,通常最好使用 Modernizr 这样的产品进行功能检测,而不是浏览器检测,因为这将是确保覆盖所有浏览器和所有版本(包括未来版本)的更可靠方法(这是浏览器检测的固有弱点)。
If you need to browser detect for Firefox, Opera and Chrome, then you're doing something wrong. The same CSS should work in all of them.
There are exceptions to this of course -- all the browsers have missing features and bugs which don't appear in the other browsers. An example would be
text-overflow:ellipsis
which isn't supported by Firefox.However, if you're sticking to commonly used features, these cases are few and far between, and in general, you really shouldn't need to do browser detection these days, except for various versions of IE.
If you're not sure whether the features you want to use are supported by most browsers, you can find out at CanIUse.com or Quirksmode.
If you're using seriously cutting-edge features, then yes, you will have problems with cross-browser support. But in this case it's generally better to do feature detection, using a product like Modernizr, rather than browser detection, since this will be a more reliable way of ensuring you cover all browsers and all versions, including future versions (which is an inherent weakeness of browser detection).
有时您可以使用前缀属性,每个浏览器根据其前缀应用自己的属性并忽略其他属性。以下代码通过 CSS3 渐变填充背景:
在此代码中:
但一般来说,浏览器嗅探并不是最好的方法,因为它在较新的浏览器中很容易失败。这样,您必须获取浏览器的用户代理字符串并解析它。然后根据每个浏览器支持的功能应用特定的 CSS 规则。如果浏览器的用户代理字符串在新版本中发生变化,您必须修改浏览器嗅探代码。想象一下,您想要支持多种浏览器,并且每个浏览器都可能在一年内发布一些新版本!
您可以查询浏览器是否支持给定的功能。例如HTML5视频:
有一个基于JavaScript编写的特征检测库,名为Modernizr。您可以下载它并将其包含在您的 html 页面中。此外,您还必须将“no-js”类添加到您的元素中。 Modernizer 运行其所有功能检测测试和一些元素类;像这样的:
所以,你可以有选择地应用 CSS 规则。例如,您想要在支持的浏览器中应用动画 3D 变换或在其他浏览器中显示某些内容。考虑以下代码:
对于默认情况,下面的替代代码:
这是一篇很好的 文章< /a> 描述这个概念。
Sometimes you can use prefixed properties that each browser apply their own properties based on their prefixes and ignore others. Following code fills a background by CSS3 gradient:
In this code:
But generally speaking, browser sniffing is not the best way, because it can easily fail in newer browsers. In that way, you have to get User Agent string of the browser and parse it. Then apply specific css rules for each browser based on its supported features. If User Agent string of the browser change in newer versions, you must modify your browser sniffing code. Imagine that you want to support multiple browsers and each of them may release some new versions in one year!
You can query the browser that if it supports a given feature. e.g. HTML5 video:
There is a feature detection library named Modernizr that is written based on JavaScript. You can download it and include it using in your html page. Also you must add 'no-js' class to your element. Modernizer runs all of its feature detection tests and and some classes to element; something like this:
So, you can apply CSS rules selectively. For instance, you want to apply an animated 3D transform in supporting browsers or display something in others. Consider the following code:
for default case and below for alternative:
This is a good article describing the concept.
除 IE 之外的浏览器没有条件注释。
但你可以用javascript来做到这一点: http://www.quirksmode.org/js/detect.html
There are no conditional comments for browsers other than IE.
But you can do it with javascript: http://www.quirksmode.org/js/detect.html
也许我来得太晚了,但是。更好的解决方案是使用 CSS/JS 浏览器确定器(缩小为 4kb)。 免责声明:这是我销售的商业产品。
要分离 CSS 和 JS 文件,请将此代码粘贴到您的
标记中:
该文件只会为旧浏览器加载(默认情况下,那些不支持 CSS3 转换的浏览器)。如果您想为每个特定浏览器分离文件,您可以执行相同的操作,但将
browser.is_old
更改为browser.chrome
或browser.webkit
等...然后,在 old-browser.css (或任何其他 CSS 文件)中为特定浏览器编写一些 CSS:
Maybe I'm too late, but. The better solution is to use CSS/JS Browser Determiner (4kb minified). Disclaimer: This is a commercial product that I sell.
To separate CSS and JS files, past this code to your
<html>
tag:This file will be only loaded for old browsers (by default, those that does not support CSS3 transition). If you want to separate files for each specific browser you can do the same but change
browser.is_old
tobrowser.chrome
orbrowser.webkit
etc...Then, write some CSS for specific browser inside old-browser.css (or any other CSS file):