CSS Layout How to - Align header div and footer








Question

We would like to know how to align header div and footer.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
* {<!--   ww w.j  a v  a 2 s.  c o m-->
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
}

header, footer, div {
  width: 100%;
}

div {
  height: 100%;
  background: pink;
  margin-top: -64px;
  padding-top: 64px;
  margin-bottom: -64px;
  padding-bottom: 64px;
  box-sizing: border-box;
}

header {
  height: 64px;
  background: purple;
  position: relative;
  z-index: 1;
}

footer {
  height: 64px;
  background: gray;
}
</style>
</head>
<body>
  <header>header</header>
  <div>content</div>
  <footer>footer</footer>
</body>
</html>

The code above is rendered as follows: