标题 | css兼容性问题 && css hack |
范文 | 以前做网页的时候,只考虑 ie6 和 ff 的兼容性,公司换了,要求也高了,ff和ie 6 7 8 要全兼容了, 碰到要单独hack ie8的。当然,用注释非常方便,只要添加相应的注释就可以解决。但问题是,为了一句css写多一个文件,或者在header上添加注释,那显然不是懒人的习惯做法。结论如下: selector{ property:value; /* 所有浏览器 */ property:value9; /* 所有ie浏览器 */ +property:value; /* ie7 */ _property 当然,注意顺序。根据css的优先性,上面的写法,分别针对firefox、ie8、ie7和ie6显示值。让我们看看这个: css代码如下: 代码如下: p.ie{ height:60px;text-align:center;line-height:60px;border:1px dashed #bbb;background:#f7f7f7;font:15; color:blue; // 所有浏览器 color:brown9; // 所有ie浏览器 +color:red; // ie7 _color:green; // ie6 } html 代碼: 代码如下: <body style=width:500px;margin:0 auto;> <p class=ie> <span style=display:block;display:none9;>嘿嘿,小子竟然也用firefox,蓝色文字。</span> <!--[if ie 8]>不错不错,挺先进的嘛,使用ie8呢!文字是褐色的。<![endif]--> <!--[if ie 7]>你,ie7,红色文字!<![endif]--> <!--[if ie 6]>孩子,虽然显示的是绿色文字,不过,ie6可不是好东西呢!<![endif]--> </p> </body> 注意下面介绍的这些hack写法仅适用于xhtml1.0。如果没有在html最前加上 <!doctype html public -//w3c//dtd xhtml 1.0 transitional//en xmlns=> 那么效果将不一样!此外,这里所说的ie8,不是指ie8的兼容模式,因为ie8的兼容模式其实就是ie7。 区别ie6、7与ff/ie8: background:blue;*background:orange; 引用 显示效果: ie 6/7:orange ff/ie8:blue 原理:ff/ie8不支持*开头,而ie6/7都支持。 区别ie6与ie7/ie8/ff: background:green;_background:blue; 引用 显示效果: ie7/8/ff:green ie6:blue 原理:ie6支持下划线_,ie7、8和firefox均不支持下划线。 区别ff/ie8和ie6/7: background:orange;+background:green;-background:blue; 或者 background:orange;*background:green!important;*background:blue; 引用 显示效果: ie6:blue ie7:green ff/ie8:orange 原理:ie6能识别-,ie7能识别+,ie8和ff都不能识别+和- ie8/ff都不识别*,ie7优先识别!important,ie6不能识别!important。 关于ie8的hacks: .test{ color:/***/#00f9; /* ie8 only */ color:#00f9; /* 适用于所有ie版本 */ } 可同时区分ie8、ie7、ie6、firefox的css hacks: .test{ color:#000; /* firefox */ color:/***/#00f9; /* ie8 */ *color:#f00; /* ie7 */ _color:#0f0; /* ie6 */ } 添加相应的注释解决兼容性问题 注释相应的css文件: <link rel=stylesheet type=text/css href=css/style.css media=screen /> <!--[if ie 6]> <link rel=stylesheet type=text/css href=css/ie6style.css media=screen /> <![endif]--> <!--[if ie 7]> <link rel=stylesheet type=text/css href=css/ie7style.css media=screen /> <![endif]--> <!--[if gte ie 8]> <link rel=stylesheet type=text/css href=css/ie8style.css media=screen /> <![endif]--> 注释相应的css 内容: <!--[if ie 6]> <style> <!-- #warp{ padding-bottom:11px;} --> </style> <![endif]--> <!--[if ie 7]> <style> <!-- #warp{ padding-bottom:11px;} --> </style> <![endif]--> <!--[if ie 8]> <style> <!-- #warp{ padding-bottom:11px;} --> </style> <![endif]--> |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。