Ensure the grid container spans the entire height of the content

Objective: Implement a sticky navigation bar at the top, followed by a grid where the last row maintains its natural height and stays at the bottom of the window if there is not enough content to push it off screen.

Challenge: I am encountering difficulty in making my grid fill 100% of the height of the content, causing issues with my sticky navigation bar. The content is generated from a headless CMS using NextJS. A typical page structure looks like this:

<html>
    <body>
        <div id="__next">
            <header></header> // should be sticky
            <div> // wrapper which is set as a grid
                Content from the CMS...
                <footer>Also from the CMS</footer>
            </div>
        </div>
    </body>
</html>

The header styling worked as intended:

header {
  position: sticky;
  top: 0px;
  left: 0px;
  width: 100%;
}

However, when creating custom error pages, I noticed that the footer inside the grid was not positioned at the bottom of the page when there was insufficient content. After researching and experimenting, the solution appeared to involve setting height: 100% and min-height: 100vh; for both html and body. By using grid-template-rows: 1fr auto;, the first row would expand to fill available space while the footer would shrink and align properly at the bottom.

This approach worked well for pages with minimal content. However, problems arose on regular pages with more than one row or sufficient content exceeding viewport height.

Based on the provided code snippet, the sticky navigation bar remains sticky initially but then loses its effect. This issue seems to stem from the grid container not expanding to accommodate all its content.

I have attempted various CSS properties such as grid-auto-rows: 1fr;, grid-template-rows: 1fr auto;, and grid-auto-rows: auto; without success.

How can I achieve the desired layout (sticky navigation bar outside of grid, footer inside grid at bottom of window when content is insufficient)?

Note: As I have control over the NextJS setup and CMS templates, I could move the footer out of the grid if necessary.

Answer №1

Your HTML structure appears to be a bit messy upon inspection.

Before anything else, consider if you truly need a sticky navbar. Typically, navigation menus are styled with position: fixed. The only scenario where a sticky navbar may be necessary is if you have two distinct content sections and require separate navbars for each one.

Below is the recommended structure:

html, body { margin: 0; padding: 0;}

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

header {
  padding: 20px;
  background-color: blue;
  position: sticky;
  top: 0px;
}

.imitation-of-content {
  height: 2000px;
}

.scope-of-sticky-navbar {
  background-color: yellow;
}

.content-without-navbar {
  background-color: green;
}


footer {
  flex: 0 0;
  padding: 20px;
  background-color: red;
}

.scope-of-sticky-navbar, .content-without-navbar {
  flex-grow: 1;
}
<html>

<body>

  <div class="scope-of-sticky-navbar">
    <header>Header stuff</header>
    <div class='content'>
      <div class="imitation-of-content">content</div>
    </div>
  </div>
  
  <div class="content-without-navbar">
    <div class="imitation-of-content">content2</div>
  </div>
  
  <footer>
    Footer stuff
  </footer>
  
</body>

</html>

The use of position:sticky will cause an element to remain in view during scrolling within its parent container. Top and bottom offsets are controlled by the top and bottom properties of the sticky element, relative to its parent.

Avoid setting manual heights for elements whose size is dynamic. Using height: 100% can lead to overflow as it sets the height based on its parent's size. Instead, utilize min-height so that the element adjusts its height accordingly.

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 a user hovers over a link, a div will appear that is also visible when hovering over that div

I have a link that displays a div when hovered over. I already have code to show the it, but how can I hide it when the cursor leaves both the link and the div? Here is my HTML code: <a href="javascript:void(0);" id="show_div">2 items</a> &l ...

Utilizing local storage, store and access user's preferred layout options through a click function

Exploring the realm of localstorage for the first time, I am considering its use to store a specific ID or CLASS from my css file. This stored information would then be utilized to render a selected layout (grid/list) in the user's browser upon their ...

jQuery - "Error: fadeIn is not a valid function"

Encountering an odd issue here. Attempting to execute fadeIn() but receiving the error ".fadeIn is not a function". Here is my code: body { display:none; } <!DOCTYPE html> <html lang="en> <head> <!-- Required meta tags --&g ...

Creating double borders on a single element without using a wrapping div is possible using only CSS

I am attempting to create a specific effect using just one element, aiming to eliminate the need for a wrapper div. Check out my progress so far: http://codepen.io/anon/pen/kdvex Although I have come close to achieving the desired effect by incorporating ...

Displayed outside the div element, the Bootstrap popover is extending beyond its

When using bootstrap popover, everything works fine except for the fact that when hovering outside the div area, the popup still appears. Changing the div to a button resolves this issue. Any suggestions? If you want to see a Demo, hover outside the red d ...

Strange occurrence: HTML/CSS card breaks page width unexpectedly

enter image description here Hey there! I'm currently working with Next.js and have run into an issue where the page is overflowing on the right side for no apparent reason. This seems to be caused by an HTML/CSS card that I created, as depicted in t ...

Explaining the mechanics of the page-turning effect in turn.js

I'm interested in creating a similar page flip effect and I'm curious about how turn.js achieved it. It seems to utilize CSS transformations like translation and rotation. Upon inspecting the code, I noticed there are two wrappers with different ...

Creating a three-column layout in HTML5 with resizable left and center columns, and a fixed right column

Looking to create a full screen layout with three columns, where the left/center column is resizable and the right column remains fixed. Currently using the resize style for the left area, but running into issues as the center area does not adjust in size ...

Alert: Angular 5 detects an invalid selector '';'

I am encountering an issue with my Angular 5 application. After running ng b --prod, I am seeing the following warnings: Warning in Invalid selector '; .space1x at 6219:39. Ignoring. The CSS style for space1x in style.css is as follows: .space1 ...

Using Flexbox to align content in a column with space between items

Is there a way to move the read more button to the bottom of the card using flexbox in a column layout? The top section, middle paragraph, and see more button are each contained within their own div inside the parent card div. https://i.stack.imgur.com/NG ...

The Angular overlay panel remains open consistently

I recently developed an Angular component similar to p-overlaypanel, but I'm facing an issue where it remains open for every item I click. What I actually want is for only one overlay panel to be clicked and shown at a time - if I click on another ove ...

Swapping out the Close button on a JQueryUI Dialog pop-up

Despite my thorough search efforts on this topic, I have not been able to find a solution to my issue. Hopefully someone can assist me with this problem. I am working with a simple dialog box: $("#dialog-search").dialog({ resizable: false, height: ...

Setting breakpoints on rows in Bootstrap 5: A guide

Looking for ways to modify the diagram in the image using Bootstrap 5 specifically for mobile devices ...

Creating an inner shadow effect for geometric shapes with text inside - a step-by-step guide!

Is it possible to apply an inner shadow to geometric shapes with text inside using CSS? This is the effect I want to achieve: https://i.stack.imgur.com/XcGS2.png I followed the code provided in this thread: Required pentagon shape with right direction ...

A guide on displaying containers with jQuery and CSS

Looking to create a smiley survey using only Front-End technologies. Once a radio button is selected, the associated content should appear for comments. Currently, I have set up my CSS with display: none. I attempted to implement this functionality using ...

Steps for positioning the scroll-thumb in the middle of the scrollbar

Currently, I am working on a react application and facing an issue while trying to display images with custom zoom-in/zoom-out functionality. Due to the need to add markers to each image that require precise positioning, using a pre-existing library for zo ...

The child's styling is conflicting with the parent's styling

Issue with Parent and Child Component Margins import "../src/App.css"; import CardComponent from "./components/CardComponent"; function App() { return ( <div className="App"> <div className="card"></div> <CardCompon ...

Title with lower border shorter than width

Looking to achieve an underline effect with a bottom border that is narrower than the width of the h2 title. Though I typically avoid uploading images, here is one to provide further clarification of my question: ...

What is the best way to keep an HTML table header fixed within a div while scrolling?

My HTML table now has the scrolling function. Below, you can find the code snippet for the HTML table setup. <div id="flagging" > <table> <thead> <th> Header 1 </th> <th> Header 2 </th> ...

Retrieve the value of a PHP array within a for loop and transfer it to JQuery

*edited format I came across a PHP code for a calendar on the internet and I am currently working on implementing an onclick event that captures the date selected by a user as a variable (which will be used later in a database query). At this point, my g ...