怎样处理IE6/IE7不鉴别display:inline

2021-03-09 12:40 jianzhan
ie6,ie7的haslayout特性是个令人头疼的难题。在做导航栏条的情况下,1般会用到ul li构造,大多数数情况下大家是把li设定为波动,让其并排显示信息在同1行。也有1种方式便是设定li为display:inline;这样能够做到一样的实际效果,可是难题是inline元素的特点:默认设置没法设定宽度,高宽比,和左右margin,(有关padding,状况有点独特,在ie6,7中 inline元素是没法设定左右padding的,可是在规范访问器里边是能够设定左右padding的)。

鉴于inline元素的这类特点,假如大家不波动而且想让li显示信息在1行,并且能够设定高宽比,宽度和左右margin,左右padding等特性,应当如何办呢?你1定会想起1个特性display:inline-block;对!”inline- block”便是干这个事的,让1个元素既不换行又具备block元素的特点。但是有点小难题.

在IE6、IE7中不鉴别display:inline-block特性,加不加display:inline-block;针对它们彻底沒有任何危害。那末让大家来想方法处理这个难题,这就涉及到到ie6,7中的haslayout特性了。ie6,7中的inline元素有个独特的状况,便是开启了ie的hasLayout特性之后就有着了layout。此时inline元素的主要表现和规范访问器里边的inline-block元素基础同样。

看下面这个事例,大家用ie的独享特性zoom来开启hasLayout,随后看看inline元素的主要表现。

拷贝编码
编码以下:

<html xmlns=”http://www.w3.org/1999/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf⑻″ />
<title>display-block</title>
<style>
span {
width:200px;
height:50px;
margin:10px;
padding:20px;
background:#ccc;
zoom:1;
</style>
</head>
<body>
<span><span style="font-family:宋体,微软雅黑,Verdana,Helvetica">span</span> </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"></body> </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"></html></span>

能够看到在ie6,7中inline元素span早已主要表现得和1个display:inline-block元素1摸1样了,可是在规范访问器中span依然是行内元素(宽高和左右margin都失效)。
假如申明了有误DTD,致使ie6在quirks 方式下分析,那末ie6会全自动开启inline元素的haslayout,但是这里只探讨一切正常状况下的分析,因此加了个zoom:1来开启haslayout;zoom的值设定为除auto外的任何值都会开启haslayout,之因此常常用zoom:1;是由于zoom这个特性自身是ie的放缩特性,设定为别的值会致使元素在ie下形变,设定为1既是维持原型不放缩。
掌握了上面的状况,大家便可以来处理以前那个难题了。能够改本来的css编码以下:

拷贝编码
编码以下:

li <span style="font-family:宋体,微软雅黑,Verdana,Helvetica">{</span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">display:inline-block; /* firefox等规范访问器鉴别*/ </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">*display:inline; /* 仅有ie6和ie7鉴别*/zoom:1; /* 开启ie6和ie7下的haslayout */ </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">width:80px; </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">height:20px; </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">margin:10px; </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">padding:10px; </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">text-align:center; </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">background:#cfc; </span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica">}</span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"> 让规范访问器鉴别display:inline-block;让ie6,7鉴别display:inline;来遮盖上面的display:inline-block;(我为何要说”遮盖”?)。随后根据zoom:1;来开启haslayout让inline元素在ie中主要表现得和inline-block元素1样。</span>
</span>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"><html xmlns=”http://www.w3.org/1999/xhtml”> </pre><pre name="code" class="html"><head> </pre><pre name="code" class="html"><meta http-equiv=”Content-Type” content=”text/html; charset=utf⑻″ /> </pre><pre name="code" class="html"><title>display-block</title> </pre><pre name="code" class="html"><style> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">ul {</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">background:#ccc;</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">padding:0;</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">margin:0;</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">list-style:none;</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">} </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">li {</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">display:inline-block; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">*display:inline; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">zoom:1; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">width:80px; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">height:20px; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">margin:10px; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">padding:10px; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">text-align:center; </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html">background:#cfc;</pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"> } </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"></style> </pre><pre name="code" class="html"></head> </pre><pre name="code" class="html"><body> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"><ul> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"><li>检测</li> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"><li>检测</li> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"><li>检测</li> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"><li>检测</li> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"></ul> </pre>
<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px"></span></span><pre name="code" class="html"></body> </pre><pre name="code" class="html"></html></pre><span style="color:rgb(17,17,17); font-family:宋体,微软雅黑,Verdana,Helvetica; font-size:14px; line-height:25px">能够看到,如今在各访问器里边的显示信息早已1致了。li元素都显示信息在同1行。</span>

<span style="font-family:宋体,微软雅黑,Verdana,Helvetica"><a target="_blank" href="http://www.poluoluo.com/jzxy/201206/167493.html">http://www.poluoluo.com/jzxy/201206/167493.html</a>
</span>