Bootstrap 4 Scrollable Div 占用剩余的中间屏幕高度
Bootstrap 4 Scrollable Div take up remaining middle screen height
我正在尝试设计一个带有 Bootstrap 4 和 6 个 div 的页面,我需要 DIV #4 占据剩余高度并可滚动。
Divs 1-3会略微动态,不是固定高度。 Div 5 和 6 是一样的。 Div 4 应该会占据屏幕的剩余部分,但内容可能会更长,因此需要滚动。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" crossorigin="anonymous"></script>
<style>
html, body {
height: 100%;
margin: 0px;
}
</style>
</head>
<body>
<div class="d-flex flex-column h-100">
<div style="background-color: #000000; width:100%; margin: 0 auto; color: white;">
VARIABLE CONTENT
</div>
<div style="background-color: blue; width:100%; margin: 0 auto;">
<table width="100%">
<tr>
<td style="color: white;">Firstname LastName</td>
<form name="RankingForm" id="RankingForm">
<td>
<select name="Ranking" id="Ranking">
<option>
</select>
</td>
</form>
</tr>
</table>
</div>
<div style="background-color: cyan; width:100%; margin: 0 auto;" class="flex-grow-0">
CONTENT
</div>
<div class="flex-grow-1" style="width:100%; overflow:scroll; margin: 0 auto;">
<div style="width:100%;">
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
</div>
</div>
<div class="w-100" style="background-color: #d0d0d0; margin: 0 auto;">
CONTENT
</div>
</body>
</html>
Generated UI, where bottom div is not at the bottom
如有任何帮助,我们将不胜感激。
如评论所示,flexbox
是您想要制作的三明治风格。但是,我相信它是在 4.1 之后引入的。因此,此解决方案仅在您的 bootstrap 版本 >= 4.1.
时有效
你的代码有一些错误。您正在使用内联样式作为 class 这样 class="flex-grow: 0"
,您可以将它们放入 style="flex-grow: 0"
或 class="flex-grow-0"
.
要保持最后 <div>
或任何你想要的 <div>
的固定高度,你可以给它一个 height: somenumberhere px
或 height: somenumberhere vh
.
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<style>
html, body {
height: 100%;
}
</style>
</head>
<body>
<div class="d-flex flex-column h-100">
<div style="background-color: #000000; width:100%; margin: 0 auto; color: white;">
VARIABLE CONTENT
</div>
<div style="background-color: blue; width:100%; margin: 0 auto;">
<table width="100%">
<tr>
<td style="color: white;">Firstname LastName</td>
<form name="RankingForm" id="RankingForm">
<td>
<select name="Ranking" id="Ranking">
<option>
</select>
</td>
</form>
</tr>
</table>
</div>
<div style="background-color: cyan; width:100%; margin: 0 auto;" class="flex-grow: 0">
CONTENT
</div>
<div class="flex-grow-1" style="width:100%; overflow:scroll; margin: 0 auto;">
<div style="width:100%;">
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
</div>
</div>
<div class="w-100" style="background-color: #d0d0d0; margin: 0 auto;">
CONTENT
</div>
</body>
我正在尝试设计一个带有 Bootstrap 4 和 6 个 div 的页面,我需要 DIV #4 占据剩余高度并可滚动。
Divs 1-3会略微动态,不是固定高度。 Div 5 和 6 是一样的。 Div 4 应该会占据屏幕的剩余部分,但内容可能会更长,因此需要滚动。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" crossorigin="anonymous"></script>
<style>
html, body {
height: 100%;
margin: 0px;
}
</style>
</head>
<body>
<div class="d-flex flex-column h-100">
<div style="background-color: #000000; width:100%; margin: 0 auto; color: white;">
VARIABLE CONTENT
</div>
<div style="background-color: blue; width:100%; margin: 0 auto;">
<table width="100%">
<tr>
<td style="color: white;">Firstname LastName</td>
<form name="RankingForm" id="RankingForm">
<td>
<select name="Ranking" id="Ranking">
<option>
</select>
</td>
</form>
</tr>
</table>
</div>
<div style="background-color: cyan; width:100%; margin: 0 auto;" class="flex-grow-0">
CONTENT
</div>
<div class="flex-grow-1" style="width:100%; overflow:scroll; margin: 0 auto;">
<div style="width:100%;">
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
</div>
</div>
<div class="w-100" style="background-color: #d0d0d0; margin: 0 auto;">
CONTENT
</div>
</body>
</html>
Generated UI, where bottom div is not at the bottom
如有任何帮助,我们将不胜感激。
如评论所示,flexbox
是您想要制作的三明治风格。但是,我相信它是在 4.1 之后引入的。因此,此解决方案仅在您的 bootstrap 版本 >= 4.1.
你的代码有一些错误。您正在使用内联样式作为 class 这样 class="flex-grow: 0"
,您可以将它们放入 style="flex-grow: 0"
或 class="flex-grow-0"
.
要保持最后 <div>
或任何你想要的 <div>
的固定高度,你可以给它一个 height: somenumberhere px
或 height: somenumberhere vh
.
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<style>
html, body {
height: 100%;
}
</style>
</head>
<body>
<div class="d-flex flex-column h-100">
<div style="background-color: #000000; width:100%; margin: 0 auto; color: white;">
VARIABLE CONTENT
</div>
<div style="background-color: blue; width:100%; margin: 0 auto;">
<table width="100%">
<tr>
<td style="color: white;">Firstname LastName</td>
<form name="RankingForm" id="RankingForm">
<td>
<select name="Ranking" id="Ranking">
<option>
</select>
</td>
</form>
</tr>
</table>
</div>
<div style="background-color: cyan; width:100%; margin: 0 auto;" class="flex-grow: 0">
CONTENT
</div>
<div class="flex-grow-1" style="width:100%; overflow:scroll; margin: 0 auto;">
<div style="width:100%;">
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
<p>VARIABLE CONTENT THAT COULD BE LONG
</div>
</div>
<div class="w-100" style="background-color: #d0d0d0; margin: 0 auto;">
CONTENT
</div>
</body>