Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make content min-height fill the screen

Tags:

html

css

As an example, I've made a fiddle: https://jsfiddle.net/L7mwpzux/3/

How do I make the div .container minimally fill the screen? So when there is almost no content, it still fills the screen.

It's for a page that is shown when the checkout cart is empty. The content is too thin, so the screen is not fully filled with content.


P.s. I am not looking for an answer that assumes that the header or footer has a static height. I want to be able to use it also in situations where the height of the header or footer is variable.

Also, I would love a CSS solution, so no JavaScript or jQuery

like image 510
Maarten Wolfsen Avatar asked Sep 16 '16 09:09

Maarten Wolfsen


1 Answers

You can use calc() and set 100vh - height of header, also add box-sizing: border-box to keep padding inside.

* {
  box-sizing: border-box;
}
body,
html {
  margin: 0;
  padding: 0;
}
header {
  height: 200px;
  background-color: blue;
  width: 100%;
}
.container {
  padding: 50px;
  min-height: calc(100vh - 200px);
}
footer {
  width: 100%;
  height: 200px;
  background-color: #333;
}
<header>
</header>

<div class="container">
  small text
</div>

<footer>
</footer>

Other approach is to use Flexbox and set display: flex on body which is parent element in this case with min-height: 100vh and then just set flex: 1 on .container so it takes rest of free height.

* {
  box-sizing: border-box;
}
body,
html {
  margin: 0;
  padding: 0;
}
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
header {
  height: 100px;
  background-color: blue;
}
.container {
  padding: 50px;
  flex: 1;
}
footer {
  height: 100px;
  background-color: #333;
}
<header>
</header>

<div class="container">
  small text
</div>

<footer>
</footer>
like image 67
Nenad Vracar Avatar answered Nov 15 '22 05:11

Nenad Vracar