Trying to make div heights as percentages of the body height. I’m using bootstrap version 5.3. I’m able to make the body element 100% of the view. However, all the other div have the same size of 24px. I’m trying to cover the entire height of the page. The following is not working. I’ve gone through several other posts that address this but none have worked for me so far. What am I missing? Blow if the full code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
/>
</head>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
margin: 0;
padding: 0;
}
</style>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-12" style="height: 5%; background-color: blueviolet">
header
</div>
</div>
<div class="row" style="height: 86.5%; background-color: aliceblue">
<div class="col-12">main</div>
</div>
<div class="row" style="height: 1.9%; background-color: green">
<div class="col-12">thin banner</div>
</div>
<div class="row" style="height: 6.56%; background-color: darkslategray">
<div class="col-12">footer</div>
</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</html>
2
Answers
As I understand, you need the
div
‘s to fill the entire page. If so, with flexbox, it’s quite easy.I changed the
height
‘s to be rounded up evenly to be visually more appealing and to better see what’s happening. Also, I took out the inline styling because it’s easier to maintain. And as you can see, I added a unique class to each.row
for easier addressing.Both of these declarations are necessary:
And notice: I changed the percentages to add up 100% in sum.
Using
vh
units setting heights relative to the viewport height. This ensures the elements take up the desired proportions of the viewport height without needing to explicitly set the height of the parent elements.