如何制作比屏幕尺寸更宽的表格,可滚动并保持标题行固定?
我有一个 表格,有 2 列,每列宽 800 像素。我想在 800x50 窗口中显示该表。所以应该有水平和垂直滚动条来查看完整的表格。
虽然我发现了一些相关的解决方案(这个和这个) 在 SO 上,它们仅在表格宽度小于屏幕尺寸时才起作用。在我的例子中,屏幕尺寸为 1200px,表格总宽度为 1600px。
我怎么能这样做呢?我想实现类似这样的目标。
编辑 糟糕,我忘了添加一项要求。对不起。我希望当用户滚动表格时表格的标题保持固定。
EDIT2
我尝试过下面提到的解决方案来包装在 div 中,但在这种情况下,垂直滚动条不会显示。请参阅带有包装 div 的此表。似乎只有当表格宽度大于屏幕尺寸时才会出现此问题。我正在FF3.6上进行测试。
EDIT3
当前表代码。尽管我可以垂直滚动,但它没有垂直滚动条。
<div style="overflow:scroll; width:800px;height:50px" >
<table style="width:1600px" border="1">
<thead>
<tr>
<th style="width:800px">id_1</th>
<th style="width:800px">id_1</th>
</tr>
</thead>
<tbody style="">
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
</tbody>
</table>
</div>
I've a table with 2 columns and each column is 800px wide. I want to show this table in 800x50 window. So there should be horizontal and vertical scrollbar to view complete table.
While I've found few related solutions (this and this) on SO, they only work if table width is smaller than screen size. In my case screen size is 1200px and total table width is 1600px.
How could I do this? i want to achieve something like this.
EDIT
Oops, I forgot to add one more requirement. Sorry. I want the header of the table to remain fixed while user scrolls table.
EDIT2
I've tried below mentioned solutions to wrap in a div, but in this case vertical scrollbar doesn't show up. Please see this table with wrapper div. It seems this problem only occurs if table width is bigger than screen size. I'm testing on FF3.6.
EDIT3
current table code. This has no vertical scroller even though I can scroll vertically.
<div style="overflow:scroll; width:800px;height:50px" >
<table style="width:1600px" border="1">
<thead>
<tr>
<th style="width:800px">id_1</th>
<th style="width:800px">id_1</th>
</tr>
</thead>
<tbody style="">
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
<tr><td>1200</td><td>1200</td></tr>
</tbody>
</table>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你想让 header 保持固定,你只需做一些 css
If you want the header to remain fixed you just do some css
您可以将表格包装在固定大小 (800x50) 的 div 中并设置
overflow:scroll
。You could wrap the table in a div that has a fixed-size (800x50) and set
overflow:scroll
.将表格放在宽度 < 的 div 中800,并设置
overflow:scroll
。Put your table in a div with width < 800, and set
overflow: scroll
.我不知道它是否有效,但是您是否尝试过为页面设置
max-width: 100%
?I don't know if it would work, but have you tried setting
max-width: 100%
for the page?只需将
overflow:scroll
添加到表格元素的 css 中即可。顺便提一句。您的示例没有显示任何滚动。Just add
overflow:scroll
to the css of your table element. Btw. your example doesn't show any scrolling.