Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you fill the remaining space with grid container in css?

Tags:

html

css

css-grid

I'm new to this. I'm trying to make a webpage with 100vh height, where the header and footer will take 80px and 50px respectively. Whatever space is left in middle should be taken over by a grid container completely. I did succeed so far, but the middle container is taking only as much as its content needs. Please see the below image for refrence:

enter image description here

Also, I was under the impression that the footer tag will put the section at the bottom of the viewport automatically. But that's not happening either. Do I have to use absolute positioning?

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

html {
  font-size: 10px;
}

body {
  height: 100vh;
  overflow-x: hidden;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
}

header {
  padding-left: 40px;
  padding-right: 40px;
}

nav {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  min-height: 80px;
}

.nav-menuList {
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
  gap: 4rem;
}

.nav-list {
  list-style-type: none;
  font-family: sans-serif;
  font-size: 1.4rem;
  font-weight: 500;
  line-height: 4.4rem;
  color: #000;
}

.grid-container {
  display: grid;
  grid-template-rows: 1fr 1fr;
  row-gap: 4rem;
  grid-template-areas:
    "h1"
    "h-btn";
  padding: 0 40px;
  align-self: center;
}

h1 {
  font-family: sans-serif;
  font-size: 4.8rem;
  font-weight: 300;
  line-height: 6.4rem;
  color: #000;
  grid-area: h1;
  width: 75%;
}

.button-group {
  grid-area: h-btn;
}

footer {
  padding: 0 40px;
}
<body>
  <div class="container">
    <header>
      <nav>
        <div class="logo">
          <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M0 4C0 1.79086 1.79086 0 4 0H28C30.2091 0 32 1.79086 32 4V28C32 30.2091 30.2091 32 28 32H4C1.79086 32 0 30.2091 0 28V4Z" fill="black" />
          </svg>
        </div>
        <div class="menu">
          <ul class="nav-menuList">
            <li class="nav-list">menu</li>
            <li class="nav-list">menu</li>
            <li class="nav-list">menu</li>
          </ul>
        </div>
      </nav>
    </header>
    <main>
      <div class="grid-container">
        <h1>Lorem ipsum dolor sit amet</h1>
        <div class="button-group">
          <button class="primary-btn hero-btn">CTA</button>
        </div>
      </div>
    </main>
    <footer>This is footer</footer>
  </div>

</body>
like image 276
Bluebug Avatar asked Dec 04 '25 14:12

Bluebug


1 Answers

Flexbox solution:

  1. Apply to the body or the flexbox container: min-height: 100vh;.
  2. Apply to the header: height: 80px; and to the footer: height: 50px;.
  3. Make the main or content-box consume the remaining space by adding: flex-grow: 1;

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

header {
  height: 80px;
}

footer {
  height: 50px;
}

main {
  flex-grow: 1;
}




/* for demonstration purpose only */
body {
  margin: 0;
}

header,
footer,
main {
  display: flex;
  justify-content: center;
  align-items: center;
}

header {
  background-color: pink;
}

main {
  background-color: lightblue;
}

footer {
  background-color: lightgreen;
}
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS-Grid solution:

  1. Apply to the body or the grid-container: min-height: 100vh;
  2. Define the rows: grid-template-rows: 80px auto 50px; which means that auto will consume all remaining space.

body {
  height: 100vh;
  display: grid;
  grid-template-rows: 80px auto 50px;  
}




/* for demonstration purpose only */
body {
  margin: 0;
}

header,
footer,
main {
  display: flex;
  justify-content: center;
  align-items: center;
}

header {
  background-color: pink;
}

main {
  background-color: lightblue;
}

footer {
  background-color: lightgreen;
}
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
like image 155
tacoshy Avatar answered Dec 07 '25 07:12

tacoshy