RD8008: IE6 IE7(Q) IE8(Q) 绝对定位元素无法根据其四个方向的偏移量自动计算其尺寸...

标准参考

根据 W3C CSS2.1 规范中规定了非替换绝对定位元素的宽度计算,其中提到:

The constraint that determines the used values for these elements is:
'left' + 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' + 'right' = width of containing block
Otherwise, set 'auto' values for 'margin-left' and 'margin-right' to 0, and pick the one of the following six rules that applies.
...
5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'

绝对定位元素的宽度计算遵从上面引用框中的等式。但除去下列情况之外:

  • 'left'、'width'、'right' 三个值均是 "auto"
  • 'left'、'width'、'right' 三个值均不是 "auto"

第五条规则指出,'width' 为 "auto" ,'left'、'right' 不是 "auto" ,则计算 'width' 的值。

对于非替换绝对定位的高度计算,也有类似的限制:

For absolutely positioned elements, the used values of the vertical dimensions must satisfy this constraint:
'top' + 'margin-top' + 'border-top-width' + 'padding-top' + 'height' + 'padding-bottom' + 'border-bottom-width' + 'margin-bottom' + 'bottom' = height of containing block
Otherwise, pick the one of the following six rules that applies.
...
5. 'height' is 'auto', 'top' and 'bottom' are not 'auto', then 'auto' values for 'margin-top' and 'margin-bottom' are set to 0 and solve for 'height'

'height' 为 "auto" ,'top'、'bottom' 不是 "auto" ,则 'margin-top'、'margin-bottom' 为 0,计算 'height' 的值。

关于 非替换绝对定位元素的宽度及高度计算 的更多信息,请参考 CSS2.1 规范 10.3.710.6.4

问题描述

IE6 及 IE7/8 的混杂模式下,非替换绝度定位元素当指定了 'left' 及 'right',而 'width' 为默认值 "auto" 。此时浏览器无法正确地计算出 'width' 的值,对于高度的计算也是如此。

造成的影响

此问题可能导致绝对定位元素的宽度和高度在 IE6 IE7(Q) IE8(Q) 中与其他浏览器有很大的差异。

受影响的浏览器

IE6 IE7(Q) IE8(Q)无法根据绝对定位元素四个方向的偏移量自动计算其尺寸

问题分析

在 IE6 IE7(Q) IE8(Q) 中,若一个非替换绝对定位元素没有显式设定 'width' 和 'height' 特性,则其无法如规范所述根据其四个方向的偏移量自动计算其尺寸。

分析以下代码:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div style="background:gray; width:100px; height:100px; position:relative;">
        <div id="d" style="background:black; position:absolute; left:10px; right:10px; top:10px; bottom:10px; color:white;">test</div>
    </div>
</body>
</html>

上面代码中,一个 'position' 为 relative 的 100x100 的灰色背景 DIV 元素内包含一个黑色背景的 DIV 元素【d】,【d】为非替换绝对定位元素,其 'top'、'right'、'bottom'、'left' 均为 10px,且没有显式的设置 'width' 及 'height'。

在各浏览器中效果如下:

IE6 IE7(Q) IE8(Q)IE7(S) IE8(S) Firefox Chrome Safari Opera
IE6 IE7(Q) IE8(Q)IE7(S) IE8(S) Firefox Chrome Safari Opera
  • IE6 IE7(Q) IE8(Q)中,当指定了 'left' 及 'right',而 'width' 为默认值 "auto" 。此时浏览器无法正确地计算出 'width' 的值,导致【d】的宽度为 shrink-to-fit 宽度。对于高度的计算也是如此;
  • 其他浏览器 中,宽度和高度根据设定的 'top'、'right'、'bottom'、'left' 的值计算而来,符合 W3C 对非替换绝对定位元素的宽度和高度计算要求。

解决方案

若能为非替换绝对定位元素设定固定的宽度及高度,则尽量不使用此方式自动计算绝对定位元素的 'width' 及 'height';若无法避免使用此方式,则可以通过判断浏览器,仅在 IE6 中使用 CSS Expression 控制绝对定位元素的宽度及高度。

使用 CSS Expression 控制绝对定位元素的宽度及高度的参考代码如下:

<!DOCTYPE html>
<html>
<head>
<style>
  #d {
    background-color: black;
    position: absolute;
    left: 10px;
    right: 10px;
    top: 10px;
    bottom: 10px;
    color: white;
    _width: expression(
      parseInt(this.offsetParent.currentStyle.width)
        - parseInt(this.currentStyle.left)
        - parseInt(this.currentStyle.right)
    );
    _height: expression(
      parseInt(this.offsetParent.currentStyle.height)
        - parseInt(this.currentStyle.top)
        - parseInt(this.currentStyle.bottom)
    );
  }
</style>
</head>
<body>
    <div style="background-color:gray;width:100px;height:100px;position:relative">
        <div id="d">test</div>
    </div>
</body>
</html>

转载于:https://www.cnblogs.com/ckmouse/archive/2012/02/14/2351220.html

内容概要:本文档主要展示了C语言中关于字符串处理、指针操作以及动态内存分配的相关代码示例。首先介绍了如何实现键值对(“key=value&rdquo;)字符串的解析,包括去除多余空格和根据键获取对应值的功能,并提供了相应的测试用例。接着演示了从给定字符串中分离出奇偶位置字符的方法,并将结果分别存储到两个不同的缓冲区中。此外,还探讨了常量(const)修饰符在变量和指针中的应用规则,解释了不同类型指针的区别及其使用场景。最后,详细讲解了如何动态分配二维字符数组,并实现了对这类数组的排序与释放操作。 适合人群:具有C语言基础的程序员或计算机科学相关专业的学生,尤其是那些希望深入理解字符串处理、指针操作以及动态内存管理机制的学习者。 使用场景及目标:①掌握如何高效地解析键值对字符串并去除其中的空白字符;②学会编写能够正确处理奇偶索引字符的函数;③理解const修饰符的作用范围及其对程序逻辑的影响;④熟悉动态分配二维字符数组的技术,并能对其进行有效的排序和清理。 阅读建议:由于本资源涉及较多底层概念和技术细节,建议读者先复习C语言基础知识,特别是指针和内存管理部分。在学习过程中,可以尝试动手编写类似的代码片段,以便更好地理解和掌握文中所介绍的各种技巧。同时,注意观察代码注释,它们对于理解复杂逻辑非常有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值