I am creating a bootstrap 5 page, but facing a challenge to stretch two child DIVs to the same height as the parent. Can someone help me please?
Here is the HTML:
https://jsfiddle.net/og7pz9sq
In the above fiddle, I am trying to make the light-green and light-yellow DIVs to occupy the while white area. Footer comes just after this white area, and that will always remains at the bottom of the page.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<title>Bootstrap</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>
<div id="wholepage"
style="min-height: 90dvh;">
<div id="header">
<div class="p-3 bg-primary text-white text-center">
Bootstrap 5 Page
</div>
</div>
<div id="topmenu">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<div class="container-fluid">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active"
href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">Link</a>
</li>
</ul>
</div>
</nav>
</div>
<div id="sidebar-and-contents">
<div class="row">
<div id="sidebar"
class="col-md-auto"
style="display: inline;">
<ul class="nav nav-pills flex-column"
style="background-color: rgb(172, 201, 135);">
<li class="nav-item">
<a class="nav-link"
href="#">Link-Left1</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="#">Link-Left2</a>
</li>
</ul>
</div>
<div id="content"
class="col"
style="background-color: rgb(241, 240, 172);">
<div>Main area for dynamic contents based on menu selections</div>
</div>
</div>
</div>
</div>
<div id="footer"
class="mt-5 p-4 bg-dark text-white text-center">
<p>Footer</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>
2
Answers
The solution is to push away from
#wholepage
and use flexbox to stretch the descendants.Somehow:
You can consider using a vertical flex box to fix the footer to the end of the page and make the middle section grow to fill the spaces.
d-flex flex-column
is added to the two parent container (wholepage
andsidebar-and-contents
).Since you are using a media query break point
md
for a different layout, consider addingflex-column flex-md-row
in thesidebar-and-contents
container to make sure the content container grows in the correct direction for smaller screen sizes.See below example: (I’ve excluded the extra padding and margins for clearer show case.)