我想固定表格的标题。表格位于可滚动div中。下面是我的代码。
<div id="table-wrapper"> <div id="table-scroll"> <table bgcolor="white" border="0" cellpadding="0" cellspacing="0" id="header-fixed" width="100%" overflow="scroll" class="scrollTable"> <thead> <tr> <th>Order ID</th> <th>Order Date</th> <th>Status</th> <th>Vol Number</th> <th>Bonus Paid</th> <th>Reason for no Bonus</th> </tr> </thead> <tbody> <tr> <td><%=snd.getOrderId()%></td> <td><%=snd.getDateCaptured()%></td> <td><%=snd.getOrderStatus()%></td> <td>Data Not Available</td> <td>Data Not Available</td> <td>Data Not Available</td> </tr> </tbody> </table> </div> </div>
以下是我用于以上div的CSS:
#table-wrapper { position:relative; } #table-scroll { height:250px; overflow:auto; margin-top:20px; } #table-wrapper table { width:100%; } #table-wrapper table * { background:white; color:black; } #table-wrapper table thead th .text { position:absolute; top:-20px; z-index:2; height:20px; width:35%; border:1px solid red; }
怎么做这样的事情?我从头开始…
我所做的是使用2个表,一个表用于标题,该表始终是静态的,而另一个表呈现单元格,我使用div固定高度的元素包装了这些单元格,并启用了滚动,overflow-y: auto;
div
overflow-y: auto;
还要确保您使用table-layout: fixed;固定宽度的td元素,以便在table使用不带字符串的情况下不会断线white space,以便在使用时断开该字符串word-wrap: break-word;
table-layout: fixed;
td
table
white space
word-wrap: break-word;
.wrap { width: 352px; } .wrap table { width: 300px; table-layout: fixed; } table tr td { padding: 5px; border: 1px solid #eee; width: 100px; word-wrap: break-word; } table.head tr td { background: #eee; } .inner_table { height: 100px; overflow-y: auto; } <div class="wrap"> <table class="head"> <tr> <td>Head 1</td> <td>Head 1</td> <td>Head 1</td> </tr> </table> <div class="inner_table"> <table> <tr> <td>Body 1</td> <td>Body 1</td> <td>Body 1</td> </tr> <!-- Some more tr's --> </table> </div> </div>