一些常用的css Hack

常用的css hack有三种:条件hack、属性hack、选择符hack

1、条件hack

语法:

<!--[if <keywords>? IE <version>?]>
HTML代码块
<![endif]-->

取值:

<keywords>

if条件共包含6种选择方式:是否、大于、大于或等于、小于、小于或等于、非指定版本

是否:指定是否IE或IE某个版本。关键字:
大于:选择大于指定版本的IE版本。关键字: gt(greater than)
大于或等于:选择大于或等于指定版本的IE版本。关键字: gte(greater than or equal)
小于:选择小于指定版本的IE版本。关键字: lt(less than)
小于或等于:选择小于或等于指定版本的IE版本。关键字: lte(less than or equal)
非指定版本:选择除指定版本外的所有IE版本。关键字: !
如不想在非IE中看到某区域,可这样写:
<!--[if IE]>
<p>你在非IE中将看不到我的身影</p>
<![endif]-->

if条件6种选择方式的使用示例

是否,示例代码:

<!--[if IE]>
<style>
.test{color:red;}
</style>
<![endif]-->

大于,示例代码:

<!--[if gt IE 6]>
<style>
.test{color:red;}
</style>
<![endif]-->

大于或等于,示例代码:

<!--[if gte IE 6]>
<style>
.test{color:red;}
</style>
<![endif]-->

小于,示例代码:

<!--[if lt IE 7]>
<style>
.test{color:red;}
</style>
<![endif]-->

小于或等于,示例代码:

<!--[if lte IE 7]>
<style>
.test{color:red;}
</style>
<![endif]-->

非指定版本,示例代码:

<!--[if ! IE 7]>
<style>
.test{color:red;}
</style>
<![endif]-->

2、属性hack

语法:selector{<hack>?property:value<hack>?;}

取值:

_:选择IE6及以下。连接线(中划线)(-)亦可使用,为了避免与某些带中划线的属性混淆,所以使用下划线(_)更为合适。

*:选择IE7及以下。诸如:(+)与(#)之类的均可使用,不过业界对(*)的认知度更高

\9:选择IE6+

\0:选择IE8+和Opera

[;property:value;];:选择webkit核心浏览器(Chrome,Safari)。IE7及以下也能识别。中括号内外的3个分号必须保留,第一个分号前可以是任意规则或任意多个规则[;color:#f00;]; 与 [color:#f00;color:#f00;]; 与 [margin:0;padding:0;color:#f00;]; 是等价的。生效的始终是中括号内的最后一条规则,所以通常选用第一种写法最为简洁。

说明:

选择不同的浏览器及版本

  • 尽可能减少对CSS Hack的使用。Hack有风险,使用需谨慎
  • 通常如未作特别说明,本文档所有的代码和示例的默认运行环境都为标准模式。
  • 一些CSS Hack由于浏览器存在交叉认识,所以需要通过层层覆盖的方式来实现对不同浏览器进行Hack的。如下面这个例子:

.test{
    color:#090\9; /* For IE8+ */
    *color:#f00;  /* For IE7 and earlier */
    _color:#ff0;  /* For IE6 and earlier */
}

3、选择符hack

语法:<hack> selector{ sRules }

说明:

选择不同的浏览器及版本

  • 尽可能减少对CSS Hack的使用。Hack有风险,使用需谨慎
  • 通常如未作特别说明,本文档所有的代码和示例的默认运行环境都为标准模式。
  • 一些CSS Hack由于浏览器存在交叉认识,所以需要通过层层覆盖的方式来实现对不同浏览器进行Hack的。
* html .test{color:#090;}       /* For IE6 and earlier */
* + html .test{color:#ff0;}     /* For IE7 */
.test:lang(zh-cn){color:#f00;}  /* For IE8+ and not IE */
.test:nth-child(1){color:#0ff;} /* For IE9+ and not IE */

猜你喜欢

转载自www.cnblogs.com/chao202426/p/12651777.html