【问题标题】:HTML/CSS table with fixed width column (and scroll if overflowing content)具有固定宽度列的 HTML/CSS 表格(如果内容溢出则滚动)
【发布时间】:2019-07-23 16:18:59
【问题描述】:

我有一个包含一个或多个固定宽度列的 HTML 表格。 有时这样一列的内容超过了列宽,我想在表格列的底部有一个滚动条。

  • 我想保持相同的语义 HTML 结构并且没有重复的表或多次迭代。
  • 我希望 JavaScript 尽可能少
  • 如果可能的话,我可以将其转换为 CSS Grid 或 Flexbox(但之前没有使用过)

我注意到 HTML 有一个 colgroup,看起来我可以设置 width 和其他 CSS 属性,但不能设置 overflow

<table class="table">
  <colgroup>
    <col class="table-colgroup table-colgroup--first">
    <col class="table-colgroup table-colgroup--second">
    <col class="table-colgroup table-colgroup--third">
  </colgroup>
  <tr class="table-row">
    <td class="table-cell table-cell--first">
      <p>First</p>
    </td>
    <td class="table-cell table-cell--second">
      <p>Second long column item that should have scroll 1</p>
    </td>
    <td class="table-cell table-cell--third">
      <p>Last column item</p>
    </td>
  </tr>
  <tr class="table-row">
    <td class="table-cell table-cell--first">
      <p>First</p>
    </td>
    <td class="table-cell table-cell--second">
      <p>Second long column item that should have scroll 2</p>
    </td>
    <td class="table-cell table-cell--third">
      <p>Last column item</p>
    </td>
  </tr>
  <tr class="table-row">
    <td class="table-cell table-cell--first">
      <p>First</p>
    </td>
    <td class="table-cell table-cell--second">
      <p>Second long column item that should have scroll 3</p>
    </td>
    <td class="table-cell table-cell--third">
      <p>Last column item</p>
    </td>
  </tr>
  <tr class="table-row">
    <td class="table-cell table-cell--first">
      <p>First</p>
    </td>
    <td class="table-cell table-cell--second">
      <p>Second long column item that should have scroll 4</p>
    </td>
    <td class="table-cell table-cell--third">
      <p>Last column item</p>
    </td>
  </tr>
  <tr class="table-row">
    <td class="table-cell table-cell--first">
      <p>First</p>
    </td>
    <td class="table-cell table-cell--second">
      <p>Second long column item that should have scroll 5</p>
    </td>
    <td class="table-cell table-cell--third">
      <p>Last column item</p>
    </td>
  </tr>
  <tr class="table-row">
    <td class="table-cell table-cell--first">
      <p>First</p>
    </td>
    <td class="table-cell table-cell--second">
      <p>Second long column item that should have scroll 6</p>
    </td>
    <td class="table-cell table-cell--third">
      <p>Last column item</p>
    </td>
  </tr>
</table>

.table {
  table-layout: fixed;
  border-collapse: collapse;
  width: 100%;
}

.table-row {
  box-shadow: 0 0 1px 1px yellowgreen;
}

.table-cell {
  box-shadow: 0 0 1px 1px blue;
}

.table-colgroup--first {
  background: palegoldenrod;
}

.table-colgroup--second {
  background: paleturquoise;
  /*
   * I would like to pass the width of the column here
   */
  width: 50px;
  overflow-x: scroll;
}

.table-colgroup--third {
  background: palegreen;
} 

/* This is intentionally long so that a scrollbar appears somewhere */
.table-cell--second p {
  width: 900px;
  background: #666;
  color: #fff;
}

请检查我的 Codepen 示例与我尝试过的内容:Code sample

【问题讨论】:

    标签: html css html-table grid


    【解决方案1】:

    您只需要使用 overflow-x auto 将表格单元格的内容包装在一个 div 中,这将在 td 的内容大于单元格时触发滚动。

    <td><div style="overflow-x: 'scroll'"><p>long line here</p></div>
    

    【讨论】:

    • 虽然此代码可能会解决问题,包括 explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请编辑您的答案以添加解释,并说明适用的限制和假设。
    【解决方案2】:

    在table标签之前,取一个父div并放入style overflow-x:auto;

    <div class="table-responsive">
      <table class="table">
         enter code here
         enter code here
       </table>
    </div>
    .table-responsive{
    overflow-x:auto;
    }
    

    【讨论】:

    • 谢谢,杰!不确定您是否阅读了问题的完整描述:我只想在该列上溢出,而不是整个表。在我看来,这就像在整个桌子上广告溢出一样。
    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多