2013-03-03 73 views
2

我想知道是否有与CSS定位平台的方式,即在不同的运行操作系统 - Windows 7,Windows 8中,Linux的等等CSS平台特定的黑客

我有一个样式表,用一个ul列表和一个边框底部设置,当鼠标悬停在菜单元素上时会改变颜色。问题是,在所有浏览器(Chrome浏览器,FF IE等)中,li项目在Windows 7和Windows8/Ubuntu之间不会显示相同的内容...)我尝试使用不同的css特性,例如行高,填充,边距等,但无论我做什么,win7和win8/ubuntu之间的项目布局都不一样。我测试了各种PC和我在相同版本的浏览器之间测试(最新的Chrome,最新的FF,IE9 ...)

因此,我可以针对说Windows 8与CSS的黑客?或者只是Ubuntu/Linux?

+0

看到我的其他帖子,看看我的CSS特定问题:http://stackoverflow.com/questions/15175577/css-html-compatibility-issues-across-the-same-browser – DextrousDave 2013-03-03 20:16:21

+0

可能会有用:http:// dowebsitesneedtolookexactlythesameineverybrowser.com/ – 2013-03-03 20:20:41

+0

如果您提供了您遇到的实际HTML/CSS问题,这将有所帮助,我确定有解决方案。我认为你的方法(针对操作系统)是根本错误的。 – 2013-03-03 20:21:16

回答

8

有一种方法由浏览器的性质做一些这方面你正在使用,有点演绎推理,还有一些技巧......并非所有事情都会这样做,但只有少数人可以用于使用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上,不是英特尔的型号。

+0

这是相当了不起的 – henry 2015-01-29 17:26:41

+0

哇,很好的答案。你似乎对CSS黑客有激情:) – shesek 2015-02-05 05:38:18