【问题标题】:Inlining HTML table cells in sentence在句子中内联 HTML 表格单元格
【发布时间】:2021-07-11 18:48:30
【问题描述】:

有没有办法让 HTML 表格单元格在正常文本流中内联,并使用 CSS 使文本出现在周围文本的基线处?

简单地使所有表格元素内联会使文本向下移动,如下例所示,这是不可取的。

table,
table > tbody,
table > tbody > tr,
table > tbody > tr > td {
    display: inline;
}

table > tbody > tr > td {
     padding: 0px;
     margin: 0px;
}
<div style="width: 40em;">
  A table containing
  <table>
    <tbody>
      <tr>
        <td>possibly multiple cells</td>
      </tr>
      <tr>
        <td>containing potentially a high amount of text that should not be shifted down</td>
      </tr>
    <tbody>
  </table>
  below the baseline.
</div>

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

    要实现您想要做的事情,您应该将vertical-align: baseline; 添加到每个有问题的表格元素中。

    此外,您不需要使用祖先层次结构来定位每个元素。这应该足够了:

    table,
    tbody,
    tr,
    td {
        display: inline;
        padding: 0;
        vertical-align: baseline;
    }
    <div style="width: 40em;">
      A table containing
      <table>
        <tbody>
          <tr>
            <td>possibly multiple cells</td>
          </tr>
          <tr>
            <td>containing potentially a high amount of text that should not be shifted down</td>
          </tr>
        <tbody>
      </table>
      below the baseline.
    </div>

    【讨论】:

    • 我也删除了保证金规则,因为它没有做任何事情。默认情况下,表格元素没有任何边距(至少在 chrome 中没有)。
    • 谢谢!我完全忽略了baseline 属性值。
    • 关于边距规则:我通常将它成对添加到填充规则中,以避免考虑是否设置了这两个规则中的一个,以防万一仍然是不希望的。但事实上,这可能是多余的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多