Firefox 1 像素错误与边框折叠,解决方法吗?
对于以下“向左 1 像素”错误有任何解决方法吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body>
<div style="padding: 50px">
<div style="border: 1px solid red">Table header info</div>
<table style="border: 1px solid green; border-collapse: collapse; width: 100%">
<tbody>
<tr>
<th>Col1</th>
<th>Col2</th>
</tr>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</tbody>
</table>
<div style="border: 1px solid red">Table footer info</div>
</div>
</body>
</html>
它看起来像这样:
有没有纯 CSS 的解决方案?
编辑
我对我的表格有点不清楚,所以这里又是:
边框折叠:
按照建议使用 cellspacing="0" 且没有边框折叠:
所以现在我的表格内的边框是加倍,但我希望表格的边框为 1 像素。
当我从表格中删除 1px 边框时,我以以下内容结尾:
我的表格内的边框仍然是双倍的。
我可以为每个 TD、TH 设置右边框和下边框,为 TR 中的每个第一个子边框设置左边框来实现我想要的,但我认为有更简单的方法吗?
Is there any workaround for the following "1 pixel to the left" bug?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body>
<div style="padding: 50px">
<div style="border: 1px solid red">Table header info</div>
<table style="border: 1px solid green; border-collapse: collapse; width: 100%">
<tbody>
<tr>
<th>Col1</th>
<th>Col2</th>
</tr>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</tbody>
</table>
<div style="border: 1px solid red">Table footer info</div>
</div>
</body>
</html>
It looks like this:
Is there any pure CSS solution to this?
Edit
I was bit unclear about my table so here it is again:
With border-collapse:
With cellspacing="0" and without border-collapse as suggested:
So now the borders inside my table are doubled, but I want 1px border across my table.
When I remove 1px border from table I end with:
Borders are still doubled inside my table.
I could set only right and bottom border for every TD, TH and left border for every first-child in TR to achieve what I want, but I think there's a simpler way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于那些喜欢将演示文稿排除在标记之外或无法访问标记的人,这里有一个纯粹的 CSS 解决方案。 我自己也遇到了这个问题,并在 FF3.5、IE6、IE7、IE8、Safari 4、Opera 10 和 Google Chrome 中测试了这个解决方案。
这会将表格设置为在所有浏览器(包括罪魁祸首 Firefox)中使用边框间距。 然后它使用 CSS star hack 仅向 IE 呈现边框折叠规则,这不能正确应用边框间距(如果您不喜欢 hack,您还可以为 IE 添加一个带有条件注释的单独样式表)。
如果您喜欢使用单元格间距,请务必使用它。 这只是作为使用 CSS 的替代方法提供的。
For those who prefer to keep presentation out of the markup, or who don't have access to the markup, here is a purely CSS solution. Just ran into this problem myself, and tested this solution in FF3.5, IE6, IE7, IE8, Safari 4, Opera 10, and Google Chrome.
This sets the table to use border-spacing in all browsers (including the culprit, Firefox). Then it uses the CSS star hack to present the border-collapse rule only to IE, which doesn't properly apply border-spacing (you could also include a separate stylesheet for IE with conditional comments if you don't like hacks).
If you prefer using cell-spacing, by all means use it. This is simply offered as an alternative method using CSS.
删除边框折叠并使用 cellspacing=0 代替。
发生这种情况是因为当您设置 border-collapse:collapse 时,Firefox 2.0 将边框置于 tr 的外部。 其他浏览器把它放在里面。
在代码中将边框宽度设置为 10px 以查看实际发生的情况。
OP编辑后编辑
您可以使用旧的表格“边框”技巧。 设置表格的背景颜色。 将 td 和 th 颜色设置为白色。 用户单元格间距= 1;
Remove the border-collapse and use cellspacing=0 instead.
It happens because when you set border-collapse:collapse, Firefox 2.0 puts the border to the outside of the tr. The other browsers put it on the inside.
Set your border widths to 10px in your code to see what is really happening.
edit after OP edit
You can use the old table "border" trick. Set the background color on the table. Set the td and th color to white. User cellspacing = 1;
这个问题已经不存在了。 在 Firefox 47.0.1 中,以下 CSS 不会出现单像素问题:
我们还可以在不依赖
border-collapse
的有效实现的情况下获得干净的单像素边框,如下所示:你明白它在做什么吗? 诀窍是我们只在单元格上放置顶部和左侧边框:
这使得表格没有右侧和底部边缘:我们将它们设置为
table
border-spacing: 0
code> 是必不可少的,否则这些线将无法连接。This issue no longer exists. In Firefox 47.0.1, the following CSS doesn't exhibit the one pixel problem:
We can also get clean one-pixel borders to work without relying on a working implementation of
border-collapse
, like this:You see what it's doing? The trick is that we put only a top and left border on the cells:
This leaves the table without a right and bottom edge: we style those onto
table
The
border-spacing: 0
is essential otherwise these lines will not connect.最佳仅 CSS 解决方案:
添加
更多信息:https://developer.mozilla。 org/en-US/docs/CSS/background-clip
感谢@medoingthings
Best CSS only solution:
Add
More information: https://developer.mozilla.org/en-US/docs/CSS/background-clip
Thanks to @medoingthings
表{边框间距:0; 边界崩溃:崩溃; } /* 适用于 FF 3.5 */
table { border-spacing: 0; border-collapse: collapse; } /* works in FF 3.5 */
在 FF 31 中对我不起作用。由于我的 thead 和 tbody 单元格的颜色不同,因此表格背景颜色技巧也不起作用。 唯一的解决方案如下:
wasn't working for me in FF 31. Since i've different colors for thead and tbody cells the table background-color trick wasn't working, too. The only solution was the following:
我今天就被这个咬了。 提供的解决方法对我来说不起作用,所以我找到了自己的解决方法:
这样,您就可以获得折叠边框,没有双边框,并且一切都在您想要的边界内,而无需特定于浏览器的规则。 没有缺点。
I was bitten by this today. The offered work-arounds didn't work for me, so I found my own:
This way, you get collapsed borders, no double borders, and everything within the boundaries you desire, without browser-specific rules. No drawbacks.
我想我从来没有听说过“向左 1px 的错误”,您应该将代码上传到某个地方,以便我们可以检查它并确保它不是“我在某处遗漏了某些东西”错误:)我也建议使用 Firebug 运行您的标记以确定是否还有其他问题。
I don't think I've ever heard of a "1px to the left bug," you should upload your code someplace so we can check it and make sure it's not an "I missed something somewhere" bug :) I would also suggest running through your markup with Firebug to determine if there is anything else going awry.
我遇到了这个问题 - 但就我而言,它与嵌套在设置为溢出:隐藏的 div 中的表有关。 我删除了这个并且它起作用了。
I ran into this problem - but in my case it had to do with the table being nested within a div set to overflow:hidden. I removed this and it worked.
遇到这个问题并作为解决方法。 我使用:
基本上通过使用背景颜色来欺骗边框。 这从而防止了双重内部边界。
Ran into this issue and as a work around. I used :
basically cheated the border by using a background color. Which thus prevented double inside borders.
我的解决方案如下。
border-collapse
、border
和border-spacing
。添加此 JavaScript 代码:
说实话,我不知道它为什么起作用。 这是 jQuery 的魔力。
My solution is as follows.
border-collapse
,border
andborder-spacing
from CSS.Add this JavaScript code:
To be honest, I don't know why it works. It's jQuery magic.
我也遇到了这个问题,完整的证明解决方案在你的 asp:gridview 中非常简单,只需添加
,线条就会在 Firefox 中消失,不需要 css 修改。
I also have run into this problem the full proof solution is very simple in your asp:gridview just add
and the lines disappear in Firefox no css modification needed.