有一種方法由瀏覽器的性質做一些這方面你正在使用,有點演繹推理,還有一些技巧......並非所有事情都會這樣做,但只有少數人可以用於使用CSS進行操作系統定位。當然,腳本提供了更多選項。這反映了大約6個月的研究和準確度,如下文所述。
此時我不知道用Chrome來做這件事的方法,我還沒有看過Opera,特別是現在它使用了與Chrome相同的方法。在Mac版本之前,Chrome並沒有發佈適用於Mac的版本。同樣,對於Mac,Google發佈聲明稱Chrome 49之後將不再支持OS X 10.6-10.8,因此Chrome 50和更新版本將會指示Mac OS X 10.9(Mavericks)或新。
雖然Firefox,Internet Explorer和Safari有更好的選擇。
Firefox 4和更新版本可以檢測到正在使用Windows主題,因此即使舊版本的Firefox至少也會檢測到您是否正在使用Windows。我創造了這個而回,並測試了這個今天再次:
@media screen and (-moz-windows-theme) {
.windows {
color:red;
}
}
出於同樣的原因,這一個適用於任何東西,但Windows中,再次爲Firefox 4及更高版本:
@media not screen and (-moz-windows-theme) {
_:-moz-ui-valid, .nonwindows {
color:red;
}
}
-moz-OS - 版本被添加到Firefox的25媒體查詢,但只有幾個選項根據從mozilla文件https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries
操作系統:windows-xp | windows-vista | windows-win7 | windows-win8 | Windows的win10
這套因此只適用於現代的Firefox瀏覽器版本> = 25,在此張貼更新時,Firefox的版本是35
@media screen and (-moz-os-version:windows-xp) {
.windows,.winxp {
color:red;
}
}
@media screen and (-moz-os-version:windows-vista) {
.windows,.vista {
color:red;
}
}
@media screen and (-moz-os-version:windows-win7) {
.windows,.win7 {
color:red;
}
}
@media screen and (-moz-os-version:windows-win8) {
.windows,.win8 {
color:red;
}
}
@media screen and (-moz-os-version:windows-win10) {
.windows,.win10 {
color:red;
}
}
微軟的瀏覽器邊緣(原斯巴達)在這一次僅在Windows 10檢測到它:
/* Microsoft Edge Browser */
@supports (-ms-ime-align:auto) {
.windows,.win10 {
color:red;
}
}
注:以前-MS-加速器:真正的方法得到了新版本中改變,讓一個可用於目標邊緣的特定版本,但不是所有的人。 -ms-ime-align:auto在2017年仍然適用於所有人。
也有檢測Macintosh電腦的方法。
火狐> = 24引入用於OS X 10.7獅子一個新滾動條覆蓋的方法和新的,其與媒體查詢檢測:
@media screen and (-moz-overlay-scrollbars) {
.mac,.mac10_7up {
color:red;
}
}
火狐> = 25還具有一種方法來檢測Mac OS X的自它們在25版本中加入支持OS X字體平滑:
@supports (-moz-osx-font-smoothing:auto) {
.mac,.mac10_6up {
color:red;
}
}
由於@media查詢和@supports過濾器可以在彼此嵌套,以下現在是可能的 - 爲了目標JUST OS X 10.6雪豹與Firefox 24和更新:
@supports (-moz-osx-font-smoothing:auto) {
@media not all and (-moz-overlay-scrollbars) {
.mac,.mac10_6 {
color:red;
}
}
}
火狐17歲以上僅支持Mac OS X 10.6+(雪豹和更新的),所以如果你有任何的上述OS X CSS規則的結果,您不使用OS X 10.5或以上。 (https://en.wikipedia.org/wiki/History_of_Firefox#Version_17_.28ESR.29)
相反,再次在Firefox 25 +中可以否定OS X(10.6和更新版本) - 由於10.5和更舊版本不支持此版本的Firefox,因此這意味着它不是Mac:
@supports (-moz-appearance:none) and (background-attachment:local)
and (not (-moz-osx-font-smoothing:auto)) {
.nonmac {
color:red;
}
}
火狐49中,火狐不再支持Mac OS X 10.9版本之前,所以如果你的版本48或更少它必須是OS X 10.8版或以上,或者如果你有17-48之間的版本中,它必須使用OS X 10.6-10.8。
當我寫這篇文章時,iOS(iPad和iPhone)沒有Firefox的版本,所以帶OS X字體平滑的Firefox確實只覆蓋Mac電腦,而不是Apple電腦。與Chrome for iOS一樣,Firefox for iOS使用Safari引擎,因此在iPad和iPhone上使用Safari黑客。所以他們都是可以定位的 - 就像Safari一樣,但是暗中不是他們所說的那樣。
通過一次否定兩個,我們有辦法的目標還剩下什麼:安卓/ Linux上,再次與Firefox 25和更新:
@supports (-moz-appearance:none) and (background-attachment:local)
and (not (-moz-osx-font-smoothing:auto)) {
@media not screen and (-moz-os-version) {
.lindroid {
color:red;
}
}
}
如果您使用Internet Explorer 6版或更高版本( Windows XP和更新版本),那麼無論如何你都清楚地使用Windows。這可以通過多種方式來確定。在Windows中存在,直到Internet Explorer 9的
條件註釋,所以這些可以被用來收集更多的信息:
這隻能檢測到有窗戶,但不一定是因爲版本的Internet Explorer 6-9只存在在Windows平臺上。這時,Internet Explorer中11是當前版本,因此這不檢測10和11,但9之前:
<!--[if (gte IE 6)&(lte IE 9)]><style type="text/css">
.windows {
color:red;
}
</style><![endif]-->
的Internet Explorer 6只在Windows XP中存在的,還是今天不再使用較老版本Windows,所以這個工程的一個:
<!--[if (IE 6)]><style type="text/css">
.windows,.winxp {
color:red;
}
</style><![endif]-->
的Internet Explorer 7的Windows XP Vista的存在,當你在Internet Explorer中單擊兼容模式選擇8或更高版本但是也常常效仿。
如果您正在使用Internet Explorer 10或這一個新的工作,讓你有Windows 7或8
@media screen and (-ms-high-contrast:active), (-ms-high-contrast:none) {
.windows {
color:red;
}
}
一個我的創作,我個人製作的檢測的Internet Explorer 11或更高版本,這是可以對於Windows 7和更高版本,最高8.1。
_:-ms-fullscreen, :root .windows {
color:red;
}
如果你不想使用Internet Explorer條件註釋,但寧可媒體查詢,這些確實存在:
要檢測的Internet Explorer 6-7(因此Windows XP和年長了到Windows 7 )這工作:
@media screen\9
{
.windows {
color:red;
}
}
這一個我創建的Internet Explorer 9,因爲沒有存在。 (所以贏得7或贏得Vista的)
@media screen and (min-width:0\0) and (min-resolution:.001dpcm)
{
.windows {
color:red;
}
}
這種檢測的Internet Explorer 8(因此與Windows XP的Windows 7)
@media \0screen
{
.windows {
color:red;
}
}
我製作的各種其他部分在過去一年中該媒體查詢,它處理Safari 6.1和更新。在發佈時,Safari是第7版。 Mac和移動設備,例如基Android瀏覽器,將在這種方式來檢測:
@media screen and (-webkit-min-device-pixel-ratio:0) and (min-color-index:0)
{
.mac_or_mobile {(;
color:red;
);}
}
這裏是一個更好的方法,以檢測大多數較新的MAC和NOT包括移動設備,例如ipad公司(或機器人會) - 這又是對使用Safari 6.1及更高版本,所以它是一臺Mac ...:
@media screen and (-webkit-max-device-pixel-ratio:1) and (min-color-index:0)
{
.mac_osx {(;
color:red;
);}
}
如果你想在手機代替,這個工程的近期高清晰的:
@media screen and (-webkit-min-device-pixel-ratio:1.1) and (min-color-index:0)
{
.retina {(;
color:red;
);}
}
我有更多關於 手機和平板電腦在這裏:https://jeffclayton.wordpress.com/2014/07/22/css-for-hi-def-mobile-retina-devices/和這裏:https://jeffclayton.wordpress.com/2014/07/28/css-hack-anything-but-ios/
Mac確實有一段時間的Internet Explorer,但沒有更新的版本後5.5版本。
Windows確實也有一段時間的Safari,但在版本5之後沒有製作更新的版本。大多數Windows用戶從不使用Safari,所以當檢測到Safari時通常可以排除Windows。
如果包括上述所有的樣式合併到一個文件中,下面的div將反映的風格,結果從上面:根據檢測
Per Firefox and Internet Explorer/Edge:
<div class="windows"> If this is Windows this is red </div>
<div class="winxp"> If this is Windows XP or older this is red </div>
<div class="win10"> If this is Windows 10 this is red </div>
Per Firefox:
<div class="vista"> If this is Windows Vista this is red </div>
<div class="win7"> If this is Windows 7 this is red </div>
<div class="win8"> If this is Windows 8 this is red </div>
<div class="nonwindows"> If this is NOT Windows this is red </div>
<div class="nonmac"> If this is NOT Mac OS X this is red </div>
<div class="mac"> If this is Mac OS X this is red </div>
<div class="mac10_7up"> If this is Mac OS X 10.7 or newer this is red </div>
<div class="mac10_6up"> If this is Mac OS X 10.6 or newer this is red </div>
<div class="mac10_6"> If this is Mac OS X 10.6 only this is red </div>
<div class="lindroid"> If this is Linux or Android this is red </div>
Per Safari:
<div class="mac_osx"> If this is a Mac using Safari
(desktop or laptop computer) this is red (includes old mobile devices but not before iOS 6) </div>
<div class="mac_or_mobile"> If this is a Mac or a mobile device using Safari
(Android or iPad/iPhone) this is red </div>
<div class="retina"> If this is a hi-def mobile device using Safari
(Android or iPad/iPhone) this is red </div>
其他注意事項...
Windows版本在Internet Explorer/Edge上(爲了便於參考):
As stated above if you detect Internet Explorer 6, you are using XP (or older Win)
If you detect Internet Explorer 7-8, you are using Windows XP, Vista, or Windows 7
If you detect Internet Explorer 9 you are using Windows Vista or Windows 7
If you detect Internet Explorer 10 you are using Windows 7 or Windows 8.0
If you detect Internet Explorer 11 you are using Windows 7 or Windows 8.0/8.1 or Windows 10
If you detect Microsoft Edge you are using Windows 10
Windows 10是本貼發佈時的Windows的當前版本。
對於歷史的興趣,如果你真的想,你可以實際確定Internet Explorer 5或更低的Mac上使用舊的黑客,我發現:
/*\*//*/ .mac_internet_explorer { color:red; } /**/
的Internet Explorer
Mac版本是唯一可用在舊的PowerPC Mac上,不是英特爾的型號。
看到我的其他帖子,看看我的CSS特定問題:http://stackoverflow.com/questions/15175577/css-html-compatibility-issues-across-the-same-browser – DextrousDave 2013-03-03 20:16:21
可能會有用:http:// dowebsitesneedtolookexactlythesameineverybrowser.com/ – 2013-03-03 20:20:41
如果您提供了您遇到的實際HTML/CSS問題,這將有所幫助,我確定有解決方案。我認爲你的方法(針對操作系統)是根本錯誤的。 – 2013-03-03 20:21:16