来自:http://www.99css.com/?p=43
在 web app 项目中经常遇到这个 bug,国外称之为100% ≠ 100% bug,又分为两种:
div 的宽度 100% ≠ 100% (IE 6&7)需求:
- 标准模式
- #container 局部滚动
- #asie 固定宽度
- #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');}
-
解决方法
#wrapper{#width:expression(this.parentNode.offsetHeight > this.parentNode.scrollHeight ? '100%' : parseInt(this.parentNode.clientWidth) + 'px');}
当然,写在 js 中亦可,不过要注意不要漏掉 window 的 riseze 事件,另外,window 的 resize 事件在 IE 中有执行多次的
bug
-
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)
45%"; }else if getPgjs "ipad" { alert "平板" ul parentNode style width "90%"; }else if getPgjs "iphone" { alert "手机" ul parentNode ...
100%"><div align="center"><IMG <br> src="/IMAGES/SKIN/2/1.gif"></div></TD> </TR><br> 网站尾部内容自定使用HTML<br> <TD align=middle width="100%">...
定位的,IE6只能说是半支持,好吧,这么说吧,可能不太严谨,就是背景图片固定的效果似乎只在根结点起作用。举个很简单的例子: 先看这段css代码: body{background:url(../image/404.png) no-repeat fixed center ...
1 在IE6中,设定height为固定值时,当内容高于此设定值时,div框会自动扩展,而在IE7、IE8与firfox中div高度为固定值,不会扩展。Width同理。 标准 IE6 2 当设定min-height时,在标准浏览器中,表现为与IE6设定固定...
本文主要介绍了width:100%;与width:auto;的区别,相信对大家学习网页布局会有很好的帮助,下面就跟小编一起来看下吧
width: auto 子元素(包括content+padding+border+...width: 100% 强制将子元素的content区域 撑满 父元素的content区域 子元素有margin、border、padding时,不改变子元素content区域的width,而是溢出父盒子,保
更改了jasperreports flash的官方版,实现打印,pdf,excel,word的导出,添加了打印后的回调执行,基本问题都已经处理,<object width="100%" height="98%"> ...
主要介绍了兼容IE6、IE7的min-width、max-width写法,需要的朋友可以参考下
bgColor="#f2f2f2"><table width="100%" border="0" cellspacing="0" cellpadding="10"> <!-- 表单提交名为当前页的名字,如当前页改名,则action的值必须改为...
<option value="7">民族 父亲姓名 母亲姓名 户口性质 学籍号 政治面貌 身份证号 学籍号 联系电话 户口性质 现居住地 家庭详细地址 母亲工作单位 父亲工作单位 ...
40%" class=tablebody1>头 衔</B>:</td> <td width="60%" class=tablebody1><input type="text" name="title" value="{$user_Title}" ...
时间匆忙快要高考了,如果有错误请谅解^^ 1... width="40" marginwidth="2" marginheight="2" scrolling="no" border="0" frameborder="0"></iframe>
本文介绍的是利用CSS3的新属性box-sizing,解决div宽度设置width:100%后再设置padding或margin超出父元素的问题,有需要的朋友们可以参考借鉴。 语法 box-sizing: content-box|border-box|inherit; 值一、content-...
<asp:DropDownList ID="ddl_week_p" runat="server" Width="100px" AutoPostBack="true"> <asp:ListItem Selected="True" Value="">---- <asp:DropDownList ID="ddl_e_week_p" ToolTip='<%# DataBinder.Eval(...
40%" class=tablebody1>头 衔</B>:</td> <td width="60%" class=tablebody1><input type="text" name="title" value="{$user_Title}" ...
提供原代码,可以根据自己的项目要求修改,只在IE6、IE7、IE8、IE9中测试。有问题联系邮箱:zliuyao2010@163.com 初始化构造 < input id="text1" type="text" echange="echange" class="e_select" style="width: ...
100%"> <tr> <td width="100%" align="center"></td> </tr> <tr> <td width="100%" align="center"></td> </tr></table><div align="center"><center>...