Next.js implementation of a sticky header and footer

My experience with React is pretty extensive, but I am fairly new to Next.js and the layout logic is confusing me. As far as I understand, if layout.js is located under /app, it means that the layout will be applied to the entire application, which is what I want. I need to have a header that sticks to the top of the page and a footer that sticks to the bottom without being hidden when I scroll. Initially, I achieved this by placing my components in page.js. However, once I started using routing, I realized that was not the correct placement according to the documentation. Instead, they should be placed in layout.js:

import { Inter } from "next/font/google";
import "./globals.css";
import FooterComponent from "@/app/components/footer/footer.component";
import HeaderComponent from "@/app/components/header/header.component";

const inter = Inter({ subsets: ["latin"] });

export const metadata = {
  title: "Solute Art Studio",
};

export default function RootLayout({ children }) {
  return (
      <html lang="en">
          <body>
            <HeaderComponent/>
            <main>{children}</main>
            <FooterComponent/>
          </body>
      </html>
  );
}

After moving the components to layout.js, I noticed that the header no longer sticks when I scroll. The footer remains at the bottom, but even when there is no content between the header and footer, I still have to scroll to see it. It seems like I messed up some CSS after relocating these components. What am I missing? I am using tailwind for styling. Header:

<Disclosure as="nav" className="bg-white shadow sticky top-0">...
. Footer:
<footer className="bg-white sticky top-[100vh]">...

Answer №1

One effective method is to encase your header, footer, and page content (children) within a div set to the "h-screen" property. Utilize flex-grow around your children with the code snippet below:

<div className="flex flex-col h-screen w-screen">
  <Header />
    <div className="flex flex-grow flex-row max-w-full max-h-full overflow-auto">
      {children}
    </div>
  <Footer />
</div>

By incorporating the Header and Footer in this layout, they will be applied to all pages. The use of "flex-grow" on the page content ensures it fills the space between the header and footer, while "overflow-auto" provides scrolling functionality if the content exceeds the view height, maintaining the header and footer at the top and bottom of the page.

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

Troubleshooting the Create Order Issue: Integrating PayPal Checkout with Smart Payment Buttons using React and Redux

Every time I attempt to process a payment, I encounter a 422 error: Unprocessable entity. The issue arises when I try to dynamically capture the purchased item details received from the redux store. I tried following this example (duplicate): PayPal Check ...

Ways to transfer data from TypeScript to CSS within Angular 6

Trying to work with ngClass or ngStyle, but I'm struggling with passing the value. Here's my current code: strip.component.ts import { ... } from '@angular/core'; @Component({ selector: 'app-strip', templateUrl: &apo ...

Exploring timezones using moment.js

I have received scheduledTime and timezone data from an API, for example: scheduledTime: 1603890000000 timezone: "EDT" My goal is to display this information in the user's device timezone, such as CST. What would be the most effective approach to ach ...

Utilizing an observer to encapsulate a custom React hook- a comprehensive guide

As part of my project, I have developed a unique custom react hook that relies on observable state from the store for its dependencies within useEffect: Here is an example of my custom hook: const useFoo = (() => { const { count } = store; useEff ...

overabundance of hexagonal background patterns

Is there a way to make my hexagon background image overflow properly? I have designed hexagons using the following code: --- CSS --- #color5 { background-image: url(http://URL/images/Logo.jpg); background-color: purple; z-index: 1; } #hex3 { ...

Emphasize the engaged menu selection when the page is refreshed

My application is a dashboard app built with React, Redux, and Antdesign. I utilized the layout provided by Ant design at https://ant.design/components/layout/. One issue I encountered is that when clicking on side menus, the active menu gets bold but upo ...

What distinguishes the act of altering attributes using React.createContext() in comparison to the value property?

Imagine you have the code snippet below: //App.js //logoutHandler function defined here function App { const testContext = React.createContext({ isLoggedIn: false, onLogout: logoutHandler }) return ( <testContext.Provider> //Some code w ...

What benefits does Mobx bring to the table?

I'm finding the MobX web tutorial quite challenging. I would greatly appreciate it if someone could explain to me simply why MobX is beneficial, especially considering that React already has its own state management system. ...

There was an internal server error encountered while trying to make a call to a Laravel API from

I have successfully implemented an API in Laravel and tested it using Postman tool. Both the Laravel project folder and the Next.js project folder are located within the same parent directory named laravel-next. The Laravel application is running on local ...

Update the CSS styling of a parent div based on the active state of specific child divs

I have a class with 4 distinct columns. div class="mainContent"> <div class="left-Col-1" *ngIf="data-1"> </div> <div class="left-Col-2" *ngIf="!data-1"> ...

How to manipulate image URLs in Next.js using rewrite rules

When using next/image on SSR pages and the Next server, it retrieves images from addresses like this: "http://inner-server-ip/img/pic1.png" Then Next changes it to "/_next/image?url=http://inner-server-ip/img/pic1.png", I aim to hid ...

Unable to supersede CSS styling

HTML <div class="dlrs"> <div id="listItems"> <div id="clrCode" class="colorBlock" style="background-image: url("/media/images/orange.png"); background-position: center center; background-repeat: no- repeat ...

The floating property doesn't appear to function properly in Internet Explorer versions 6 through 8 when

I've noticed that my website in Dutch looks great on Firefox and Safari (both Mac and PC), as well as Chrome (Mac tested, PC untested), but for some reason it's not displaying correctly on IE 6-8 (which is unavailable on Mac). I suspect there may ...

How can I eliminate the default background of Mui tooltip and personalize it in react?

Below is an example of how to customize a nested tooltip within a default background tooltip in MUI. The challenge here is to remove the grey border in the customized tooltip and have only a white background with black text. Check out the code snippet be ...

React MUI5 component latency when changing disable prop conditionally

I've been experiencing issues with latency when trying to conditionally enable/disable a Material UI button. This problem arose after updating my materialUi and reactjs to the latest versions (React 18, MUI/Material: 5.10.10). I'm using a sample ...

The React snackbar is mysteriously peeking out from behind the popup

While using the react-notifications-component snack bar, I encountered an issue where my snack bar was appearing behind the pop-up. Is there a way to fix this with z-index? I tried using <ReactNotification style={{ zIndex: 10000 }}/>, but it didn&ap ...

Activate an event on a shadow DOM element that is covered by an element in the light DOM (not directly related)

I am currently facing a challenge in retrieving the coordinates of a table cell within a shadow DOM. This table cell may have overlay elements displayed above it, which could span over multiple cells and may not necessarily be direct children of the cell I ...

How can I properly implement a Closure or IIFE to manage an onclick event in ReactJS?

I'm encountering an issue while attempting to utilize the this object in an event handler. An undefined error related to the this object keeps popping up. My development stack includes ReactJS and Redux as well. class Chat extends Component { c ...

In search of bundle.js, the React app is scouring through the component folder, rather than

Upon refreshing on a nested route (register/email-confirmation), the console displays errors that do not occur on non-nested routes. The issue seems to be related to the search for bundle.js and the image within the nested route path, rather than the root ...

What is the best method for invoking hook calls within React class-based components?

Is it possible to use functions such as useMediaQuery in class-based components? If so, how can this be achieved? import React from 'react'; import clsx from 'clsx'; import { withStyles ,withTheme ,withMediaQuery } from '@material ...