What is the best way to create a fixed footer in Next.js using React?

I'm looking to create a fixed footer that will stay at the bottom of the page if there isn't enough content to fill it. I've been researching ways to achieve this using CSS, but many of the methods don't easily translate to React/Next.js since they involve manipulating the html and body tags. I'm curious if there are alternative approaches to accomplish this.

Below is the JSX for the Footer component:

     <div>
        <footer>
          <a href={"https://www.instagram.com/linghandmade18/"}>
            <i className="fab fa-instagram" />
          </a>
        </footer>
        <h2>Some Text</h2>
      </div>

And here is my Layout file for Next.js:

const Layout = (props) => {
  return (
    <div>
      <Navbar />
      {props.children}
      <Footer />
    </div>
  );
};

Answer №1

To avoid dealing with html and body tags directly, it's helpful to create a container where you can apply your styles. Start by adding a common container inside your Layout (assign the container class to the parent element), like this:

const Layout = (props) => {
  return (
    <div class="container">
      <Navbar />
        {props.children}
      <Footer />
    </div>
  );
};

You now have a .container class for the div that contains Navbar, Content, and Footer. Next, add the following styles to the container class:

.container {
  min-height: 100vh;
  position: relative;
}

This will ensure that the container height is at least 100vh (viewport height) and will expand as content length increases.

If the h2 tag is part of the Footer component, consider making these adjustments for better accessibility:

const Footer = (
  <footer>
    <a href={"https://www.instagram.com/linghandmade18/"}>
      <i className="fab fa-instagram" />
    </a>
    <h2>Some Text</h2>
  </footer>
);

To style the footer, you can use the following CSS:

footer {
  position: absolute;
  bottom: 0;
  left: 0;
}

These adjustments will keep the footer at the bottom of the page regardless of content height.

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

My program contains redundant sections that are being repeated multiple times, and I am unsure of how to remedy this issue

This particular payment gateway relies on a paid market for processing transactions. Unfortunately, there seems to be an issue where multiple error messages are being triggered during the payment verification process. The errors include: ❌ | An error h ...

jquery mobile popup message will fade away within a short time interval

In my main page, I have the following code: $(document).bind("pageinit", function () { $.mobile.loading('hide'); }); I am displaying a popup message using the following code: $.mobile.loading('show&apos ...

Store the information from an AJAX post request in a div element into the browser

I've set up a datatable with a button that posts data into a div. Below is the HTML code I used: HTML : <div class="card-body"> <div class="overlay" id="kt_datatable_nodata"> <div class="o ...

Implement Infinite Scrolling Pagination in WordPress

I'm completely new to Wordpress and currently utilizing the FoundationPress theme. My goal is to include a button that users can click on to load more posts. Essentially, I want users to see four blog posts initially and then be able to load the next ...

Content not being hidden by Twitter Bootstrap tabs

I am encountering an issue with my code. When I click on an inactive tab, the content of the first tab does not hide and both tabs' content is displayed simultaneously. Is there a solution to this problem? Thank you for your help! <ul style="l ...

Tips for increasing the size of a parent div when a child div is displayed with a set width?

Is there a way to make the parent div automatically expand when the child div with a fixed width is displayed onclick? Goal: I want the child div to show up when I click the link, and at the same time, I need the parent div to expand or scale to fit the ...

Encountering a Parsing error while utilizing redux: Unexpected token present

Trying to implement Redux for managing the searchField state on the website. After creating action.js, reducer.js, constant.js, and redux.connect() function, An error occurred: Parsing error: Unexpected token 62 | return <div className=&apo ...

How can I utilize JavaScript to retrieve the background color of a table cell upon clicking?

I am trying to create a function where an alert pops up displaying the background color of a table cell whenever it is clicked. Unfortunately, I am having trouble accessing and retrieving the background color value. The table cell in question is defined a ...

Achieving overlapping of a <div> element with a "position: fixed" container while enabling vertical scrolling of the fixed container's content

In my single page application, I have a fixed container and I am trying to make one div inside this container overlap the boundaries of the fixed container while also vertically scrolling with its contents. Unfortunately, I have only been able to achieve e ...

The process of locating a textarea by its id becomes obsolete when integrating CKEDITOR

The data table has editable cells, where clicking on a cell will trigger a bootstrap modal to display with a textarea containing the text retrieved from the database. Snippet of the table structure: <table class="table table-striped table-hover" id="t ...

Direct AngularJS to automatically reroute users from the login page to the welcome page

I am currently developing a web application where I need to redirect from the login page to the welcome page once the user id and password have been validated. <script> var app = angular.module('myApp', []); app.controller(&apo ...

After updating the state, the Next.js axios call experiences a delay before executing the desired action

I am currently working on a NextJS project that relies on Axios for handling API calls. Within this project, there is a loading state implemented to show a loading spinner when making these API calls. However, I have encountered an issue where after click ...

Align Horizontal Fixed Element

<html> <head> <style> #rectangle { position: absolute; right: 0; bottom: 0; width: 150px; height: 200px; } </st ...

Tips for organizing an array of objects in JavaScript according to a specific attribute value?

I'm trying to sort an array of notification objects in decreasing order of severity: Error > Warning > Information. For example: var notificationArray = [ { code : "103", severity : "Error" }, { code : "104", severity : "Information" ...

It appears that the crackling noise is being generated by AudioContext.decodeAudioData

I am currently in the process of developing an electron app that enables users to cut and rearrange multiple audio samples while seamlessly playing them back. The combined duration of these samples can exceed an hour, making it impossible to decode and sto ...

Sending Data from Browser to Node.js using Ajax

I've been struggling to send an AJAX post request to my Node server built with Express for a while now. Despite reading various solutions on similar issues, I couldn't figure out which files to edit. Initially, I attempted using `http.createServe ...

The equivalent of ESM for resolving modules using the `createRequire` function with a specified

In the process of developing a JavaScript instrumentation engine, I am currently focused on traversing a source file's Abstract Syntax Tree (AST) and queuing imported modules for instrumentation in a recursive manner. In order to achieve this, it is c ...

Is it feasible to utilize the Next.js (App Router) Context API to access this context in every fetch request?

To perform database queries, I require some context information. The context functions are not accessible on the client side, so I have been passing this context as args whenever I call a server component. However, I am exploring more efficient ways to h ...

How can I acquire a duplicate of a Webgl texture?

I have a webgl texture and I have stored it in a JavaScript variable var texture1 = CreateTexture() function CreateTexture(){ var texture = gl.createTexture() // more WebGL texture creation code here return texture } I am looking to create a copy o ...

The use of JSON.stringify on ArrayData is not updating as expected within the useEffect hook in Next.js

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faSearch, faAmbulance, faAnchor, faPlus, faFloppyDisk, } from '@fortawesome/free-solid-svg-icons'; import { Input, Table, Button, Modal, Moda ...