简体   繁体   English

修复了表 header 无法正常工作并将 header 堆叠在一起的问题

[英]Fixed Table header not working and stacking the header on top of one another

All of the header elements of my table are stacked on top of one another when the <table> 's position is fixed.<table>的 position 固定时,我表格的所有 header 元素都堆叠在一起。

How can I fix this issue so that all the table header elements will be aligned properly with column property?我该如何解决这个问题,以便所有表 header 元素都与列属性正确对齐?

Note.笔记。 I do not want to use sticky and only looking of a solution with Fixed我不想使用粘性和只寻找Fixed的解决方案

 table { /* border: 5px solid red; */ display: flex; flex-flow: column; width: 100%; } thead { flex: 0 0 auto; } tbody { flex: 1 1 auto; display: block; overflow-y: auto; overflow-x: hidden; } th { text-align: left; position: fixed; } tr { width: 100%; display: table; table-layout: fixed; }
 <table> <thead> <tr> <th>Account No.</th> <th>Date</th> <th>Transaction Details</th> <th>Value date</th> <th>details</th> <th>Deposit AMT</th> <th>Note</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data:This is test data;This is test data !</td> <td>test</td> <td>test</td> </tr> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data !This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data !This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data !This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> </tbody> </table> <div style="height: 2000px;"></div>

  1. I have changed display of table to block which will help the table to be aligned horizontally center.我已将表格的显示更改为block ,这将有助于表格水平居中对齐。

  2. I have given position: sticky; top: 0我给了position: sticky; top: 0 position: sticky; top: 0 to thead tr which will make it stick to top when scrolled. position: sticky; top: 0 to thead tr滚动时它会粘在顶部。

  3. I have given min-width: 100px;我给了min-width: 100px; to th, td which will set the minimum width of each cell and will make sure that the width of the cells of the header matches the width of the cells of the body. to th, td 这将设置每个单元格的最小宽度,并确保 header 的单元格宽度与正文单元格的宽度相匹配。

Last, I have removed flex-flow: column;, display: table; and table-layout: fixed;最后,我删除了flex-flow: column;, display: table; and table-layout: fixed; flex-flow: column;, display: table; and table-layout: fixed; as they are not required.因为它们不是必需的。 I have set overflow-y: auto; and overflow-x: hidden;我设置了overflow-y: auto; and overflow-x: hidden; overflow-y: auto; and overflow-x: hidden; on tbody element to make the body scrollable if the content is overflowing.如果内容溢出,则在 tbody 元素上使主体可滚动。

 table { /* border: 5px solid red; */ display: block; width: 100%; } thead tr { position: sticky; top: 0; } tbody { overflow-y: auto; overflow-x: hidden; } th, td { min-width: 100px; text-align: left; }
 <.--The content below is only a placeholder and can be replaced.--> <table> <thead> <tr> <th>Account No:</th> <th>Date</th> <th>Transaction Details</th> <th>Value date</th> <th>details</th> <th>Deposit AMT</th> <th>Note</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data;This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data !This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data !This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> <tr> <td>1</td> <td>10-10-2023</td> <td>ALJDFLDOI 4584</td> <td>10-10-2023</td> <td>This is test data !This is test data !This is test data !</td> <td>test</td> <td>test</td> </tr> </tbody> </table> <div style="height: 2000px;"></div>

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

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