3

I would like to render a page which has a fixed header and footer and a scrollable (if needed) middle part (which made of a sidebar and a main content area) - using CSS Grid.

In the example below (also as a codepen), the "page" is the square rectangle (#container) and the paragraphs in the main area are the content which I would like to have scrolling, with the footer fixed:

#container {
  display: grid;
  height: 400px;
  width: 400px;
  border: solid black;
  grid-template-areas: 
    "header header"
    "sidebar main" 
    "footer footer";
  grid-template-columns: 1fr 3fr;
  grid-template-rows: auto 1fr auto;
}

#header {
  grid-area: header;
  background: gray;
}

#sidebar {
  grid-area: sidebar;
}

#main {
  grid-area: main;
}

#footer {
  grid-area: footer;
  background: gray;
}
<div id="container">
  <div id="header">the header</div>
  <div id="sidebar">the sidebar</div>
  <div id="main">
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
    <p>the main part<p>
  </div>
  <div id="footer">the footer</div>
</div>

My understanding of the grid system was that stating

grid-template-rows: auto 1fr auto;

means "the first and third row will have the height enforced by their content, and the second row will take all the remaining space within the height of the page".

This is what actually happens when the content of the main area does not overflow (the footer is stuck at the bottom, as expected), but as soon as the overflow happens, the scroll bar is for the whole page, and not only for the second row.

Is there a way to elegantly(*) manage this with CSS Grid?


(*) by elegantly I mean in a proper way, without hijacking the scrollbar as I saw on some sites where scrolling becomes a nightmare, with a zillon scrollbars, each scrolling something in a hardly predictable and user-friendly way.

WoJ
  • 27,165
  • 48
  • 180
  • 345

2 Answers2

3

The answer of @Paulie_D is basically the right one for me. However, you should pay attention to how the sidebar behaves if it itself has too much content.

From my point of view there are two possibilities:

  1. The main-content and the sidebar scroll separately from each other (e.g. useful for SaaS-products, admin-areas, internal-areas, etc. but not for "normal" websites).
  2. The main-content and the sidebar scroll together.

1. Main-Content and sidebar scrolling separate:

For the first solution, you can actually fall back on the approach of @Paulie_D.

In this case add overflow:auto to #main

I would just give the sidebar some extra values. First an overflow: hidden; and then on the :hover status an overflow-x: hidden; overflow-y: scroll;, so that the scrollbar is not always visible there.

Personally, I would not give the content an overflow: auto;, rather also an overflow-x: hidden; overflow-y: scroll;, so that the scrollbar is always visible there and the content itself is not always a bit wider when the scroolbar is not necessary.

*,
*::before,
*::after {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  height: 100%;
  width: 100%;
  font-size: 100%;
  overflow: hidden;
}

body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.site {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 
    "siteHeader siteHeader"
    "siteSidebar siteMain" 
    "siteFooter siteFooter";
  height: 100%;
  width: 100%;
}

header {
  grid-area: siteHeader;
  height: 40px;
  padding: 10px 20px;
  background: gainsboro;
}

main {
  grid-area: siteMain;
  padding: 20px;
  overflow-x: hidden;
  overflow-y: scroll;
}

aside {
  grid-area: siteSidebar;
  padding: 20px;
  border-right: 2px solid gray;
  overflow: hidden;
}

aside:hover {
  overflow-x: hidden;
  overflow-y: scroll;
}

footer {
  grid-area: siteFooter;
  padding: 10px 20px;
  background: silver;
}
<html>

  <body>

    <div class="site">

      <header>

        {headerContent}

      </header>

      <main>

        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>

        <p>Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.</p>

        <p>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu.</p>

        <p>Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque.</p>

        <p>Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac placerat dolor lectus quis orci. Phasellus consectetuer vestibulum elit. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Vestibulum fringilla pede sit amet augue. In turpis. Pellentesque posuere. Praesent turpis. Aenean posuere, tortor sed cursus feugiat, nunc augue blandit nunc, eu sollicitudin urna dolor sagittis lacus. Donec elit libero, sodales nec, volutpat a, suscipit non, turpis. Nullam sagittis. Suspendisse pulvinar, augue ac venenatis condimentum, sem libero volutpat nibh, nec pellentesque velit pede quis nunc. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce id purus. Ut varius tincidunt libero. Phasellus dolor. Maecenas vestibulum mollis</p>

      </main>

      <aside>

        {asideContent}

      </aside>

      <footer>

        {footerContent}

      </footer>

    </div>

  </body>

</html>

2. Main-Content and sidebar scrolling together:

For the second solution, you'll need to adjust your grid a bit and add another div around your content and sidebar. This div then gets the property overflow-x: hidden; overflow-y: scroll; and can then get its own grid to properly align the content and sidebar.

*,
*::before,
*::after {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  height: 100%;
  width: 100%;
  font-size: 100%;
  overflow: hidden;
}

body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.site {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 
    "siteHeader siteHeader"
    "siteContent siteContent" 
    "siteFooter siteFooter";
  height: 100%;
  width: 100%;
}

header {
  grid-area: siteHeader;
  height: 40px;
  padding: 10px 20px;
  background: gainsboro;
}

.content {
  grid-area: siteContent;
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-areas: 
    "contentSidebar contentMain";
  overflow-x: hidden;
  overflow-y: scroll;
}

main {
  grid-area: contentMain;
  padding: 20px;
}

aside {
  grid-area: contentSidebar;
  padding: 20px;
  border-right: 2px solid gray;
}

footer {
  grid-area: siteFooter;
  padding: 10px 20px;
  background: silver;
}
<html>

  <body>

    <div class="site">

      <header>

        {headerContent}

      </header>

      <div class="content">

        <main>

          <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>

          <p>Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.</p>

          <p>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu.</p>

          <p>Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus. Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque.</p>

          <p>Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac placerat dolor lectus quis orci. Phasellus consectetuer vestibulum elit. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Vestibulum fringilla pede sit amet augue. In turpis. Pellentesque posuere. Praesent turpis. Aenean posuere, tortor sed cursus feugiat, nunc augue blandit nunc, eu sollicitudin urna dolor sagittis lacus. Donec elit libero, sodales nec, volutpat a, suscipit non, turpis. Nullam sagittis. Suspendisse pulvinar, augue ac venenatis condimentum, sem libero volutpat nibh, nec pellentesque velit pede quis nunc. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce id purus. Ut varius tincidunt libero. Phasellus dolor. Maecenas vestibulum mollis</p>

        </main>

        <aside>

          {asideContent}

        </aside>

      </div>

      <footer>

        {footerContent}

      </footer>

    </div>

  </body>

</html>

I hope that could help :)

Arne
  • 995
  • 1
  • 8
  • 16
1

You just have to tell the element what to do when it overflows:

In this case add overflow:auto to #main

enter image description here

#container {
  display: grid;
  height: 250px;
  width: 400px;
  border: solid black;
  grid-template-areas: "header header" "sidebar main" "footer footer";
  grid-template-columns: 1fr 3fr;
  grid-template-rows: auto 1fr auto;
}

#header {
  grid-area: header;
  background: gray;
}

#sidebar {
  grid-area: sidebar;
}

#main {
  grid-area: main;
  overflow: auto;
}

#footer {
  grid-area: footer;
  background: gray;
}
<div id="container">
  <div id="header">the header</div>
  <div id="sidebar">the sidebar</div>
  <div id="main">
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
    <p>the main part</p>
  </div>
  <div id="footer">the footer</div>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161