【问题标题】:CSS only scrollable table body [duplicate]仅 CSS 可滚动表体 [重复]
【发布时间】:2019-08-21 01:43:03
【问题描述】:

我想使用 CSS 仅滚动表格正文。我找到了一些示例,但如果表格行长度不同,它们就无法正常工作。

我从这个例子开始:

https://codepen.io/pdg/pen/MayBJK

HTML 代码:

<div class="container">
  <h1>CSS only scrollable table body</h1>
  <p>by PDG</p>
<table class="table-scroll small-first-col">
  <thead>
    <tr>
    <th>Head 1</th>
    <th>Head 2</th>
    <th>Head 3</th>
    <th>Head 4</th>
    </tr>
  </thead>
  <tbody class="body-half-screen">
    <tr>
      <td>1</td>
      <td>First row</td>
      <td>3 dsad sad sad sad sad sadsadsad sadasd asdsa</td>
      <td>4 dasd sad asdsad sad sadsa dasdsad</td>
    </tr>
    <tr>
      <td>1 dasd asd sadsadsad sadsad</td>
      <td>2dsadsadsa dsad sadasd sad sad sa</td>
      <td>A very long cell content comes here, just to test it all!!!</td>
      <td>4</td>
    </tr>
    <tr>
      <td>1</td>
      <td>2 dsad asd asd sad sad sad asdsadsad</td>
      <td>3</td>
      <td></td>
    </tr>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>There is an empty cell above!</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr><tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td> Last row</td>
    </tr>
  </tbody>
</table>
  <h3>Notes</h3>
  <p>The table uses flex properties to arrange the cell sizes, however, this tends to make them the same as "fixed width". To fix that, you need to create additional classes that target the <strong>td:nth-child()</strong> and <strong>th:nth-child()</strong> with a specific <strong>flex-basis</strong> property.</p>
  <p>The <strong>height</strong> of the body also must be fixed and cannot be percentages (in the example above I use "vh")</p>
</div>

CSS 代码:

.container{
  padding: 1rem;
  margin: 1rem;
}
.table-scroll{
  /*width:100%; */
  display: block;
  empty-cells: show;

  /* Decoration */
  border-spacing: 0;
  border: 1px solid;
}

.table-scroll thead{
  background-color: #f1f1f1;  
  position:relative;
  display: block;
  width:100%;
  overflow-y: scroll;
}

.table-scroll tbody{
  /* Position */
  display: block; position:relative;
  width:100%; overflow-y:scroll;
  /* Decoration */
  border-top: 1px solid rgba(0,0,0,0.2);
}

.table-scroll tr{
  width: 100%;
  display:flex;
}

.table-scroll td,.table-scroll th{
  flex-basis:100%;
  flex-grow:2;
  display: block;
  padding: 1rem;
  text-align:left;
}

/* Other options */

.table-scroll.small-first-col td:first-child,
.table-scroll.small-first-col th:first-child{
  flex-basis:20%;
  flex-grow:1;
}

.table-scroll tbody tr:nth-child(2n){
  background-color: rgba(130,130,170,0.1);
}

.body-half-screen{
  max-height: 50vh;
}

.small-col{flex-basis:10%;}

这似乎是个好主意,在他们的演示中一切看起来都很好,但是在一个更复杂的示例中,您可以看到列的垂直对齐不是很好。请参阅下面的示例:

https://jsfiddle.net/pnfu321g/

我还尝试了一个不同的例子:http://jsfiddle.net/hashem/CrSpu/555/

但在我的情况下,这也不适用于具有不同宽度的行的复杂表。

请提出一个 css 解决方案(无 javascript),该解决方案将使包含任意大小行的表格看起来都不错。

更新

其他解决方案对我不起作用的原因是因为我使用的是旧版本的 Mozilla Firefox - 57。这就是为什么我测试的几乎所有东西都不起作用的原因。我将问题标记为重复。

【问题讨论】:

  • 这实际上不再像一个表格,因为您已经更改了它所有元素的显示类型。在普通表格中,列中的所有单元格都具有相同的宽度,但在这里您已明确地进行了设置,以便单元格在列中具有不同的宽度。这里的预期结果是什么?您希望细胞如何排列?
  • 我希望得到以下结果:如果单元格宽度过长,文本将中断并落在两三行,单元格宽度将与其他单元格对齐。所以表格行高会增加,但单元格长度会相似。

标签: html css


【解决方案1】:

您可以在表头内的th 元素上使用position:sticky

基本示例(CSS):

table.sticky-headers { width:100%; }
table.sticky-headers thead tr th { text-align:left; position:sticky; top:0; background:#fff; }

基本标记(添加更多行以获得滚动条):

<table class="sticky-headers">
    <thead>
        <tr>
            <th>Head 1</th>
            <th>Head 2</th>
            <th>Head 3</th>
            <th>Head 4</th>
            <th>Head 5</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Content 1</td>
            <td>Content 2</td>
            <td>Content 3</td>
            <td>Content 4</td>
            <td>Content 5</td>
        </tr>
   </tbody>
</table>

这里的工作示例:http://jsfiddle.net/yp1xb7dj/

【讨论】:

  • 请在此示例中添加粘性位置:jsfiddle.net/pnfu321g。我尝试将它添加到“th”和“td”元素,但“td”元素仍然没有很好地垂直对齐。
  • @Pascut 您应该删除所有display 属性和flex 相关属性,并为表格或单元格设置最大宽度。文本将自动换行。
  • 我会用“最大宽度”尝试你的建议。感谢您的建议。
  • 那里发生了很多事情,所有的 flexbox 属性是什么?您需要桌体有最大高度吗?我将废弃所有 CSS 并从头开始,以我的示例为起点。
  • 我尝试删除“显示”属性并添加“最大宽度”属性。但是如果我删除显示属性,“tbody”就不再可滚动了。请参阅此示例:jsfiddle.net/pnfu321g/1
【解决方案2】:

如果您希望固定单元格宽度,并且内容较多的单元格中的文本会中断并落在两三行,您可以尝试指定单元格宽度,高度将自动固定:

table.sticky-headers thead tr th {
    width: 20%;
    text-align:left; 
    position:sticky; 
    top:0; 
    background:#fff;
}

结果:

【讨论】:

  • 这是我能做到的最好了,否则我想我不明白你的意思jsfiddle.net/q0x78yw4/1
  • 试试这个jsfiddle.net/bq7d05m4
  • 我在 Mozilla Firefox 上进行了测试,您的示例仅适用于 Chrome。它运行良好,但我希望该解决方案也能在 Mozilla 和 Safari 上运行。
  • 我刚刚在 Mozilla 上进行了测试,它运行良好。 prntscr.com/ov156z
  • 我使用的是旧版本的 Mozilla:57,是的,它在最新版本上运行良好。非常感谢,我接受了你的回答,认为是最好的!感谢您的宝贵时间。
猜你喜欢
  • 2023-03-29
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 2015-07-03
  • 2013-01-27
  • 2017-09-21
  • 2018-11-12
相关资源
最近更新 更多