Search code examples
htmlcsscss-grid

How can I put a CSS Grid fixed


Im starting working with blazor and im using a CSS Grid and I want to put two grids as fixed position but when I put position: fixed in CSS Code the size and position of the grid change. How could I manage this? I saw some questions here in Stackoverflow but none of that was what I want.

body {
    margin: 0px;
}

.layout {
    height: 100%;
    display: grid;
    grid-template-columns: 250px 1fr;
    grid-template-rows: 60px 1fr;
    grid-template-areas:
        'header header'
        'sidebar main';
}

.header{
    grid-area: header;
    background-color: red;
}

.main {
    grid-area: main;
    background-color: blue;
}

.sidebar {
    grid-area: sidebar;
    background-color: gray;
    padding: 10px 5px 5px 5px;
}
<html>
    <body>
        <div class="layout">
        
            <div class="header">
                <h1>Header</h1>
            </div>

            <div class="sidebar">
                <Sidebar/>
            </div>

            <div class="main">
                <h1>Main</h1>
            </div>

        </div>
    </body>
</html>

Thank you for your attention.


Solution

  • When you set the position of the grid div to fixed it takes the div out of the scope of its original parent, rather, out of the document flow. So whereas header followed the rules of the grid without the position fixed, with it, 'header' grid is no longer in the grid.

    As for the side bar I believe you can achieve your desired result by following a technique similar to the one mentioned in this post with max-height and overflow: scroll: https://github.com/rachelandrew/cssgrid-ama/issues/24

    Have you considered flex-grids? I created a code snippet below that might be what you're looking for..

    html, body {
      margin: 0;
      height: 100%;
    }
    
    #container {
      display: flex;
      flex-direction: column;
      height: 100vh;
    }
    
    main {
    display: flex;
    flex-direction: row;
    overflow: auto;
    flex: 1;
    }
    .sections {
      flex: 1 1 auto;
      overflow: auto;
    }
    section {
      height: 100%;
      overflow: auto;
      border-bottom: 1px solid;
      background: lightgreen;
    
    }
    
    .sidebar {
      flex: 0 0 20%;
      background-color: lightgrey;
    }
    
    header {background: #f88}
    section:last-child {border: none}
    footer {background: lightblue}
    <div id="container">
      <header>top</header>
      <main>
        <div class="sidebar">sidebar</div>
        <div class="sections">
        <section>1st</section>
        <section>2nd</section>
        <section>3rd<br>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
        </section>
        <section>4th</section>
        <section>5th</section>
        </div>
      </main>
      <footer>bottom</footer>
    </div>