-1

* {
  box-sizing: border-box;
}

.wrapper {
  max-width: 1024px;
  margin: 0 auto;
  font: 1.2em Helvetica, arial, sans-serif;
}

.wrapper>* {
  border: 2px solid #f08c00;
  background-color: #ffec99;
  border-radius: 5px;
  padding: 10px;
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(12, [col-start] 1fr);
  grid-gap: 20px;
}

.wrapper>* {
  grid-column: col-start / span 12;
}

@media (min-width: 500px) {
  .side {
    grid-column: col-start / span 3;
    grid-row: 3;
  }
  .ad {
    grid-column: col-start / span 3;
    grid-row: 4;
  }
  .content,
  .main-footer {
    grid-column: col-start 4 / span 9;
  }
  nav ul {
    display: flex;
    justify-content: space-between;
  }
}

@media (min-width: 700px) {
  .main-nav {
    grid-column: col-start / span 2;
    grid-row: 2 / 4;
  }
  .content {
    grid-column: col-start 3 / span 8;
    grid-row: 2 / 4;
  }
  .side {
    grid-column: col-start 11 / span 2;
    grid-row: 2;
  }
  .ad {
    grid-column: col-start 11 / span 2;
    grid-row: 3;
  }
  .main-footer {
    grid-column: col-start / span 12;
  }
  nav ul {
    flex-direction: column;
  }
}
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Realizing_common_layouts_using_CSS_Grid_Layout -->

<div class="wrapper">
  <header class="main-head">The header</header>
  <nav class="main-nav">
    <ul>
      <li><a href="">Nav 1</a></li>
      <li><a href="">Nav 2</a></li>
      <li><a href="">Nav 3</a></li>
    </ul>
  </nav>
  <article class="content">
    <h1>Main article area</h1>
    <p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p>
  </article>
  <aside class="side">Sidebar</aside>
  <div class="ad">Advertising</div>
  <footer class="main-footer">The footer</footer>
</div>

Here's an example from mozilla doc.

How do I make the footer always stay at the bottom of the page (no matter the viewport)

How do I make the content in between the header and the footer cover the whole remaining area (no matter the viewport). basically, how do I make sure that header + footer + content (and margins) in b/w cover 100% height?

One last question, I want to achieve same with bootstrap. Is there any bootstrap template that does exactly this using its classes? (responsiveness is important).

p.s. Learning html/css, so some explanation of how your solution works would be really helpful.

mahan
  • 12,366
  • 5
  • 48
  • 83
007
  • 2,136
  • 4
  • 26
  • 46

1 Answers1

2

Add width: 100vw; and height: 100vh; to your wrapper, it will stretch . I also added

html, body{
  margin: 0;
}

because the 100vw doesn't take into account the margin of the body, you'll have to set it to 0.

* {
  box-sizing: border-box;
}

html, body{
  margin: 0;
}


.wrapper {
  max-width: 1024px;
  margin: 0 auto;
  font: 1.2em Helvetica, arial, sans-serif;
  width: 100vw; 
  height: 100vh;
}

.wrapper>* {
  border: 2px solid #f08c00;
  background-color: #ffec99;
  border-radius: 5px;
  padding: 10px;
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(12, [col-start] 1fr);
  grid-template-rows: 50px auto auto 50px;
  grid-gap: 20px;
}

.wrapper>* {
  grid-column: col-start / span 12;
}

@media (min-width: 500px) {
  .side {
    grid-column: col-start / span 3;
    grid-row: 3;
  }
  .ad {
    grid-column: col-start / span 3;
    grid-row: 4;
  }
  .content,
  .main-footer {
    grid-column: col-start 4 / span 9;
  }
  nav ul {
    display: flex;
    justify-content: space-between;
  }
}

@media (min-width: 700px) {
  .main-nav {
    grid-column: col-start / span 2;
    grid-row: 2 / 4;
  }
  .content {
    grid-column: col-start 3 / span 8;
    grid-row: 2 / 4;
  }
  .side {
    grid-column: col-start 11 / span 2;
    grid-row: 2;
  }
  .ad {
    grid-column: col-start 11 / span 2;
    grid-row: 3;
  }
  .main-footer {
    grid-column: col-start / span 12;
  }
  nav ul {
    flex-direction: column;
  }
}
<div class="wrapper">
  <header class="main-head">The header</header>
  <nav class="main-nav">
    <ul>
      <li><a href="">Nav 1</a></li>
      <li><a href="">Nav 2</a></li>
      <li><a href="">Nav 3</a></li>
    </ul>
  </nav>
  <article class="content">
    <h1>Main article area</h1>
    <p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p>
  </article>
  <aside class="side">Sidebar</aside>
  <div class="ad">Advertising</div>
  <footer class="main-footer">The footer</footer>
</div>
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
  • Thanks, will need to take a deep dive into your sln, but after a quick test, how do I make the header and footer not grow in height along with everything else? max-height of 60px on header and footer creates a lot of empty space in between. Also, at some smaller width, the footer becomes ~2/3 width of the viewport. is it possible to keep it 100% width all the time? – 007 Dec 12 '18 at 06:05
  • I updated the answer, add `grid-template-rows: 50px auto auto 50px;`. The footer becomes ~2/3 because you have a media query that set it to act this way. You'll have to decide how you want to set the layout of other elements as well... – Itay Gal Dec 12 '18 at 06:25
  • Thank you for the update and explanation. Cheers! – 007 Dec 20 '18 at 15:31