首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有异常,某些情况上会导致绝对定位元素位置跟其他浏览器中有差异

2012-11-03 
Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有错误,某些情况下会导致绝对定位元素位置跟其他浏览

Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有错误,某些情况下会导致绝对定位元素位置跟其他浏览器中有差异

关于绝对定位元素的定位,依赖于其包含块。也就是说,当绝对定位元素的"top"、"left"值被设置之后,绝对定位元素会根据这两个值,相对于其包含块产生位移。见:http://www.w3.org/TR/CSS2/visuren.html#absolute-positioning

关于包含块的概念,在帖子:【分享】说说标准 -- 可视化格式模型(visual formatting model)之一 -- 包含块(containing block)已经做了详细的说明,其中,W3C CSS2.1规范中规定,绝对定位元素的包含块(containing block),由离它最近的position特性值是"absolute"、"fixed"、"relative"之一的祖先元素组成;如果这个祖先元素不存在,则包含块是初始包含块。

但是,这个规则在Firefox里好像不大管用。

本来想做一个很华丽丽的效果,需要把一个绚丽的图片针对表格的左上角定位,,本来在其他浏览器中好好的,但是到Firefox中就不灵了……

HTML code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><div style="width:200px; height:200px; position:absolute; background-color:silver;">   <table style="position:absolute; width:100px; height:100px; top:100px; left:100px; background-color:green;">       <tr>               <td>                       <div style="position:absolute; width:50px; height:50px; top:50px; left:50px; background-color: gold;">                               TEXT                        </div>               </td>       </tr>    </table></div>

本来是要想让金色的绝对定位 DIV 相对于 TABLE 来定位,但事与愿违,在 Firefox 里,变成了这样:
Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有异常,某些情况上会导致绝对定位元素位置跟其他浏览器中有差异
金色的 DIV 没有相对于绿色 TABLE 的左上角定位,跑偏了,相对于灰色的 DIV 定位了,当TABLE 不存在,哈!

其他浏览器中,就这样子:
Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有异常,某些情况上会导致绝对定位元素位置跟其他浏览器中有差异
可见,在Firefox中,TABLE 中好像创建不了包含块。
所以,在Firefox里,绝对定位元素的包含块是"position"特性值为"absolute"、"fixed"、"relative"之一的非table类型的祖先元素。定位的时候,也就会出现意想不到的效果。

这个问题,还可能会影响绝对定位元素的自动宽度计算。

所以,想相对于 TABLE 元素定位的同学小心了。

?

?

解决方案

这是 Firefox 的一个 bug,绝对定位元素无法根据 'display' 特性是 'table' 且是绝对定位的祖先元素定位。

所以,不要采用这种结构。

为达到相同的效果,可以采用如下方式替代:

改变元素的 containing block:让绝对定位元素相对于 TABLE 元素的单元格定位,即,在 TD 上设置 'position:absolute' ;改变元素的定位方式:把绝对定位元素的 'absolute' 改为 'relative' ,用相对定位。

还有就是table 用div形式来表现

?

<html><head><style type="text/css">.row div{ border:1px solid black; height:20px; width:24%; float:left; margin-right:-1px; margin-top:-1px; text-align:center; font-weight:bold; display:block;}#head div{ background-color:#CCFFFF; margin-top:0;}</style>   <style type="text/css"> <!--  /*重点:固定行头样式*/.scrollRowThead{position: relative; left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);z-index:0;}/*重点:固定表头样式*/.scrollColThead {position: relative;top: expression(this.parentElement.parentElement.parentElement.scrollTop);z-index:2;}/*行列交叉的地方*/.scrollCR { z-index:3;}/*div外框*/.scrollDiv {height:400px;clear: both; border: 1px solid #EEEEEE;OVERFLOW: scroll;width: 900px; }/*行头居中*/.scrollColThead td,.scrollColThead th{ text-align: center ;}/*行头列头背景*/.scrollRowThead,.scrollColThead td,.scrollColThead th{background-color:EEEEEE;}/*表格的线*/.scrolltable{border-bottom:1px solid #CCCCCC; border-right:1px solid #CCCCCC; }/*单元格的线等*/.scrolltable td,.scrollTable th{border-left: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; padding: 5px; }.scrollTable thead th{background-color:#EEEEEE;font-weight:bold;position:relative;}--> </style><script language="javascript">function changeplace(x){document.getElementById('scrollRowThead').style.left=x+"px";//alert(document.getElementById('scrollRowThead').style.left);}</script><head><body><div id="scrollDiv" onscroll="changeplace(document.getElementById('scrollRowThead').parentNode.parentNode.scrollLeft)"> <div id="head" style="width:200%" id="row1" style="width:200%;" onmousedown="move(this)">     <div id="scrollRowThead">haha</div><div></div><div></div><div></div>    </div></div></html> 

?

<html xmlns="http://www.w3.org/1999/xhtml"><HTML><HEAD><TITLE></TITLE> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> <!-- /*重点:固定行头样式*/.scrollRowThead{position: relative; left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);z-index:0;}/*重点:固定表头样式*/.scrollColThead {position: relative;top: expression(this.parentElement.parentElement.parentElement.scrollTop);z-index:2;}/*行列交叉的地方*/.scrollCR { z-index:3;}/*div外框*/.scrollDiv {height:400px;clear: both; border: 1px solid #EEEEEE;OVERFLOW: scroll;width: 900px; }/*行头居中*/.scrollColThead td,.scrollColThead th{ text-align: center ;}/*行头列头背景*/.scrollRowThead,.scrollColThead td,.scrollColThead th{background-color:EEEEEE;}/*表格的线*/.scrolltable{border-bottom:1px solid #CCCCCC; border-right:1px solid #CCCCCC; }/*单元格的线等*/.scrolltable td,.scrollTable th{border-left: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; padding: 5px; }.scrollTable thead th{background-color:#EEEEEE;font-weight:bold;position:relative;}--> </style> </HEAD><body><div id="scrollDiv" cellpadding="3" cellspacing="0" width="1300" > <th >&nbsp;</th> <th colspan="2">列头</th> <th colspan="2">列头</th> <th colspan="2">列头</th> <th colspan="2">列头</th> <th colspan="2">列头</th> <th rowspan="2">列头</th></tr><tr > <th >h1</th> <th >h2</th> <th >h3</th> <th >h4</th> <th >h3</th> <th >h4</th> <th >h3</th> <th >h4</th> <th >h3</th> <th >h4</th> <th >h5</th></tr></thead><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr><tr> <td ><input type="checkbox" name="checkbox" value="checkbox">a</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td> <td>单元格5</td></tr></table></div></body></html> 还有什么办法解决没

热点排行