[英]Scrollable table inside div with div having limited maximum height?
I have an absolute positioned div, inside a table is positioned (centered). 我有一个绝对定位的div,在表内定位(居中)。 This table can have 5 rows or 15 rows or more. 该表可以具有5行或15行或更多。 I would like my div (and the table/rows) to be displayed up to a maximum height of X. If that height is reached, the div should take this max height and the table/rows should show a scroll bar, to scroll the table inside the div. 我希望显示div(和表格/行)的最大高度为X。如果达到该高度,则div应采用此最大高度,表格/行应显示滚动条,以滚动div内的表格。
How would i be able to achieve this ? 我将如何实现这一目标?
Using a combination of max-height
on the container and overflow-y: auto
on the table, it's fairly straightforward: 使用容器上的max-height
和表上的overflow-y: auto
的组合,这非常简单:
.special-table { border: 1px solid black; max-height: 100px; overflow-y: auto; } .special-table table { border: 1px solid gray; margin: 0 auto; /* centers the table */ }
<h1>When Everything Fits</h1> <div class="special-table"> <table> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> </table> </div> <h1>When the Table Overflows</h1> <div class="special-table"> <table> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> <tr><th>Foo</th><td>Bar</td></tr> </table> </div>
tbl{
height: 100px;
overflow: scroll;
position: absolute;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.