`
caibinghong
  • 浏览: 143822 次
  • 性别: Icon_minigender_1
  • 来自: 福建
社区版块
存档分类
最新评论

标准模式中的 IE 6&7 width 100% bug

阅读更多

来自:http://www.99css.com/?p=43

在 web app 项目中经常遇到这个 bug,国外称之为100% ≠ 100% bug,又分为两种:

div 的宽度 100% ≠ 100% (IE 6&7)需求:

  1. 标准模式
  2. #container 局部滚动
  3. #asie 固定宽度
  4. #content 自适应宽度

再复杂一点还会要求两列等高,可参考 http://www.99css.com/?p=40

HTML

<div id="container"> <div id="wrapper"> <div id="content"> <h2>Content</h2> </div> </div> <div id="aside"> <h2>Aside</h2> </div> </div>


 

当然,别忘了 DTD 声明,因为这个只存在于标准模式

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

用之前介绍的HTML5 写法亦可:

<!DOCTYPE html>

CSS

/*简单重置*/ body, div, h2{margin:0;padding:0;} /*设置颜色,方便区分*/ #container{background:yellow;} #content{background:#FF8539;} #aside{background:#B9CAFF;} /*去除html默认滚动条*/ html{overflow-y:hidden;} /*关键布局代码*/ #container{height:300px;overflow:auto;} #wrapper{float:left;width:100%;margin-left:-200px;} #content{margin-left:200px;} #aside{float:right;width:200px;}


#content, #aside{height:200px;}时,即高度未超出#container时一切正常

[图片]

#content, #aside{height:400px;}时,出现纵向滚动条

正常显示效果如下:

[图片]

E 6&7 中 bug 出现:


原因:IE 6&7 滚动条的宽度未算在 100% 中,理想的状况是:#container 的宽度(100%) + #container 滚动条的宽度
= body 的 100%,W3C对此的定义:

In the case of a scrollbar being placed on an edge of the element’s box,
it should be inserted between the inner border edge and the outer padding
edge. Any space taken up by the scrollbars should be taken out of (subtracted
from the dimensions of) the containing block formed by the element with
the scrollbars.

Internet Explorer 100% Width Bug》中给出了思路:

element_selector { width: expression(this.parentNode.offsetHeight > this.parentNode.scrollHeight ? '100%' : parseInt(this.parentNode.offsetWidth - XX) + 'px'); }

其中 XX 是滚动条的宽度,在 Windows XP 主题下是 17px,Windows 经典主题下稍微小一点,在其他第三方系统主题下有可能是不确定宽度。

根据下图,简单改进一下即可

[图片]

IE6

[图片]

解决方法(原理同上)

body{_width:expression(this.parentNode.offsetHeight > this.parentNode.scrollHeight ? '100%' : parseInt(this.parentNode.clientWidth) + 'px');}

  1. 解决方法

    #wrapper{#width:expression(this.parentNode.offsetHeight > this.parentNode.scrollHeight ? '100%' : parseInt(this.parentNode.clientWidth) + 'px');}
    
    

    当然,写在 js 中亦可,不过要注意不要漏掉 window 的 riseze 事件,另外,window 的 resize 事件在 IE 中有执行多次的
    bug

  2. body 的宽度 100% ≠ 100% (仅 IE6)通常表现为 iframe 出现纵向滚动条时同时出现横向滚动条,简单粗暴的使用body{overflow-x:hidden;}是不负责任的,有时会截断要显示的内容第一个页面(父页面) <iframe frameborder="0" height="300" scrolling="auto" src="iframe.html" width="500">
    
    

    第二个页面(iframe)

    HTML

    <div></div>
    
    

    CSS

    body, div{margin:0;padding:0;} div{background-color:yellow;height:500px;}
    
    

    正常效果

    [图片]

分享到:
评论

相关推荐

    CSS设置元素width=100%失效的一种解决方式(该元素的父元素为body).pdf

    CSS设置元素width=100%失效的一种解决方式(该元素的父元素为body)

    判断客服端媒体设备

    45%&quot;; }else if getPgjs &quot;ipad&quot; { alert &quot;平板&quot; ul parentNode style width &quot;90%&quot;; }else if getPgjs &quot;iphone&quot; { alert &quot;手机&quot; ul parentNode ...

    海贼王ONEPIECE风格for leadbbs3.14

    100%&quot;&gt;&lt;div align=&quot;center&quot;&gt;&lt;IMG &lt;br&gt; src=&quot;/IMAGES/SKIN/2/1.gif&quot;&gt;&lt;/div&gt;&lt;/TD&gt; &lt;/TR&gt;&lt;br&gt; 网站尾部内容自定使用HTML&lt;br&gt; &lt;TD align=middle width=&quot;100%&quot;&gt;...

    IE6特有bug兼容性问题整理

    定位的,IE6只能说是半支持,好吧,这么说吧,可能不太严谨,就是背景图片固定的效果似乎只在根结点起作用。举个很简单的例子: 先看这段css代码: body{background:url(../image/404.png) no-repeat fixed center ...

    CSS之IE BUG分析与解决

    1 在IE6中,设定height为固定值时,当内容高于此设定值时,div框会自动扩展,而在IE7、IE8与firfox中div高度为固定值,不会扩展。Width同理。 标准 IE6 2 当设定min-height时,在标准浏览器中,表现为与IE6设定固定...

    解析width:100%;与width:auto;的区别

    本文主要介绍了width:100%;与width:auto;的区别,相信对大家学习网页布局会有很好的帮助,下面就跟小编一起来看下吧

    css属性width默认值width: auto与width: 100%区别详解

    width: auto 子元素(包括content+padding+border+...width: 100% 强制将子元素的content区域 撑满 父元素的content区域 子元素有margin、border、padding时,不改变子元素content区域的width,而是溢出父盒子,保

    jasperreports flash 改写

    更改了jasperreports flash的官方版,实现打印,pdf,excel,word的导出,添加了打印后的回调执行,基本问题都已经处理,&lt;object width="100%" height="98%"&gt; ...

    兼容IE6、IE7的min-width、max-width写法

    主要介绍了兼容IE6、IE7的min-width、max-width写法,需要的朋友可以参考下

    搜索引擎名次查询.net版源码下载

    bgColor="#f2f2f2"&gt;&lt;table width="100%" border="0" cellspacing="0" cellpadding="10"&gt; &lt;!-- 表单提交名为当前页的名字,如当前页改名,则action的值必须改为...

    教师档案管理系统源代码

    &lt;option value="7"&gt;民族 父亲姓名 母亲姓名 户口性质 学籍号 政治面貌 身份证号 学籍号 联系电话 户口性质 现居住地 家庭详细地址 母亲工作单位 父亲工作单位 ...

    动网论坛头衔购买for dvbbs7.0 sp2

    40%&quot; class=tablebody1&gt;头&nbsp;&nbsp;&nbsp;&nbsp;衔&lt;/B&gt;:&lt;/td&gt; &lt;td width=&quot;60%&quot; class=tablebody1&gt;&lt;input type=&quot;text&quot; name=&quot;title&quot; value=&quot;{$user_Title}&quot; ...

    管理员登陆确认码转换为图片插件 for dvbbs 6.0

    时间匆忙快要高考了,如果有错误请谅解^^ 1... width=&quot;40&quot; marginwidth=&quot;2&quot; marginheight=&quot;2&quot; scrolling=&quot;no&quot; border=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;

    div宽度设置width:100%后再设置padding或margin超出父元素的解决办法

    本文介绍的是利用CSS3的新属性box-sizing,解决div宽度设置width:100%后再设置padding或margin超出父元素的问题,有需要的朋友们可以参考借鉴。 语法 box-sizing: content-box|border-box|inherit; 值一、content-...

    C# 列表的下拉框联动.pdf

    &lt;asp:DropDownList ID="ddl_week_p" runat="server" Width="100px" AutoPostBack="true"&gt; &lt;asp:ListItem Selected="True" Value=""&gt;---- &lt;asp:DropDownList ID="ddl_e_week_p" ToolTip='&lt;%# DataBinder.Eval&#40;...

    动网论坛头衔购买完善版v1.2 for dvbbs7.0 sp2

    40%&quot; class=tablebody1&gt;头&nbsp;&nbsp;&nbsp;&nbsp;衔&lt;/B&gt;:&lt;/td&gt; &lt;td width=&quot;60%&quot; class=tablebody1&gt;&lt;input type=&quot;text&quot; name=&quot;title&quot; value=&quot;{$user_Title}&quot; ...

    jQuery select小控件适用IE6、IE7、IE8

    提供原代码,可以根据自己的项目要求修改,只在IE6、IE7、IE8、IE9中测试。有问题联系邮箱:zliuyao2010@163.com 初始化构造 &lt; input id="text1" type="text" echange="echange" class="e_select" style="width: ...

    网络考试系统

    100%&quot;&gt; &lt;tr&gt; &lt;td width=&quot;100%&quot; align=&quot;center&quot;&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width=&quot;100%&quot; align=&quot;center&quot;&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/table&gt;&lt;div align=&quot;center&quot;&gt;&lt;center&gt;...

Global site tag (gtag.js) - Google Analytics