CSS技巧(二):CSS hack
2013-10-24 11:26
302 查看
什么是CSS hack
CSS hack由于不同的浏览器,比如IE6,IE7,Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。 这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果。
CSS hack分类
hack主要分为CSS选择器hack、CSS属性hack、IE条件注释hack。
CSS选择器hack:比如 IE6能识别*html .class{},IE7能识别*+html .class{}或者*:first-child+html .class{}等。
CSS属性hack:比如 IE6能识别下划线"_"和星号" * ",IE7能识别星号" * ",但不能识别下划线"_",而firefox两个都不能认识等。
IE条件注释hack:
针对所有IE:<!--[if IE]><!--您的代码--><![endif]-->
针对IE6及以下版本:<!--[if lt IE 7]><!--您的代码--><![endif]-->
这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效。
书写顺序,一般是将识别能力强的浏览器的CSS写在前面。
用法
比如要分辨IE6和firefox两种浏览器,可以这样写:
可以看到在IE6中看到是红色的,在firefox中看到是绿色的。
在firefox中,它是识别不了后面的那个带星号的东西是什么的,于是将它过滤掉,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。
在IE6中,它两个background都能识别出来,它解析得到的结果是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div的背景颜色就是红色的了。
浏览器识别
上面这段代码大家可以直接copy出来,保存成html在各浏览器试试。分析:
(1)background-color:blue; 各个浏览器都认识,这里给firefox用;
(2)background-color:red\9; \9所有的ie浏览器可识别;
(3)background-color:yellow\0; \0 是留给ie8的,但笔者测试,发现最新版opera也认识,汗。。。不过且慢,后面自有hack写了给opera认的,所以,\0我们就认为是给ie8留的;
(4)+background-color:pink; + ie7定了;
(5)_background-color:orange; _专门留给神奇的ie6;
(6):root #test { background-color:purple\9; } :root是给ie9的,网上流传了个版本是 :root #test { background-color:purple\0;},新版opera也认识,所以经笔者反复验证最终ie9特有的为:root 选择符 {属性\9;}
(7)@media all and (min-width:0px){ #test {background-color:black\0;} } 这个是老是跟ie抢着认\0的神奇的opera,必须加个\0,不然firefox,chrome,safari也都认识。。。
(8)@media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} }最后这个是浏览器新贵chrome和safari的。
作者: ForEvErNoME
出处: http://www.cnblogs.com/ForEvErNoME/
欢迎转载或分享,但请务必声明文章出处。如果文章对您有帮助,希望你能 推荐 或 关注
CSS hack由于不同的浏览器,比如IE6,IE7,Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。 这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果。
CSS hack分类
hack主要分为CSS选择器hack、CSS属性hack、IE条件注释hack。
CSS选择器hack:比如 IE6能识别*html .class{},IE7能识别*+html .class{}或者*:first-child+html .class{}等。
CSS属性hack:比如 IE6能识别下划线"_"和星号" * ",IE7能识别星号" * ",但不能识别下划线"_",而firefox两个都不能认识等。
IE条件注释hack:
针对所有IE:<!--[if IE]><!--您的代码--><![endif]-->
针对IE6及以下版本:<!--[if lt IE 7]><!--您的代码--><![endif]-->
这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效。
书写顺序,一般是将识别能力强的浏览器的CSS写在前面。
用法
比如要分辨IE6和firefox两种浏览器,可以这样写:
div{ background:green; /* for firefox */ *background:red; /* for IE6 */ (both IE6 && IE7) }
可以看到在IE6中看到是红色的,在firefox中看到是绿色的。
在firefox中,它是识别不了后面的那个带星号的东西是什么的,于是将它过滤掉,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。
在IE6中,它两个background都能识别出来,它解析得到的结果是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div的背景颜色就是红色的了。
浏览器识别
<!DOCTYPE html> <html> <head> <title>Css Hack</title> <style> #test { width:300px; height:300px; background-color:blue; /*firefox*/ background-color:red\9; /*all ie*/ background-color:yellow\0; /*ie8*/ +background-color:pink; /*ie7*/ _background-color:orange; /*ie6*/ } :root #test { background-color:purple\9; } /*ie9*/ @media all and (min-width:0px){ #test {background-color:black\0;} } /*opera*/ @media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} } /*chrome and safari*/ </style> </head> <body> <div id="test">test</div> </body> </html>
上面这段代码大家可以直接copy出来,保存成html在各浏览器试试。分析:
(1)background-color:blue; 各个浏览器都认识,这里给firefox用;
(2)background-color:red\9; \9所有的ie浏览器可识别;
(3)background-color:yellow\0; \0 是留给ie8的,但笔者测试,发现最新版opera也认识,汗。。。不过且慢,后面自有hack写了给opera认的,所以,\0我们就认为是给ie8留的;
(4)+background-color:pink; + ie7定了;
(5)_background-color:orange; _专门留给神奇的ie6;
(6):root #test { background-color:purple\9; } :root是给ie9的,网上流传了个版本是 :root #test { background-color:purple\0;},新版opera也认识,所以经笔者反复验证最终ie9特有的为:root 选择符 {属性\9;}
(7)@media all and (min-width:0px){ #test {background-color:black\0;} } 这个是老是跟ie抢着认\0的神奇的opera,必须加个\0,不然firefox,chrome,safari也都认识。。。
(8)@media screen and (-webkit-min-device-pixel-ratio:0){ #test {background-color:gray;} }最后这个是浏览器新贵chrome和safari的。
浏览器 | CSS hack |
---|---|
IE6 | _background-color:#38DB24; |
IE67 | *background-color:#38DB24 ; |
IE67 | +background-color:#38DB24 ; |
IE67 | #background-color:#38DB24; |
IE67 | background-color:#38DB24 !ie; |
IE678910 | background-color:#38DB24\9; |
IE78910&Firefox&Opera&Chrome&Safari | html>body .ie78910-all-hack { background-color: #38DB24 } |
IE8910&Firefox&Opera&Chrome&Safari | html>/**/body .ie8910-all-hack { background-color: #38DB24 } |
IE8910&Opera | background-color:#38DB24\0; |
IE910 | :root .ie910-hack { background-color:#38DB24\9; } |
IE910 | background-color:#38DB24\9\0; |
IE910&Firefox&Opera&Chrome&Safari | body:nth-of-type(1) .ie910-all-hack {background-color:#38DB24 ;} |
IE910&Firefox&Opera&Chrome&Safari | @media all and (min-width: 0px) { .ie910-all-2-hack{ background-color:#38DB24 ;} } |
IE910&Firefox&Opera&Chrome&Safari | @media all and (min-width: 0px) { .ie910-all-3-hack{background-color:#38DB24 ;} } |
IE910&Firefox&Opera&Chrome&Safari | :root *> .ie910-all-4-hack { background-color:#38DB24 } |
IE10 | @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { .ie10-hack{background-color:#38DB24 ;} } |
Firefox | @-moz-document url-prefix() { .firefox-hack{background-color:#38DB24 ;} } |
Chrome&Safari | @media screen and (-webkit-min-device-pixel-ratio:0) {.chrome-safari-hack{background-color:#38DB24 ;} } |
出处: http://www.cnblogs.com/ForEvErNoME/
欢迎转载或分享,但请务必声明文章出处。如果文章对您有帮助,希望你能 推荐 或 关注
相关文章推荐
- IE10的CSS技巧(IE10专属css hack写法)
- IE10的CSS技巧(IE10专属css hack写法)
- 关于CSS Hack与float闭合的CSS技巧 清除浮动代码
- css:常用CSS书写技巧和CSS HACK技巧
- CSS技巧(二):CSS hack
- CSS技巧(二):CSS hack
- css ie6 ie7 ff的CSS hack使用技巧
- 常用CSS书写技巧和CSS HACK技巧
- CSS常用技巧之CSS书写技巧和CSS HACK技巧
- 您未必知道的Css技巧
- css技巧之如何实现ul li边框重合
- CSS高级技巧
- 详解CSS透明相关技巧 (转载)
- div+csS中的一些技巧和浏览器兼容的办法
- css技巧总结(未完待续)
- css基础 一个class属性给两个名字(实用技巧)
- CSS技巧:改变选择网页文字的背景色
- CSS布局技巧之-宽度自适应
- CSS技巧:清除浮动