简体   繁体   English

具有固定宽度列的HTML / CSS表(如果内容溢出,则滚动)

[英]HTML/CSS table with fixed width column (and scroll if overflowing content)

I have an HTML table with one or multiple fixed-width columns. 我有一个带有一个或多个固定宽度列的HTML表。 Sometimes the content of such a column exceeds the column width and I would like to have a scrollbar at the bottom of the table column. 有时,此类列的内容超出了列的宽度,我想在表格列的底部使用一个滚动条。

  • I would like to maintain the same semantic HTML structure and not have duplicate tables or multiple iterations. 我想保持相同的语义HTML结构,并且没有重复的表或多次迭代。
  • I would like to have as little JavaScript as possible 我想要尽可能少的JavaScript
  • I can turn this into a CSS Grid or Flexbox if that is possible there (but haven't used that before) 如果可以的话,我可以将其转换为CSS Grid或Flexbox(但以前没有使用过)

I've noticed that is HTML there is a colgroup and it looks like I can set the width and other CSS properties, but not overflow . 我已经注意到,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;
}

Please check my Codepen example with what I've tried: Code sample 请使用我尝试过的代码检查我的Codepen示例: 代码示例

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

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

Before table tag , take a parent div and put style overflow-x:auto; 在table标记之前,先获取父div并放置样式溢出-x:auto;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM