针对IE7的CSS Hack

2006.04.2920:44

IE7 修复了很多 bug,也增加了对一些选择符的支持,所以现在诸如 *html {}html>body {} 等针对 IE 隐藏或显示的 hack 都会在 IE7 中失效。虽然 CSS Hack 不推荐使用,条件注释才是万无一失的过滤器,但是条件注释只能出现在 HTML 中,CSS Hack 还是有用武之地的。Nanobot 发现了一些针对 IE7 的 CSS Hack,具体就是:

>body
html*
*+html

这三种写法,其中前两种都是不合法的 CSS 写法,在标准兼容浏览器中被被忽略,但是 IE7 却不这么认为。对于 >body ,它会将缺失的选择符用全局选择符 * 代替,也就是将其处理成了 *>body,而且不光对于 > 选择符,+,~ 选择符中这个现象也存在。对于 html* ,由于 html 和 * 之间没有空格,所以也是一种 CSS 语法错误,但 IE7 不会忽略,而是错误地认为这里有一个空格。对于第三种 *+html,IE7 认为 html 前面的 DTD 声明也是一个元素,所以 html 会被选中,这三种方法中只有这一种方法是合法的 CSS 写法,也就是说可以通过校验器的验证,因此也是作者推荐的 hack 用法。

最后作者给出了最佳方式:

IE 6 and below
Use * html {} to select the html element.
IE 7 and below
Use *+html, * html {} to select the html element.
IE 7 only
Use *+html {} to select the html element.
IE 7 and modern browsers only
Use html>body {} to select the body element.
Modern browsers only (not IE 7)
Use html>/**/body {} to select the body element.

具体信息参考原文:Easy CSS hacks for IE7

3 Comments Comment RSS »

The URI to TrackBack this entry is: http://old9.blogsome.com/2006/04/29/css-hack-for-ie7/trackback/

  1. 真实晕头转向了已经

    Comment by 悬翎 — 2006.4.30 @ 08:58

  2. Any kinds of hacking is temporary solution. M$ will correct em sooner or later. So forget hacking, change your design.

    Comment by dorian — 2006.5.08 @ 14:22

  3. IE7,你准备好了没?

    您可以把本篇当做是与《臭虫为友——浏览器补救办法,臭虫以及解决方案 》三 系 列的第四系列。
    其实,对于专职于web标准的人来说,我们在IE7 Beta1的时候就准备好了,XD。不信可以看看针…

    Trackback by Realazy — 2006.11.07 @ 15:40

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


为防评论机器人,劳驾您输入图片中的字符 :)
captcha