Tips for creating CSS Grid elements that stay fixed to the viewport

I am attempting to achieve a 100vh viewport with multiple responsive overlays on top of it. I have managed to fix the header element on the viewport using position: sticky, but I am encountering issues with making the rest work in the same way.

Additionally, I am exploring the use of CSS Grid for this layout. Below is the code snippet:

body {
  display: grid;
  grid-template-columns: 1fr minmax(20em, 30vw);
  grid-template-rows: clamp(3em, 10vh, 5em) 1fr clamp(7em, 20vh, 9em);
}

header {
  background-color: #eee;
  grid-column: 1 / end;
  grid-row: 1 / 2;
}

main {
  height: 100vh;
  grid-column: 1 / end;
  grid-row: 1 / end;
}

aside {
  background-color: #ccc;
  grid-column: 2 / end;
  grid-row: 2 / end;
}

footer {
  background-color: #444;
  grid-row: 3 / end;
  grid-column: 1 / end;
}
<body>
  <header></header>
  <main>
    <section class="questions" id="objectives">
      <h2>Objectives</h2>
      <div class="question">
        <h3>1. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>2. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>3. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>4. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>5. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>6. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>7. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>8. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
      <div class="question">
        <h3>9. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
        <button>Yes</button><button>No</button>
      </div>
    </section>
  </main>
  <aside></aside>
  <footer></footer>
</body>

Codepen https://codepen.io/ikamy/pen/rNYwOwM

Kindly minimize the view vertically and then scroll. I am attempting to fix the footer and aside elements without using position: fixed or absolute positioning.

My goal is to have the body displayed as a Grid with 100vh height, while making the other Grid elements stick, and allowing scrolling only on the main element.

Answer №1

Make sure to use min-height:100vh instead of height:100vh

Then, apply position:sticky to the element you want to stick at the top or bottom.

body {
        display: grid;
        grid-template-columns: 1fr minmax(20em, 30vw);
        grid-template-rows: clamp(3em, 10vh, 5em) 1fr clamp(7em, 20vh, 9em);
    }
    
    header {
        background-color: #eee;
        grid-column: 1 / end;
        grid-row: 1 / 2;
    }
    
    main {
      min-height:100vh;
        grid-column: 1 / end;
        grid-row: 1 / end;
    }
    
    
    aside {
        background-color: #ccc;
        grid-column: 2 / end;
        grid-row: 2 / end;
        position: sticky;
        top: 0;
        z-index:99999;
    }
    
    footer {
        background-color: #444;
        grid-row: 3 / end;
        grid-column: 1 / end;
       position: sticky;
        bottom: 0
          ;
    }
<body>
    <header></header>
    <main>
        <section class="questions" id="objectives">
            <h2>Objectives</h2>
            <div class="question">
                <h3>1. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>2. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>3. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>4. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>5. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>6. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>7. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>8. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
            <div class="question">
                <h3>9. Lorem ipsum dolor sit amet, consectetur adipisicing.</h3>
                <button>Yes</button><button>No</button>
            </div>
        </section>
    </main>
    <aside></aside>
    <footer></footer>
</body>

For more information, visit: 'position: sticky' not working when 'height' is defined

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When I added a border in CSS and inserted a link within that border, the link ended up extending beyond the border itself

I decided to create a border class for my link and placed the link inside that border. However, when I view it on a responsive device, the link extends beyond the borders while the text within the border remains intact. How can I fix this issue? Here is m ...

Enable the ability to scroll and click to navigate an overlapping Div element

A customer has a website with a dark teal design and is not pleased with the appearance of the scroll bar, as it disrupts the overall style. The client requested that I find a solution without using third-party libraries, and one that they can easily under ...

Are user message templates available for use with Twitter Bootstrap?

I am interested in implementing Twitter Bootstrap into my website design. While it looks amazing, I now need to incorporate user messages (similar to those found on forums or Twitter). These messages should include short text, submission time, user image, ...

Center-align the text vertically in relation to the icon

A query has arisen, regarding a div that contains text and an icon. The issue at hand is that on larger screens the text is not vertically aligned in the center. For further reference, please visit this bootply link: http://www.bootply.com/CHKeRxKOX6 http ...

The widths of inputs vary between FireFox and Chrome

I've been investigating why the widths in FireFox and Chrome show a significant difference. This variation is causing a problem with my Modal in the views. Chrome: https://i.sstatic.net/D0lgu.png FireFox: https://i.sstatic.net/do4YU.png Upon comp ...

Click on link after animation has finished

I'm currently facing an issue with my script not functioning correctly. The code I'm utilizing is from a resource provided by Codyhouse to implement a 3D rotating navigation menu on my webpage. Essentially, when you click the hamburger icon, it o ...

In JavaScript, a prompt is used to request the user to input a CSS property. If the input is incorrect,

Implement a while loop that continuously prompts the user to enter a color. If the color entered matches a CSS property such as blue, red, or #000000: The background will change accordingly, but if the user enters an incorrect color, a message will be dis ...

What is the best way to shift <p> slightly to the left using css?

I've created an HTML file structured like this. <html> <head> </head> <body> <div class="final_time"> <p class="final_time_text">some text here</p> </div> </b ...

Tips on how to prevent certain classes from being impacted by a hue-rotate filter applied to all elements on a webpage

I am currently in the process of adding a feature that allows users to choose between a dark or light theme, as well as select a specific theme color for the app. The implementation involves using CSS filters such as invert(1) for the dark theme and hue-ro ...

Getting the css property scaleX with JQuery is as simple as executing the

I am struggling to print out the properties of a specific div element using jQuery. My goal is to have the different css properties displayed in an information panel on the screen. Currently, I am facing difficulties trying to show scaleX. Here is my curr ...

Issues with Bootstrap split button display malfunctioning

Experiencing an issue with a Bootstrap split button while working on a test Drupal website. Although it seems to be working fine in Codepen and JSFiddle, on this particular website, the dropdown component appears to be getting pushed under the button. Desp ...

Ways to have the "li" element displayed on a separate line

let selector = document.querySelector('.activate') selector.addEventListener('mouseover', function(){ document.querySelector('#hidden-li').classList.remove("hidden") }) selector.addEventListener('mouseleave', f ...

"Steady layout of grid for the navigation bar and

Currently, I am in the process of developing a control panel with the use of HTML and CSS. To structure the page, I opted for a grid layout. However, I encountered an issue where the navbar and sidebar do not stay fixed on the screen despite trying various ...

Is there a Polar transformation available in CSS3?

Converting a line into a ring is a straightforward process in graphic design software like GIMP: https://i.sstatic.net/7lQZe.png (source: adamhaskell.net) I'm exploring the possibility of achieving the same effect using CSS. Here's what I&ap ...

Obtain the rotational value in 3D CSS using JavaScript by extracting it from the matrix3d()

My element has been 3D transformed in the following way: .foo { transform: rotateX(-30deg) rotateY(35deg); } Now, I am looking to retrieve these values using JavaScript. Extracting the 3D matrix is simple: var matrix = $('.foo').css('tr ...

Hovering into Transition Time

My article card has a transition on the top attribute of the info div, which is supposed to be smooth and last for 0.3 seconds. However, the description suddenly appears during this transition. I'm trying to figure out why this is happening and how to ...

JavaScript encoding and decoding challenges

Can anyone help me figure out what's wrong? I'm trying to encode and decode a simple input, but it just doesn't seem to work! Any ideas why? Thanks in advance for your assistance :) ENCODE: function encryption_encode(s, delta) { var te ...

Developing a system mode called "night mode"

I've decided to incorporate a dark mode feature into my Wordpress theme. Creating both dark and light modes was a breeze, but now I want to add a third mode that serves as the default for pages. This new mode will automatically switch between dark a ...

What are some ways to customize the text and button location for Bootstrap 5's file input?

Bootstrap 5's input type file seems too simplistic. Check it out here https://i.stack.imgur.com/VZ0h5.png I am curious about three things: Can the "Choose file" button be moved to the right? Is it possible to change the message that says "No files ...

Generate a menu submenu allowing for interactive toggling and dynamic visualization of expandable/collapsible sections upon clicking, featuring visually

Looking for assistance in creating a dynamic menu with sub-menus that can expand upon clicking. The goal is to have an expandable menu structure where the user can click to expand or collapse certain sections. However, my current code is not functioning co ...