【问题标题】:Flexible column widths in table with scrollable body具有可滚动正文的表格中的灵活列宽
【发布时间】:2020-07-07 01:54:58
【问题描述】:

我目前有以下实现,我希望表格的主体可以滚动。但是,列宽都是相等的,我试图弄清楚如何根据内容实现灵活的表格单元格宽度,同时保持正文的滚动。想法?

.modal {
  height: 300px;
}

.first {
  height: 100%;
}

table.list {
  border-collapse: collapse;
  width: 100%;
  height: 100%;
}

thead,
tbody tr {
  display: table;
  width: 100%;
  table-layout: fixed;
}

thead {
  font-weight: bold;
  border-bottom: 1px solid #ccc;
  height: 40px;
}

th {
  padding: 0 $now-global-space--sm 0 $now-global-space--md;
  text-align: left;
  word-break: break-word;
  hyphens: auto;
}

tbody {
  overflow: auto;
  display: block;
  height: calc(100% - 40px);
}

tr {
  line-height: 1.5;
  border: none;
  border-collapse: none;
  min-height: 40px;
}

td {
  min-height: 40px;
  min-width: 40px;
  padding: 4px;
  text-align: left;
  word-break: break-word;
  hyphens: auto;
}

tr:nth-child(2n) td {
  background-color: red;
}

tr:hover td {
  background-color: pink;
}
<div class="modal">
  <div class="first">
    <table class="list" role="grid">
      <thead>
        <tr role="row">
          <th role="columnheader">Date</th>
          <th role="columnheader">Action</th>
          <th role="columnheader">Comment</th>
        </tr>
      </thead>
      <tbody>
        <tr role="row">
          <td role="gridcell">Mar 24</td>
          <td role="gridcell">somoene</td>
          <td role="gridcell">ddddd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 23</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">ddddd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 22</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">dd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 21</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 20</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asdasdsadasdsa</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 14</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asdasdsadsadsadsa</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 7</td>
          <td role="gridcell">lala</td>
          <td role="gridcell">asdasdsadsa</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

【问题讨论】:

  • 如果您希望评论栏中的内容最多,您可以分配一个可以为其指定宽度的类

标签: css


【解决方案1】:

javascriptdispay:grid/contents 可能是选项,但包装器和position:sticky 可能是避免破坏表格布局的折衷方案,下面是这个想法的演示

.modal {
  height: 300px;
  border:solid;
}

.first {
  height: 100%;
  position: relative;
  overflow: auto;
}

table.list {
  border-collapse: collapse;
  width: 100%;
  height: 100%;
}

thead {
  font-weight: bold;
  border-bottom: 1px solid #ccc;
  height: 40px;
}

thead tr {
  position: sticky;
  top: 0;
  background: white;
  box-shadow: 0 0 5px;
}

th {
  text-align: left;
  word-break: break-word;
  hyphens: auto;
}

tr {
  line-height: 1.5;
}

td {
  height: 40px;
  width: 40px;
  padding: 4px;
  text-align: left;
  word-break: break-word;
  hyphens: auto;
}

tr:nth-child(2n) td {
  background-color: red;
}

tr:hover td {
  background-color: pink;
}
<div class="modal">
  <div class="first">
    <table class="list" role="grid">
      <thead>
        <tr role="row">
          <th role="columnheader">Date</th>
          <th role="columnheader">Action</th>
          <th role="columnheader">Comment</th>
        </tr>
      </thead>
      <tbody>
        <tr role="row">
          <td role="gridcell">Mar 24</td>
          <td role="gridcell">somoene</td>
          <td role="gridcell">ddddd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 23</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">ddddd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 22</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">dd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 21</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 20</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asdasdsadasdsa</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 14</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asdasdsadsadsadsa</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 24</td>
          <td role="gridcell">somoene</td>
          <td role="gridcell">ddddd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 23</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">ddddd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 22</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">dd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 21</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asd</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 20</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asdasdsadasdsa</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 14</td>
          <td role="gridcell">someone</td>
          <td role="gridcell">asdasdsadsadsadsa</td>
        </tr>
        <tr role="row">
          <td role="gridcell">Mar 7</td>
          <td role="gridcell">lala</td>
          <td role="gridcell">asdasdsadsa</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 2015-12-25
    • 2011-02-14
    • 1970-01-01
    • 2015-08-09
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多