---
title: "!important在ie7.0的hack方法 "
author: "dhj"
date: "2009-02-24T10:47:58Z"
tags: []
slug: "important-ie7-0-hack"
draft: false
lastmod: "2009-02-24T10:47:58Z"
id: 337785
url: "https://dang.iteye.com/blog/337785"
---

**英文原文地址:** [http://www.ibloomstudios.com/article7/](http://www.ibloomstudios.com/article7/)

由于ie对!important识别存在bug,而现在大部分网页标准设计师又通过这个bug来兼容ie和ff,但是ie7.0把这个bug给修复 了,所以问题又出现了,怎么兼容ie.7.0的同时又能兼容ie6.0和ff?正所谓"上有政策,下有对策",国外的网页标准设计师通过使用css filter的办法(并不是css hack)来兼容ie7.0,ie6.0和ff,以下为我从国外网站的翻译.

新建一个css样式如下：

#item {  
    width: 200px;  
    height: 200px;  
    background: red;  
}

新建一个div,并使用前面定义的css的样式：

&lt;div id="item"&gt;some text here&lt;/div&gt;

在body表现这里加入lang属性,中文为zh：

&lt;body lang="en"&gt;

现在对div元素再定义一个样式：

\*:lang(en) #item{  
    background:green !important;  
}

这样做是为了用!important覆盖原来的css样式,由于:lang选择器ie7.0并不支持,所以对这句话不会有任何作用,于是也达到了ie6.0下同样的效果,但是很不幸地的是,safari同样不支持此属性,所以需要加入以下css样式：

#item:empty {  
    background: green !important  
}

:empty选择器为css3的规范,尽管safari并不支持此规范,但是还是会选择此元素,不管是否此元素存在,现在绿色会现在在除ie各版本以外的浏览器上,并在以下浏览器和操作系统下通过测试：

ie7 beta 2 preview/win  
ie5.01+/win  
firefox 1.5/win  
opera 8.5/win & linux  
netscape 7.01, 8/win  
mozilla 1.7.12/win & linux  
safari 2/mac  
firefox 1.0.4/linux  
epiphany 1.4.8/linux  
galeon 1.3.20/linux

![](http://www.blueidea.com/articleimg/2006/06/3626/01.jpg)

![](http://www.blueidea.com/articleimg/2006/06/3626/02.jpg)

按照远作者的说法其实这不能算是一种hack,应该属于filter,不过这似乎并不是最重要的,因为通过这个办法,我们又一次了解决IE6.0,IE7.0和其他浏览器之间的兼容性问题,而且使用:lang-filter这办法,在今后的一段时间内都会有用