ricksanchez ricksanchez - 4 months ago 22
CSS Question

specifying div height (bootstrap)

so I am trying to make a cool portfolio landing page with Bootstrap.

The template includes a fixed navbar and a sticky footer. The rest of the page is whtespace that I want to fill with basically a giant background so I put a test paragraph element to try and see how to size it. I figured I could just add CSS rules that made body's height 100% but when I do that I get an overflow and my footer isn't at the bottom of the page anymore. Instead I have a small overflow of the background at the bottom.

How can I fix this? I made the background color bright yellow to try and show what I mean. Any help is appreciated!

Here's the HTML

<!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">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Work in Progress</title>
</head>

<body>
<!--navbar-->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Portfolio</a>
</div>
<div id="navbar" class="navbar-collapse collapse"
aria-expanded="false" style="height: 1px;">
<ul class="nav navbar-nav">
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-haspopup="true"
aria-expanded="false">Projects
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Rails Projects</a></li>
<li><a href="#">LAMP Projects</a></li>
</ul>
</ul>
</div>
</div>
</nav>


<div class="main">
<div class="container">
<p>Test text</p>
</div>
</div>



<!--footer-->
<footer class="footer">
<div class="container">
<div class="row">
<div class="col-md-4">
<p>Contact info or whatever goes here.</p>
</div>
<div class="col-md-4 col-md-offset-4">
<p>More info or whatever goes here.</p>
</div>
</div>
</div>
</footer>


</body>
</html>


And here's the CSS

html, body{
height: 100%
}

.main{
height: 100%;
background-color: #FFFF00;
margin-top: 50px;
}

.footer{
position: absolute;
bottom: 0;
width: 100%;
height: 60px;
background-color: #000;
color: #9d9d9d;
}

Answer

You can use this CSS (position: relative on body and .main height as calc function (100% minus height of the footer):

html,
body {
  height: 100%;
  position: relative;
}
.main {
  height: calc(100% - 60px);
  background-color: #FFFF00;
  padding-top: 50px;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #000;
  color: #9d9d9d;
}
Comments