id
selector nor .content-header
or .content-footer
.
There are a couple of ways that you can achieve it. I want to show you 3 of them.
Flex – flex-grow-1
-
Use the
h-100
class for all the parents of the#content
div includinghtml
andbody
. -
Use
d-flex
,flex-column
, andh-100
classes for the#content
div. - Use
flex-grow-1
on themain
content.
You should use boostrap version 4.1 or higher because the lower version does not have flex-grow-1
.
See this pen. I recommend you to add and remove texts so that you see that it works.
Flex – mt-auto
-
Use the
h-100
class for all the parents of the#content
div includinghtml
andbody
. -
Use
d-flex
,flex-column
, andh-100
classes for the#content
div. - Use
mt-auto
for thefooter
.
html,
body {
height: 100%
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<div id="app" class="h-100">
<div id="content" class="d-flex flex-column h-100">
<nav id="content-header" class="bg-info p-5">
...code here...
</nav>
<main id="content-main" class="bg-primary p-5">
...code here...
Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugiat hic aspernatur quibusdam alias delectus odit officiis in, est sapiente deserunt harum aliquam at mollitia deleniti labore corrupti illum recusandae dolorum.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Iste inventore voluptatum sint mollitia unde quisquam numquam vitae? Id, quia. Cupiditate nam vero natus, facere nesciunt vel delectus assumenda eos sequi!
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Non asperiores perferendis quae harum ab, dolorem dicta repudiandae quisquam repellendus eveniet, totam voluptatum, eum cum nobis? Atque alias dolores nam illum.
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Animi odit aspernatur minima tempora! Similique consequatur distinctio odit nemo, pariatur consectetur ad ipsum provident corporis nostrum culpa cumque doloremque quo quia.
</main>
<div id="footer" class="bg-danger p-5 mt-auto">
...code here...
</div>
</div>
</div>
min-height: calc(100vh – (header + footer height));
We have used this one, because some very old browsers does not support flex
box. Check browser support for flex.
- Find the sum of height of footer and header, suppose it is
120px
- Set
min-height
ofmain
tocalc(100vh - 120px)
;
main {
min-height: calc(100vh - 120px);
}
See this pen.
https://codepen.io/anon/pen/bKExLm
Visit these pages to learn the other methods