【问题标题】:Table cell alignment with rowspan表格单元格与行跨度对齐
【发布时间】:2016-05-11 15:22:29
【问题描述】:

我有一个 HTML 表格,其中包含一个具有行跨度的单元格。基本上只是左边的一个单元格,右边有 rowspan=2 和 2 列。我希望右侧的列粘在顶部。但是,右侧的空间在两列之间平均分配。将 height: 100% 添加到右侧的最后一列在 Chrome 中可以解决问题,但在 FF 或 IE 中不起作用。

<table border="1" style="width: 200px;">
<tr>
    <th>Col1</th>
    <th>Col2</th>
  </tr>
  <tr>
    <td rowspan="2">Some long cell here...The point is that the Col_A and Col_B should both stick to the top. I tried adding height: 100% to the last column (Col_B) but that does only work in Chrome. Firefox puts Col_B at the very bottom while IE seems to completely ignore it.</td>
    <td>Col_A</td>
  </tr>
  <tr>
    <td>Col_B</td>
  </tr>
</table>

小提琴:https://jsfiddle.net/jtr1r132/1/

更新:

正如 Pragnest 在下面建议的那样,将 height:1px 添加到右侧的第一列 (Col_A) 对 Chrome 和 FF 有效。但在 IE(和 Edge)中,它仍然无法按预期工作。

更新小提琴:https://jsfiddle.net/jtr1r132/9

【问题讨论】:

  • 你想把示例 2 中的 Col_B 放在 ff 的顶部,对吧?
  • 我认为你想用 height:100% 解决这个问题,这是 FF 中的问题,但如果你想要解决方案将 td 放在首位,那么在你的问题中提及,否则它将是动态问题。谢谢
  • 如果可能的话,我希望 Col_B 在每个浏览器中都位于顶部 ;-)

标签: html css


【解决方案1】:

在你的情况下,你需要一些替代来实现你的输出。 如下所示,您需要删除第二列中的高度并在第一列中设置 1 px 高度,以便自动实现您的输出。

喜欢下面的 html 更改 –

    <table border="1" style="width: 200px;">
      <tr>
        <th>Col1</th>
        <th>Col2</th>
      </tr>
      <tr>
        <td rowspan="2">Some long cell here...The point is that the Col_A and Col_B should both stick to the top. I tried adding height: 100% to the last column (Col_B) but that does only work in Chrome. Firefox puts Col_B at the very bottom while IE seems to completely ignore it.</td>
        <td style="height: 1px">Col_A</td>
      </tr>
      <tr>
        <td >Col_B</td>
      </tr>
    </table>

【讨论】:

  • @Prgnest,如果您阅读最后一行,您会发现“将高度:100% 添加到右侧的最后一列在 Chrome 中可以解决问题,但在 FF 或 IE 中不起作用。”所以如果你在上面加上一些点会更好。谢谢
  • 是的,我读了那行,但我无法在第二列中找到 100% 高度的所需输出,所以我想用外部方式实现输出,所以我在回答中添加了评论“你需要一些替代方案来实现你的输出”。
  • 感谢您的建议,这在 Chrome 和 FF、IE(或 Edge)中确实有效,但仍然无效。我更新了小提琴:jsfiddle.net/jtr1r132/9
  • 我不知道完美的答案,但是是的,我有一个可以在所有浏览器上运行的解决方案。您需要在 td 中创建一个子级 DOM,并在该子 DOM 上应用高度 1px 来代替 td。喜欢见jsfiddle.net/pragneshkhalas/jxa87ebu
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多