【发布时间】:2023-03-09 20:49:01
【问题描述】:
我有一些表格(因为它们在 vue 组件中被扭曲,我不能将它们放在一个表格中),其中表格单元格需要在使用各种边框时对齐。在 Firefox 上,使用 box-sizing: border-box 效果很好,但在 Chrome 或 Edge Chromium 上相同的代码会错位。
这是我的代码:
<table>
<tr>
<td class="cell-th2-4"></td>
<td class="cell-th2-4"></td>
<td class="cell-th2-3"></td>
</tr>
</table>
<table>
<tr>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4 vertical_separator"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4 vertical_separator"></td>
<td class="cell4"></td>
<td class="cell4"></td>
</tr>
<tr>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4 vertical_separator"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4"></td>
<td class="cell4 vertical_separator"></td>
<td class="cell4"></td>
<td class="cell4"></td>
</tr>
</table>
这是我的 CSS:
.vertical_separator {
border-left: 2px solid;
}
.cell-th2-4 {
min-width: 3.6em;
text-align: center;
box-sizing: border-box;
border-left: 2px solid;
border-right: 2px solid;
}
.cell-th2-3 {
min-width: 2.7em;
text-align: center;
box-sizing: border-box;
border-left: 2px solid;
border-right: 2px solid;
}
.cell4 {
min-width: 0.9em;
text-align: center;
box-sizing: border-box;
}
.cell4:first-child {
border-left: 2px solid;
}
.cell4:last-child {
border-right: 2px solid;
}
table {
border-spacing: 0;
border-collapse: collapse;
table-layout: fixed;
box-sizing: border-box;
}
td {
padding: 0;
border: 1px solid;
overflow: hidden;
box-sizing: border-box;
}
tr {
white-space: nowrap;
height: 1.5em;
box-sizing: border-box;
}
这是我的JsFiddle - 如果您在 Firefox 中打开它,它会很好对齐:
我在 Chrome/Chromium Edge 中打开的列会像这样错位:
对此我能做些什么?
【问题讨论】:
-
当我使用 firefox 和 Chrome/Chromium Edge 时,我没有发现任何区别。您能否分享一下您观察到的差异的屏幕截图?
-
我认为它与边框折叠有关。也许这个答案有用stackoverflow.com/questions/19068909/…
-
哈,单元格大小也有差异,但这可能是由于我的浏览器默认字体大小设置所致。调查这个.....