gpt4 book ai didi

html - 使用网格布局 + Bootstrap + 表格溢出时保持页眉/页脚位置

转载 作者:行者123 更新时间:2023-12-04 08:57:14 25 4
gpt4 key购买 nike

鉴于以下页面布局,我需要确保页面本身不可滚动(页面高度 == 100vh),但其中的表格(如果需要)提供滚动。
页眉和页脚应该放在页面的顶部和底部,不受表格内容的影响。意思是,如果表格内容超过可用的垂直空间,它不能向下插入页脚,而是提供原地滚动。
当前布局是 root以 Bootstrap4 开头的 div navbar紧随其后的是 page_container设置一个 grid layout为其内部header , content , footer div。
JSFiddle 的布局包含一个不超过页面高度的表格:https://jsfiddle.net/kvdyg4q5/
JSFiddle 的布局包含超过页面高度的表格:https://jsfiddle.net/1nv7hqg6/
如您所见,表格将页脚向下推。这就是问题所在。
正在使用的 css

<style type="text/css">
#root {
height: 100%;
min-height: 100vh;
max-height: 100vh;
}

.page_container {
max-height: calc(100vh - 70px);
height: calc(100vh - 70px);
min-height: calc(100vh - 70px);
}

.table_container {
display: grid;
height: 100%;
grid-template-rows: auto 1fr auto;
}

.table_body {
display: block;
height: 100%;
overflow-y: scroll;
}
</style>
HTML 布局
<body>
<div id="root">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"><a class="navbar-brand" href="#">Company</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar1</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar2</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar3</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar4</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar5</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar6</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar7</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar8</a></li>

</ul>
</div>
</nav>
<div style="padding: 6px;">
<div class="page_container">
<div class="table_container">
<div>
<h1>HEADER</h1>
</div>
<div>
<div>
<table class="table">
<thead>
<tr>
<th scope="col">Column 1</th>
<th scope="col">Column 2</th>
<th scope="col">Column 3</th>
<th scope="col"> Column 4 </th>
<th scope="col">Column 5</th>
</tr>
</thead>
<tbody class="table_body">
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<!-- more rows...-->

</tbody>
</table>
</div>
</div>
<div>
<h2>Footer</h2>
</div>
</div>
</div>
</div>
</div>

最佳答案

为此,您不需要 CSS 网格,您可以简单地使用 flexbox 并制作内容 flex:1 0 0overflow:auto

[root] {
height: 100vh;
display: flex;
flex-direction: column;
}

[page] {
flex: 1;
display: flex;
flex-direction: column;
}

[content] {
flex: 1 0 0;
overflow: auto;
}


/* Sticky Header */
table {
position: relative;
}

th {
position: sticky;
top: 0;
background: black;
color: white;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<div root>
<div navbar>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"><a class="navbar-brand" href="#">Company</a><button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar1</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar2</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar3</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar4</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar5</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar6</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar7</a></li>
<li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar8</a></li>

</ul>
</div>
</nav>
</div>
<div page>
<div header>
<h1>HEADER</h1>
</div>
<div content>
<div>
<table class="table">
<thead>
<tr>
<th scope="col">Column 1</th>
<th scope="col">Column 2</th>
<th scope="col">Column 3</th>
<th scope="col"> Column 4 </th>
<th scope="col">Column 5</th>
</tr>
</thead>
<tbody class="table_body">
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
<tr>
<td colspan="5">
<h3>Row</h3>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div footer>
<h2>Footer</h2>
</div>
</div>
</div>

关于html - 使用网格布局 + Bootstrap + 表格溢出时保持页眉/页脚位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63753352/

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