gpt4 book ai didi

html - 强制表格内的表格填充剩余的屏幕高度

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:13 26 4
gpt4 key购买 nike

我在一张 table 中有一张 table 。我希望内表将剩余空间填充到屏幕底部。如果表格中的行太多而无法全部显示在屏幕上,我希望表格有一个垂直滚动条。整个页面不应有垂直滚动条。

我可以向元素添加 ID 或类,但一般的 HTML 需要尽可能保持不变。

https://jsfiddle.net/4rv9htau/2/

前一段时间我使用 height 100% 和固定像素值来解释其他元素的额外高度,但我想避免这种情况,这样我就不必担心在其他元素的大小发生变化时更改它 future 。

html,
body,
table,
tbody,
tr {
height: 100%;
margin: 0;
}

.MainCell {
display: block;
}

table div:last-child {
max-height: 250px;
overflow-x: hidden;
overflow-y: auto;
}

html,
body {
background-color: red;
}

table {
background-color: blue;
}

table td div:first-child {
background-color: green;
}

table td div:last-child,
table table {
background-color: yellow;
}
<table>
<tbody>
<tr>
<td class="MainCell">
<div>
<input type="submit" name="btnBack" value="Back" id="btnBack">
<input type="submit" name="btnDelete" value="Delete" id="btnDelete">
<input type="submit" name="btnCut" value="Cut" id="btnCut">
</div>
<div>
<table style="border-collapse:collapse;">
<thead>
<tr>
<th>COL #1</th>
<th>COL #2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>

最佳答案

使用vh

<td>
<div>...</div><!-- First div -->
<div>...</div><!-- Second div -->
</td>
  1. 为第一个 div 设置固定高度。
  2. 第二个div的高度:calc(100vh - fixed height)

html,
body,
table,
tbody,
tr {
height: 100%;
margin: 0;
}

html,
body {
background-color: red;
}

table {
border-collapse: collapse;
}

.MainCell {
display: block;
padding: 0;
}

.MainCell>div:first-child {
/* Center the inputs inside the div horizontally and vertically with */
display: flex;
justify-content: center;
align-items: center;
height: 30px;
margin: 0;
padding: 0;
}

.MainCell>div:nth-child(2) {
max-height: calc(100vh - 30px);
}

table div:last-child {
max-height: 250px;
overflow-x: hidden;
overflow-y: auto;
}

table {
//background-color: blue;
}

table td div:first-child {
background-color: green;
}

table td div:last-child,
table table {
background-color: yellow;
}
<table>
<tbody>
<tr>
<td class="MainCell">
<div>
<input type="submit" name="btnBack" value="Back" id="btnBack">
<input type="submit" name="btnDelete" value="Delete" id="btnDelete">
<input type="submit" name="btnCut" value="Cut" id="btnCut">
</div>
<div>
<table style="border-collapse:collapse;">
<thead>
<tr>
<th>COL #1</th>
<th>COL #2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
<tr>
<td>Some val #1</td>
<td>Some val #1</td>
</tr>
<tr>
<td>Some val #2</td>
<td>Some val #2</td>
</tr>
<tr>
<td>Some val #3</td>
<td>Some val #3</td>
</tr>
<tr>
<td>Some val #4</td>
<td>Some val #4</td>
</tr>
<tr>
<td>Some val #5</td>
<td>Some val #5</td>
</tr>
<tr>
<td>Some val #6</td>
<td>Some val #6</td>
</tr>
<tr>
<td>Some val #7</td>
<td>Some val #7</td>
</tr>
<tr>
<td>Some val #8</td>
<td>Some val #8</td>
</tr>
<tr>
<td>Some val #9</td>
<td>Some val #9</td>
</tr>
<tr>
<td>Some val #10</td>
<td>Some val #10</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>

编辑

设置.MainCell { padding: 0 } table { border-collapse: collapse}来防止整个页面的垂直滚动条。

关于html - 强制表格内的表格填充剩余的屏幕高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56936789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com