IE6不识min-width三种解决方法
时间:2008-02-08 来源:lucent918
其中600px自行设置。 width:expression(document.body.clientWidth <= 600? "600px": "auto" );
min-width:600px; 第二种
min-width默认是不被IE6支持的,不清楚IE7的情况,在FF中支持很好,而且这也是一个非常有用的属性。
既然IE6默认不支持,我们肯定就要使用一些技巧来至少表面上看来是支持的。
思路:3个div相对定位嵌套,通过border-left,和margin-left取负值的办法,实现最小宽度为border-left的值。
1,定义此width宽度为90%,min-width为400px;rule为400px的盒子做对比,此时用FF能看到我们的预期效果,在IE下不行:
.width { width:90%; min-width:400px; border:1px solid #aaa;}
.rule { width:400px; background:#c00; border:1px solid #c00; color:#fff; margin:10px 0; text-align:center; padding:5px 0;}
<div class="width">
内容填充内容填充内容填充内容填充内容填充内容填充内容填充
</div>
<div class="rule">这是400px宽</div>
效果演示:http://www.rexsong.com/blog/attachments/200512/13_202952_minwidth1.htm
2,在width内嵌套了一层minwidth,定义border-left为400px,同时相对定位,看到左边框空白的效果,留出我们需要的位置。
.width { width:90%; min-width:400px; border:1px solid #aaa;}
.rule { width:400px; background:#c00; border:1px solid #c00; color:#fff; margin:10px 0; text-align:center; padding:5px 0;}
* html .minwidth { border-left:400px solid #fff; position:relative; float:left; z-index:1;}
<div class="width">
<div class="minwidth">内容填充内容填充内容填充内容填充内容填充内容填充</div>
</div>
<div class="rule">这是400px宽</div>
效果演示:http://www.rexsong.com/blog/attachments/200512/13_203000_minwidth2.htm
3,在minwidth内再嵌套一层container,定义负左边距等于minwidth层的border-left值,同时相对定位,完成效果。
.width { width:90%; min-width:400px; border:1px solid #aaa;}
.rule { width:400px; background:#c00; border:1px solid #c00; color:#fff; margin:10px 0; text-align:center; padding:5px 0;}
* html .minwidth { border-left:400px solid #fff; position:relative; float:left; z-index:1;}
* html .container { margin-left:-400px; position:relative; float:left; z-index:2; padding:3px;}
<div class="width">
<div class="minwidth">
<div class="container">
最内层必须要有内容填充,最内层必须要有内容填充,最内层必须要有内容填充,最内层必须要有内容填充,最内层必须要有内容填充。
</div>
</div>
</div>
<div class="rule">这是400px宽</div>
效果演示:http://www.rexsong.com/blog/attachments/200512/13_203009_minwidth3.htm
这又是一个负边距例子,我们不难理解:当container宽超过400px时,container右边距和minwidth一样自动延伸;当container宽不超过400px时,container右边距等于minwidth左边距,也就是说container的宽就是minwidth的border-left,只不过浮动在上边而已。
提示:* html .minwidth的写法是IE6专用(IE5未知,有兴趣的测试),可以使用其他hack手段。
基本思路而已,如果需要提高兼容性,可在此基础扩展。个人认为实用有限,因为使用了浮动,不过在某些场合表现出来还是蛮神奇的。
How to Use CSS to Solve min-width Problems in Internet Explorer http://www.webreference.com/programming/min-width/
无忧脚本
第三种
Javascript:
- // 说明:让 IE6 及其以下版本支持 CSS 中 min/max-width/height 属性
- // 整理:http://www.codebit.cn
- // minmax.js: make IE5+/Win support CSS min/max-width/height
- // version 1.0, 08-Aug-2003
- // written by Andrew Clover <[email protected]>, use freely
- /*@cc_on
- @if (@_win32 && @_jscript_version>4)
- var minmax_elements;
- minmax_props= new Array(
- new Array('min-width', 'minWidth'),
- new Array('max-width', 'maxWidth'),
- new Array('min-height','minHeight'),
- new Array('max-height','maxHeight')
- );
- // Binding. Called on all new elements. If <body>, initialise; check all
- // elements for minmax properties
- function minmax_bind(el) {
- var i, em, ms;
- var st= el.style, cs= el.currentStyle;
- if (minmax_elements==window.undefined) {
- // initialise when body element has turned up, but only on IE
- if (!document.body || !document.body.currentStyle) return;
- minmax_elements= new Array();
- window.attachEvent('onresize', minmax_delayout);
- // make font size listener
- em= document.createElement('div');
- em.setAttribute('id', 'minmax_em');
- em.style.position= 'absolute'; em.style.visibility= 'hidden';
- em.style.fontSize= 'xx-large'; em.style.height= '5em';
- em.style.top='-5em'; em.style.left= '0';
- if (em.style.setExpression) {
- em.style.setExpression('width', 'minmax_checkFont()');
- document.body.insertBefore(em, document.body.firstChild);
- }
- }
- // transform hyphenated properties the browser has not caught to camelCase
- for (i= minmax_props.length; i-->0;)
- if (cs[minmax_props[i][0]])
- st[minmax_props[i][1]]= cs[minmax_props[i][0]];
- // add element with properties to list, store optimal size values
- for (i= minmax_props.length; i-->0;) {
- ms= cs[minmax_props[i][1]];
- if (ms && ms!='auto' && ms!='none' && ms!='0' && ms!='') {
- st.minmaxWidth= cs.width; st.minmaxHeight= cs.height;
- minmax_elements[minmax_elements.length]= el;
- // will need a layout later
- minmax_delayout();
- break;
- } }
- }
- // check for font size changes
- var minmax_fontsize= 0;
- function minmax_checkFont() {
- var fs= document.getElementById('minmax_em').offsetHeight;
- if (minmax_fontsize!=fs && minmax_fontsize!=0)
- minmax_delayout();
- minmax_fontsize= fs;
- return '5em';
- }
- // Layout. Called after window and font size-change. Go through elements we
- // picked out earlier and set their size to the minimum, maximum and optimum,
- // choosing whichever is appropriate
- // Request re-layout at next available moment
- var minmax_delaying= false;
- function minmax_delayout() {
- if (minmax_delaying) return;
- minmax_delaying= true;
- window.setTimeout(minmax_layout, 0);
- }
- function minmax_stopdelaying() {
- minmax_delaying= false;
- }
- function minmax_layout() {
- window.setTimeout(minmax_stopdelaying, 100);
- var i, el, st, cs, optimal, inrange;
- for (i= minmax_elements.length; i-->0;) {
- el= minmax_elements[i]; st= el.style; cs= el.currentStyle;
- // horizontal size bounding
- st.width= st.minmaxWidth; optimal= el.offsetWidth;
- inrange= true;
- if (inrange && cs.minWidth && cs.minWidth!='0' && cs.minWidth!='auto' && cs.minWidth!='') {
- st.width= cs.minWidth;
- inrange= (el.offsetWidth<optimal);
- }
- if (inrange && cs.maxWidth && cs.maxWidth!='none' && cs.maxWidth!='auto' && cs.maxWidth!='') {
- st.width= cs.maxWidth;
- inrange= (el.offsetWidth>optimal);
- }
- if (inrange) st.width= st.minmaxWidth;
- // vertical size bounding
- st.height= st.minmaxHeight; optimal= el.offsetHeight;
- inrange= true;
- if (inrange && cs.minHeight && cs.minHeight!='0' && cs.minHeight!='auto' && cs.minHeight!='') {
- st.height= cs.minHeight;
- inrange= (el.offsetHeight<optimal);
- }
- if (inrange && cs.maxHeight && cs.maxHeight!='none' && cs.maxHeight!='auto' && cs.maxHeight!='') {
- st.height= cs.maxHeight;
- inrange= (el.offsetHeight>optimal);
- }
- if (inrange) st.height= st.minmaxHeight;
- }
- }
- // Scanning. Check document every so often until it has finished loading. Do
- // nothing until <body> arrives, then call main init. Pass any new elements
- // found on each scan to be bound
- var minmax_SCANDELAY= 500;
- function minmax_scan() {
- var el;
- for (var i= 0; i<document.all.length; i++) {
- el= document.all[i];
- if (!el.minmax_bound) {
- el.minmax_bound= true;
- minmax_bind(el);
- } }
- }
- var minmax_scanner;
- function minmax_stop() {
- window.clearInterval(minmax_scanner);
- minmax_scan();
- }
- minmax_scan();
- minmax_scanner= window.setInterval(minmax_scan, minmax_SCANDELAY);
- window.attachEvent('onload', minmax_stop);
- @end @*/
由于只有 IE6 及其以下版本不支持min/max-width/height 属性,因此,我们可以用下面的调用方式:
Code:
<!--[if lt IE 7]>
<![endif]-->