【必备】史上最全的浏览器 CSS & JS Hack 手册 【必备】史上最全的浏览器 CSS & JS Hack 手册

  浏览器渲染页面的方式各不相同,甚至同一浏览器的不同版本(“杰出代表”是 IE)也有差异。因此,浏览器兼容成为前端开发人员的必备技能。如果有一份浏览器 Hack 手册,那查询起来就方便多了。这篇文章就向大家分享 Browserhacks 帮我们从网络上收集的各个浏览器特定的 CSS & JavaScript Hack,记得推荐和分享啊!

IE 选择器 Hack

1

2

3

/* IE 6 and below */

* html .selector {}

.suckyie6.selector {}/* .suckyie6 can be any unused class */

1

2

/* IE 7 and below */

.selector, {}

1

2

3

4

/* IE 7 */

*:first-child+html .selector {}

.selector, x:-IE7{}

*+html .selector {}

/* Everything but IE 6 */

html > body .selector {}

1

2

3

/* Everything but IE 6/7 */

html >/**/body .selector {}

head ~/* */body .selector {}

1

2

3

4

5

/* Everything but IE 6/7/8 */

:root *> .selector {}

body:last-child .selector {}

body:nth-of-type(1) .selector {}

body:first-of-type .selector {}

IE 属性/值 Hack

1

2

3

/* IE 6 */

.selector {_color:blue; }

.selector { -color:blue; }

1

2

3

/* IE 6/7 - acts as an !important */

.selector {color:blue!ie; }

/* string after ! can be anything */

1

2

3

4

5

6

7

/* IE 6/7 - any combination of these characters:

! $ & * ( ) = % + @ , . / ` [ ] # ~ ? : < > | */

.selector { !color:blue; }

.selector { $color:blue; }

.selector { &color:blue; }

.selector { *color:blue; }

/* ... */

1

2

3

/* IE 8/9 */

.selector {color:blue\0/; }

/* must go at the END of all rules */

1

2

/* IE 9/10 */

.selector:nth-of-type(1n) {color:blue\9; }

1

2

3

/* IE 6/7/8/9/10 */

.selector {color:blue\9; }

.selector { color/*\**/:blue\9; }

1

2

/* Everything but IE 6 */

.selector { color/**/:blue; }

IE Media Query Hack

1

2

/* IE 6/7 */

@mediascreen\9{}

1

2

/* IE 8 */

@media \0screen {}

1

2

/* IE 9/10, Firefox 3.5+, Opera */

@mediascreenand (min-resolution: +72dpi) {}

1

2

/* IE 10+ */

@mediascreenand (-ms-high-contrast: active), (-ms-high-contrast:none) {}

1

2

/* IE 6/7/8 */

@media \0screen\,screen\9{}

1

2

/* IE 8/9/10 & Opera */

@mediascreen\0{}

1

2

/* IE 9/10 */

@mediascreenand (min-width:0\0) {}

1

2

/* Everything but IE 6/7/8 */

@mediascreenand (min-width:400px) {}

IE JavaScript Hack

1

2

3

/* IE 6 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 6]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

1

2

3

4

/* IE 7 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 7]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

navigator.appVersion.indexOf("MSIE 7.")!=-1

1

2

/* IE <= 8 */

varisIE ='\v'=='v';

1

2

3

/* IE 8 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 8]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

1

2

3

/* IE 9 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 9]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

1

2

/* IE 10 */

varisIE = eval("/*@cc_on!@*/false") && document.documentMode === 10;

1

2

/* IE 10 */

varisIE = document.body.style.msTouchAction != undefined;

Firefox 浏览器

  选择器 Hack

1

2

/* Firefox 1.5 */

body:empty .selector {}

1

2

/* Firefox 2+ */

.selector, x:-moz-any-link {}

1

2

/* Firefox 3+ */

.selector, x:-moz-any-link; x:default{}

1

2

/* Firefox 3.5+ */

body:not(:-moz-handler-blocked) .selector {}

  媒体查询 Hack

1

2

/* Firefox 3.5+, IE 9/10, Opera */

@mediascreenand (min-resolution: +72dpi) {}

1

2

/* Firefox 3.6+ */

@mediascreenand (-moz-images-in-menus:0) {}

1

2

/* Firefox 4+ */

@mediascreenand (min--moz-device-pixel-ratio:0) {}

  JavaScript Hack

1

2

/* Firefox */

varisFF = !!navigator.userAgent.match(/firefox/i);

1

2

/* Firefox 2 - 13 */

varisFF = Boolean(window.globalStorage);

1

2

/* Firefox 2/3 */

varisFF = /a/[-1]=='a';

1

2

/* Firefox 3 */

varisFF = (functionx(){})[-5]=='x';

Chrome 浏览器

  选择器 Hack

1

2

/* Chrome 24- and Safari 5- */

::made-up-pseudo-element, .selector {}

  

  媒体查询 Hack

1

2

/* Chrome, Safari 3+ */

@mediascreenand (-webkit-min-device-pixel-ratio:0) {}

  

  JavaScript Hack

1

2

/* Chrome */

varisChrome = Boolean(window.chrome);

  

Safari 浏览器

  选择器 Hack

1

2

3

/* Safari 2/3 */

html[xmlns*=""] body:last-child .selector {}

html[xmlns*=""]:root .selector {}

1

2

/* Safari 2/3.1, Opera 9.25 */

*|html[xmlns*=""] .selector {}

1

2

/* Safari 5- and Chrome 24- */

::made-up-pseudo-element, .selector {}

  

  媒体查询 Hack

1

2

/* Safari 3+, Chrome */

@mediascreenand (-webkit-min-device-pixel-ratio:0) {}

  

  JavaScript Hack

1

2

/* Safari */

varisSafari = /a/.__proto__=='//';

  

Opera 浏览器

  选择器 Hack

1

2

/* Opera 9.25, Safari 2/3.1 */

*|html[xmlns*=""] .selector {}

1

2

/* Opera 9.27 and below, Safari 2 */

html:first-child .selector {}

1

2

/* Opera 9.5+ */

noindex:-o-prefocus, .selector {}

  

  媒体查询 Hack

1

2

/* Opera 7 */

@mediaalland (min-width:0px){}

1

2

/* Opera 12- */

@mediaalland (-webkit-min-device-pixel-ratio:10000), notalland (-webkit-min-device-pixel-ratio:0) {}

1

2

/* Opera, Firefox 3.5+, IE 9/10 */

@mediascreenand (min-resolution: +72dpi) {}

1

2

/* Opera, IE 8/9/10 */

@mediascreen{}

  

  JavaScript Hack

1

2

/* Opera 9.64- */

varisOpera = /^function\(/.test([].sort);

1

2

/* Opera 12- */

varisOpera = Boolean(window.opera);

  

  浏览器渲染页面的方式各不相同,甚至同一浏览器的不同版本(“杰出代表”是 IE)也有差异。因此,浏览器兼容成为前端开发人员的必备技能。如果有一份浏览器 Hack 手册,那查询起来就方便多了。这篇文章就向大家分享 Browserhacks 帮我们从网络上收集的各个浏览器特定的 CSS & JavaScript Hack,记得推荐和分享啊!

IE 选择器 Hack

1

2

3

/* IE 6 and below */

* html .selector {}

.suckyie6.selector {}/* .suckyie6 can be any unused class */

1

2

/* IE 7 and below */

.selector, {}

1

2

3

4

/* IE 7 */

*:first-child+html .selector {}

.selector, x:-IE7{}

*+html .selector {}

/* Everything but IE 6 */

html > body .selector {}

1

2

3

/* Everything but IE 6/7 */

html >/**/body .selector {}

head ~/* */body .selector {}

1

2

3

4

5

/* Everything but IE 6/7/8 */

:root *> .selector {}

body:last-child .selector {}

body:nth-of-type(1) .selector {}

body:first-of-type .selector {}

IE 属性/值 Hack

1

2

3

/* IE 6 */

.selector {_color:blue; }

.selector { -color:blue; }

1

2

3

/* IE 6/7 - acts as an !important */

.selector {color:blue!ie; }

/* string after ! can be anything */

1

2

3

4

5

6

7

/* IE 6/7 - any combination of these characters:

! $ & * ( ) = % + @ , . / ` [ ] # ~ ? : < > | */

.selector { !color:blue; }

.selector { $color:blue; }

.selector { &color:blue; }

.selector { *color:blue; }

/* ... */

1

2

3

/* IE 8/9 */

.selector {color:blue\0/; }

/* must go at the END of all rules */

1

2

/* IE 9/10 */

.selector:nth-of-type(1n) {color:blue\9; }

1

2

3

/* IE 6/7/8/9/10 */

.selector {color:blue\9; }

.selector { color/*\**/:blue\9; }

1

2

/* Everything but IE 6 */

.selector { color/**/:blue; }

IE Media Query Hack

1

2

/* IE 6/7 */

@mediascreen\9{}

1

2

/* IE 8 */

@media \0screen {}

1

2

/* IE 9/10, Firefox 3.5+, Opera */

@mediascreenand (min-resolution: +72dpi) {}

1

2

/* IE 10+ */

@mediascreenand (-ms-high-contrast: active), (-ms-high-contrast:none) {}

1

2

/* IE 6/7/8 */

@media \0screen\,screen\9{}

1

2

/* IE 8/9/10 & Opera */

@mediascreen\0{}

1

2

/* IE 9/10 */

@mediascreenand (min-width:0\0) {}

1

2

/* Everything but IE 6/7/8 */

@mediascreenand (min-width:400px) {}

IE JavaScript Hack

1

2

3

/* IE 6 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 6]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

1

2

3

4

/* IE 7 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 7]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

navigator.appVersion.indexOf("MSIE 7.")!=-1

1

2

/* IE <= 8 */

varisIE ='\v'=='v';

1

2

3

/* IE 8 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 8]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

1

2

3

/* IE 9 */

(checkIE = document.createElement("b")).innerHTML ="<!--[if IE 9]><i></i><![endif]-->";

varisIE = checkIE.getElementsByTagName("i").length == 1;

1

2

/* IE 10 */

varisIE = eval("/*@cc_on!@*/false") && document.documentMode === 10;

1

2

/* IE 10 */

varisIE = document.body.style.msTouchAction != undefined;

Firefox 浏览器

  选择器 Hack

1

2

/* Firefox 1.5 */

body:empty .selector {}

1

2

/* Firefox 2+ */

.selector, x:-moz-any-link {}

1

2

/* Firefox 3+ */

.selector, x:-moz-any-link; x:default{}

1

2

/* Firefox 3.5+ */

body:not(:-moz-handler-blocked) .selector {}

  媒体查询 Hack

1

2

/* Firefox 3.5+, IE 9/10, Opera */

@mediascreenand (min-resolution: +72dpi) {}

1

2

/* Firefox 3.6+ */

@mediascreenand (-moz-images-in-menus:0) {}

1

2

/* Firefox 4+ */

@mediascreenand (min--moz-device-pixel-ratio:0) {}

  JavaScript Hack

1

2

/* Firefox */

varisFF = !!navigator.userAgent.match(/firefox/i);

1

2

/* Firefox 2 - 13 */

varisFF = Boolean(window.globalStorage);

1

2

/* Firefox 2/3 */

varisFF = /a/[-1]=='a';

1

2

/* Firefox 3 */

varisFF = (functionx(){})[-5]=='x';

Chrome 浏览器

  选择器 Hack

1

2

/* Chrome 24- and Safari 5- */

::made-up-pseudo-element, .selector {}

  

  媒体查询 Hack

1

2

/* Chrome, Safari 3+ */

@mediascreenand (-webkit-min-device-pixel-ratio:0) {}

  

  JavaScript Hack

1

2

/* Chrome */

varisChrome = Boolean(window.chrome);

  

Safari 浏览器

  选择器 Hack

1

2

3

/* Safari 2/3 */

html[xmlns*=""] body:last-child .selector {}

html[xmlns*=""]:root .selector {}

1

2

/* Safari 2/3.1, Opera 9.25 */

*|html[xmlns*=""] .selector {}

1

2

/* Safari 5- and Chrome 24- */

::made-up-pseudo-element, .selector {}

  

  媒体查询 Hack

1

2

/* Safari 3+, Chrome */

@mediascreenand (-webkit-min-device-pixel-ratio:0) {}

  

  JavaScript Hack

1

2

/* Safari */

varisSafari = /a/.__proto__=='//';

  

Opera 浏览器

  选择器 Hack

1

2

/* Opera 9.25, Safari 2/3.1 */

*|html[xmlns*=""] .selector {}

1

2

/* Opera 9.27 and below, Safari 2 */

html:first-child .selector {}

1

2

/* Opera 9.5+ */

noindex:-o-prefocus, .selector {}

  

  媒体查询 Hack

1

2

/* Opera 7 */

@mediaalland (min-width:0px){}

1

2

/* Opera 12- */

@mediaalland (-webkit-min-device-pixel-ratio:10000), notalland (-webkit-min-device-pixel-ratio:0) {}

1

2

/* Opera, Firefox 3.5+, IE 9/10 */

@mediascreenand (min-resolution: +72dpi) {}

1

2

/* Opera, IE 8/9/10 */

@mediascreen{}

  

  JavaScript Hack

1

2

/* Opera 9.64- */

varisOpera = /^function\(/.test([].sort);

1

2

/* Opera 12- */

varisOpera = Boolean(window.opera);