-1

https://jsfiddle.net/chovy/5vofg0cr/8/

<div class="navbar">
navbar
</div>
<section>
  <header>header</header>
  <div class="chat">
  chat window
  </div>
  <footer>
    <textarea>chat enter</textarea>
  </footer>
</section>

css:

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  border: 1px solid gray;
}

.navbar {
  height: 2rem;
}

section {
  height: calc(100vh - 2rem);
}

.chat {
  background-color: green;
  height: 100%;
}

I'm trying to make the whole thing fit 100% of viewport without overflowing past the viewport size (ie: different window sizes)

chovy
  • 72,281
  • 52
  • 227
  • 295

1 Answers1

0

I usually keep track of both the footer and the headers height in a css variable, then use min-height in combination with the calc() method.

:root {
  --header-height: 2rem;
  --footer-height: 4rem;
}

header {
  background-color: magenta;
  height: var(--header-height)
}

.chat {
  background-color: aqua;
  min-height: calc(100vh - var(--header-height) - var(--footer-height))
}

footer {
  background-color: green;
  height: var(--footer-height)
}
<header>header</header>
<div class="chat">
   This will fill up the entire remaining space even if there is not enough content
   to fill up vertical space, so the footer will always stick to the bottom of the screen
</div>
<footer>footer</footer>

Using min-height has a few benefits, first off it'll allow your content to grow over the value you set (more than 100vh for example). second, it'll keep the footer on the bottom of the screen, even if there isn't enough content to fill 100vh

calc() being the awesome method that it is, will allow you do use any unit you want (%, px, em, rem, vh)

Sadra M.
  • 1,304
  • 1
  • 17
  • 28
  • Is it possible to know the footer height, for example? On different widths if viewport it may take up different heights. – A Haworth Jul 16 '22 at 12:33
  • Short answer, not without javascript, but because this is `min-height` your footer height shouldn't really matter. – Sadra M. Jul 16 '22 at 12:36