最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if user is in high contrast mode via JavaScript or CSS - Stack Overflow

programmeradmin2浏览0评论

When pressing Shift+Left+Alt+Print Windows switches into high contrast mode - is there any chance to detect that on a web page (using JavaScript or CSS)?

Is there any chance to detect that in the HTTP-Request (a.k.a the server-side e.g. via PHP or Ruby)?

When pressing Shift+Left+Alt+Print Windows switches into high contrast mode - is there any chance to detect that on a web page (using JavaScript or CSS)?

Is there any chance to detect that in the HTTP-Request (a.k.a the server-side e.g. via PHP or Ruby)?

Share Improve this question edited Jul 30, 2015 at 5:45 jezrael 863k102 gold badges1.4k silver badges1.3k bronze badges asked Dec 17, 2009 at 11:12 pagidpagid 13.9k13 gold badges81 silver badges107 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

According to this article about using CSS sprites in high contrast, in high contrast mode on Windows, background images should be set to "none" and it also changes the background color. This should override any CSS stylesheet. So you can perform some javascript to detect it after initial rendering. Check his demo page (the "FYI [Not] in high contrast mode" text).

I have Mac (FYI switch using Cmd + Alt + Ctrl + 8) and his technique doesn't work for me, but he says it works on Windows.

If it works, you can either use some JavaScript to simply change your CSS or set a (session) cookie and reload the page to pass it to the server and perform server-side actions.

The following works for me on Win8 with (the desktop-)IE:

<style type="text/css">
// ...
@media screen and (-ms-high-contrast: active) {
   /* any rules may come here, for example: */
   .leftMenu a:hover { text-decoration: underline; }
}
// ...
</style>

I think it must work with Windows Store Apps as well. This is not a complete solution, but maybe useful a bit.

MSDN doc: @media, -ms-high-contrast. The High-contrast mode description is also worth mentioning.

Use @media (prefers-contrast: more):

.contrast {
  width: 100px;
  height: 100px;
  outline: 2px dashed black;
}

@media (prefers-contrast: more) {
  .contrast {
    outline: 2px solid black;
  }
}

It's supported in all major evergreen browsers.

Warning: -ms-high-contrast only works in Microsoft browsers.

If you are implementing high contrast in your web application then use following code block for detecting black-on-white and white-on-black contrast selection. This will work fine in IE.

@media screen and (-ms-high-contrast: black-on-white) { /* Put your styling code............. */ }

@media screen and (-ms-high-contrast: white-on-black) { /* Put your styling code............. */ }

发布评论

评论列表(0)

  1. 暂无评论