简体   繁体   English

绝对定位的表头(th) - 哪个浏览器错了?

[英]Absolutely-positioned table header (th) - which browser is wrong?

This simple table with one absolutely-positioned column renders differently in Firefox (and IE) than in Chrome (and other Webkit-based browsers): 这个带有一个绝对定位列的简单表在Firefox(和IE)中的呈现方式与在Chrome(以及其他基于Webkit的浏览器)中的呈现方式不同:

在此输入图像描述

http://jsfiddle.net/WZ6x8/ http://jsfiddle.net/WZ6x8/

<div>
    <table>
        <tr>
            <th class="absolute">&nbsp;</th>
            <th>&nbsp;</th>
        </tr>
    </table>
</div>

* {
    margin: 0;
    padding: 0;
}
table {
    border-collapse: collapse;
    table-layout:fixed;
}
th {
    border: 1px solid black;
    width: 100px;
}
.absolute {
    left: 0;
    position: absolute;
}
div {
    margin-left: 100px;
}

Why is there a difference? 为什么会有区别? Which browser is wrong (according to the standard)? 哪个浏览器出错(根据标准)? How can this be fixed to work on all browsers, without removing the line border-collapse: collapse ? 如何在删除行border-collapse: collapse 情况下修复所有浏览器的工作border-collapse: collapse

Edit: as noted by @urzeit, "If you specify top: 0; the output in firefox is the same as in chrome." 编辑:正如@urzeit所说,“如果你指定top: 0; firefox中的输出与chrome相同。” However, there are two issues: first, with multiple rows, top: 0; 但是,有两个问题:第一,有多行, top: 0; collapses them all into one; 将它们全部折叠成一个; second, the right edge of the absolutely-positioned column extends one pixel too far. 第二,绝对定位列的右边缘延伸一个像素太远。 You can see both issues at http://jsfiddle.net/WZ6x8/3 . 您可以在http://jsfiddle.net/WZ6x8/3上看到这两个问题。

This is caused by rounding issues which are often inconsistent across browsers, and are probably the same ones that plague things like background offsets, percentage calculations and so on. 这是由于舍入问题引起的,这些问题通常在浏览器中不一致,并且可能与背景偏移,百分比计算等问题相同。

In a nutshell, the reason why rounding issues come into play is found in section 17.6.2 which, as you may have guessed, describes the collapsing border model: 简而言之,舍入问题发挥作用的原因可以在第17.6.2节中找到 ,正如您可能已经猜到的那样,它描述了折叠边界模型:

Borders are centered on the grid lines between the cells. 边框以单元格之间的网格线为中心。 User agents must find a consistent rule for rounding off in the case of an odd number of discrete units (screen pixels, printer dots). 在奇数个离散单元(屏幕像素,打印机点)的情况下,用户代理必须找到一致的规则以进行四舍五入。

And: 和:

The top border width of the table is computed by examining all cells who collapse their top borders with the top border of the table. 通过检查使用表的顶部边框折叠其顶部边框的所有单元格来计算表格的顶部边框宽度。 The top border width of the table is equal to half of the maximum collapsed top border. 表格的顶部边框宽度等于最大折叠顶部边框的一半。 The bottom border width is computed by examining all cells whose bottom borders collapse with the bottom of the table. 通过检查底部边框与表底部折叠的所有单元格来计算底部边框宽度。 The bottom border width is equal to half of the maximum collapsed bottom border. 底部边框宽度等于最大折叠底部边框的一半。

Since the border width is 1 pixel, which is an odd number, rounding issues occur when attempting to halve that value. 由于边界宽度是1个像素,这是一个奇数,因此在尝试将该值减半时会出现舍入问题。 So the question of which browser is wrong — or if any of them are — is debatable. 因此,哪个浏览器错误 - 或者是否有任何浏览器 - 的问题是值得商榷的。

Unfortunately, because this is a rounding issue, it's not possible to work around this using pure CSS unless through inconceivably elaborate hackery, and very difficult to do so using a script as browsers tend to be inconsistent when reporting decimal/fractional offset values as well (in particular, Firefox, IE and Chrome all report wildly different values for offsetTop of your table-cell and its next sibling). 不幸的是,因为这是一个舍入问题,除非通过不可思议的复杂hackery,否则不可能使用纯CSS解决这个问题,并且使用脚本很难这样做,因为在报告十进制/小数偏移值时浏览器往往不一致(特别是,Firefox,IE和Chrome 报告了表格单元格及其下一个兄弟版本的offsetTop的不同值。

While I can't offer a solution to your problem, hopefully at least I've helped you understand why browsers are behaving the way they do. 虽然我无法为您的问题提供解决方案,但希望至少我已经帮助您理解为什么浏览器的行为方式如此。


Here's the nitty gritty if you're interested in why the issue ultimately lies in the way the collapsing border model is defined. 如果您对为什么问题最终取决于折叠边界模型的定义方式感兴趣,那么这就是细节。

Section 9.7 states that if an element is absolutely positioned, then its display is set to block , even if it would otherwise be a table-cell . 第9.7节规定如果一个元素是绝对定位的,那么它的display被设置为block ,即使它本来是一个table-cell In all browsers, the computed display for that th is in fact block , so no problem there. 在所有的浏览器中,计算的display为该th实际上block ,所以没有问题出现。

As you've correctly pointed out, the containing block of your table cell is the initial containing block. 正如你正确地指出的那样, 包含块的表格单元格的是初始包含块。 This removes it from its usual containing block which would otherwise be the table. 这将它从通常的包含块中移除,否则它将成为表。 Section 10.6.4 does add that that if your cell does not have any specified height, top or bottom offsets, ie they are all auto , then it should remain in its static vertical position and measurements made accordingly. 第10.6.4节确实补充说,如果你的单元没有任何指定的高度,顶部或底部偏移,即它们都是auto ,那么它应该保持在静态垂直位置并相应地进行测量。 (Likewise the horizontal position is accounted for in section 10.3.7 , however since you've given it left: 0 and width: 100px , it gets shifted to the left edge and its width is as specified, excluding the borders.) (同样,水平位置在10.3.7节中考虑 ,但是因为你已经给它left: 0width: 100px ,它会移动到左边缘,它的宽度是指定的,不包括边框。)

But what is this static vertical position? 但是,什么这个静态垂直位置? Because it would ordinarily be display: table-cell if it wasn't absolutely-positioned, the static position, and its corresponding measurements, is determined by its position in the table. 因为它通常是display: table-cell如果它没有绝对定位,静态位置及其相应的测量值由它在表中的位置决定。

Your given table layout is covered by the following subsections of section 17 : 您给定的表格布局由第17节的以下小节涵盖:

Section 17 contains elaborate descriptions on how tables, captions, table-rows and table-cells should be laid out. 第17节包含有关如何布置表,标题,表格行和表格单元格的详细说明。 A lot of it is based on HTML, and certain sections are left vague and/or undefined for various reasons. 其中很多都是基于HTML的,并且由于各种原因,某些部分变得模糊和/或未定义。 Fixed table layout is pretty well-defined though, and in this case it is not even relevant. 固定表格布局虽然定义得很好,但在这种情况下甚至不相关。

Section 17.5 says this near the bottom: 第17.5节说明了这一点在底部附近:

The edges of the rows, columns, row groups and column groups in the collapsing borders model coincide with the hypothetical grid lines on which the borders of the cells are centered. 折叠边框模型中的行,列,行组和列组的边缘与单元格边框居中的假想网格线重合。 (And thus, in this model, the rows together exactly cover the table, leaving no gaps; ditto for the columns.) (因此,在这个模型中,行一起完全覆盖了表格,没有留下任何间隙;同样适用于列。)

And: 和:

Note. 注意。 Positioning and floating of table cells can cause them not to be table cells anymore, according to the rules in section 9.7 . 根据第9.7节中的规则,表格单元格的定位和浮动可能导致它们不再是表格单元格。 When floating is used, the rules on anonymous table objects may cause an anonymous cell object to be created as well. 使用浮动时,匿名表对象上的规则也可能导致创建匿名单元对象。

Which, of course, has been explained just above. 当然,上面已经解释过了。

But if an absolutely-positioned table cell is no longer a cell, what happens ? 但如果一个绝对定位的表格单元不再是一个单元格, 会发生什么

A "missing cell" is a cell in the row/column grid that is not occupied by an element or pseudo-element. “缺失单元”是行/列网格中未被元素或伪元素占据的单元。 Missing cells are rendered as if an anonymous table-cell box occupied their position in the grid. 丢失的单元格呈现为好像匿名表格单元格框占据了它们在网格中的位置。

So while the actual th box is absolutely positioned, it leaves an anonymous "ghost" cell in its place in order for the table to be rendered properly. 因此,尽管实际th框绝对定位,它的叶子在其位匿名的“鬼”细胞,以便进行正确呈现表。 This anonymous table-cell is empty and does not inherit the styles from the actual table-cell, so it has no intrinsic width. 该匿名表单元格是空的, 继承从实际的表单元格的样式,所以它有没有内在的宽度。

However, because the actual table-cell's vertical position is static, it is still affected by how the table is rendered, which brings us to section 17.6.2 on the collapsing border model. 但是,因为实际的表格单元格的垂直位置是静态的,所以它仍然受到表格渲染方式的影响,这将我们带到折叠边界模型的第17.6.2节。

multiple rows - Try this css.. 多行 - 试试这个css ..

<style>
.absolute {
    left: 0;
    margin-top: -1px;
    border-right:0px;
    position: absolute;
}

@media screen and (-webkit-min-device-pixel-ratio:0)
{
.absolute {
    margin-top: 0px;
} 
} 
</style>

Chrome And Firefox Demo Screenshot. Chrome和Firefox演示截图。

在此输入图像描述

The reason this is happening is because you've specified the entry as absolute, meaning firefox's browser style where it sets the "top" css property to 1px is being used. 发生这种情况的原因是因为你已经将条目指定为绝对值,这意味着firefox的浏览器样式将“top”css属性设置为1px。 If you want to avoid this you might be able to use a css reset file which blows away browser-specific default formatting. 如果你想避免这种情况,你可以使用css重置文件,它会破坏特定于浏览器的默认格式。

As for your issue with setting top:0 resulting in the cells collapsing into each other, that is the correct behavior. 至于你设置top:0的问题导致单元格相互折叠,这是正确的行为。 Absolute renders in the space of the closest positioned CSS element. 绝对渲染在最接近定位的CSS元素的空间中。 Think of it as if the element is no longer in its usual place in the DOM. 可以把它想象成元素不再在DOM中的通常位置。 If the two cells specified as absolute have the same closest positioned parent element (in this case the body) then they would both render relative to the same place with each having the top:0 property which means they would overlap. 如果指定为绝对的两个单元格具有相同的最接近定位的父元素(在本例中为主体),那么它们将相对于相同的位置呈现,每个单元格具有top:0属性,这意味着它们将重叠。

I don't think either is wrong, you're just running into the (common) issue of browser styles. 我认为这两个都不对,你只是遇到浏览器风格的(常见)问题。

In short , 简而言之 ,

There are two problem to me in this problem 这个问题对我来说有两个问题

a) The first problem is overlapping of left <th> with right <th> a)第一个问题是左<th>和右<th>重叠

The correct browser is one which overlapp the <th> because you are firstly giving the outer div margin-right:100px; 正确的浏览器是重叠<th>浏览器,因为你首先给出外部div margin-right:100px; the <th> with aboslute property should fit this area, but the extra border of 1px increase the width by 2px(left border + right border) .Hence the overlap is correct 带有aboslute属性的<th>应该适合这个区域,但1px的额外边框会将宽度增加2px(左边框+右边框)。重叠是正确的

b) The second problem is of top margin for the left element b)第二个问题是左边元素的上边距

It can be removed by adding 它可以通过添加删除

th{
 top:0px;
}

This is because of browser effect to html page 这是因为浏览器对html页面的影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM